ObjectFactory, Marshalling, Unmarshalling

280 views
Skip to first unread message

Sphere

unread,
Mar 11, 2010, 11:38:06 AM3/11/10
to Java API for KML
Hello,

i would like to use this example:
from http://labs.micromata.de/display/jak/KML+in+the+Java+world#KMLintheJavaworld-JAXBXSD%3DJAK

My Problem is: i've this message: i don't know how can i solve it?

I would be appreciate for any help.

Best regards,
Sphere

"HelloKml.class" doesnt contain ObjectFactory.class or jaxb.index
at
com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:
197)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
.........

Code: [
public class HelloKml {

public static void main(String[] args) throws FileNotFoundException,
JAXBException, SAXParseException {

Kml kml = KmlFactory.createKml();

Placemark placemark = KmlFactory.createPlacemark();
placemark.setName("Bahareh- My Placemark");
placemark.setDescription("HelloKml");
placemark.setVisibility(true);
placemark.setStyleUrl("styles.kml#jugh_style");

placemark.createAndSetLookAt()
.withLongitude(-90.86879847669974).withLatitude(48.25330383601299)
.withRange(440.8).withTilt(8.3).withHeading(2.7);

Point point = KmlFactory.createPoint();
point.addToCoordinates("-90.86948943473118,48.25450093195546,0");
point.setExtrude(false);
point.setAltitudeMode(AltitudeMode.CLAMP_TO_GROUND);

point.addToCoordinates("9.444652669565212,51.30473589438118,0");

placemark.setGeometry(point);

kml.setFeature(placemark);

JAXBContext jc = JAXBContext.newInstance("HelloKml.class");

Unmarshaller u = jc.createUnmarshaller();

Kml kml3 = (Kml) u.unmarshal(new File("HelloKml.xml"));

Marshaller m = jc.createMarshaller();
StringWriter sw = new StringWriter();
m.marshal(kml3, sw);

}
}

]

mike

unread,
Mar 28, 2011, 4:20:26 PM3/28/11
to javaap...@googlegroups.com, Sphere
I want to do something similar to the original poster and am getting the same error.  I want to marshal only a placemark (I'll be creating a full kml out of a subset of all the placemarks at a later time). I am not finding any way to marshal an xml fragment in the JAK API.

Thanks,
mike

Emanuele Ziglioli

unread,
May 8, 2011, 8:01:19 PM5/8/11
to Java API for KML
Hi,

I've already posted a message but can't see it yet.

Anyway, it's about the same problem using the Jaxb extension of the
Restlet library.
If I return a Kml representation, I get an error: can't find
ObjectFactory.

Now, the problem with JAK is that it removes ObjectFactory classes at
the package level, and that's what Restlet expects to find.
But there is a solution, see:
http://cmaki.blogspot.com/2007/09/annotated-jaxb-classes.html
http://stackoverflow.com/questions/899668/how-do-i-use-a-jaxb-index-file

We can provide a text file "jaxb.index" under de/micromata/opengis/kml/
v_2_2_0 containing just the text "Kml".

It's all very good removing the package factories but I think JAK
should provide a "jaxb.index" file in order not to break compatibility
with clients of jaxb annotated libraries.
I might add it as a bug request for this google code project.

Emanuele

Emanuele Ziglioli

unread,
May 8, 2011, 7:10:44 PM5/8/11
to Java API for KML
I've incurred into this problem while trying to replace my current
homebuilt Kml library (out of schema using the jaxb compiler).
The library I built works well but I understand JAK solves a few
issues and seems to be a better choice.
But perhaps they've gone a bit far so that JAK doesn't behave any
more like a jaxb compiled library.

See http://labs.micromata.de/display/jak/Implementation#Implementation-CreateKMLFactory%28anddeleteJAXB%27sObjectFactory%29
Having deleted jaxb's ObjectFactory, it means perhaps breaking
compatibility with software that expects to find one.

For example, I'm using the Jaxb extension of the Restlet library. This
piece of code works with plain jaxb classes (as generated by the xjc
compiler ) but not with JAK:


Kml kml = new Kml();
....
ConverterHelper ch = new JaxbConverter();
VariantInfo vi = new VariantInfo( MediaType.APPLICATION_KML );
Representation re = ch.toRepresentation( kml, vi, this );
System.out.print( re.getText() );

Problem creating Marshaller
javax.xml.bind.JAXBException: "de.micromata.opengis.kml.v_2_2_0"
doesnt contain ObjectFactory.class or jaxb.index

I could always just return Strings and force the return MIME type to
be kml, but it doesn't look to me like a very elegant solution.

Emanuele

On Mar 29, 8:20 am, mike <mik...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages