HOW DO I USE THE XML MARSHAL and UNMARSHAL
Before we can marshal, we need to create a Java class that represents and processes our XML structure.
Once that is done, reading, writing and processing the XML data is as easy as setting and getting information from a Java class.
So, let us see exactly how that is done:
We create a XSD file, which holds the structure of our XML document.
Now that the XSD file is created, we can generate the Java class(es) with the XJC command.
Here is an example of XJC in an ANT build script, and here is an example of a MAVEN pom that instantiates XJC.
After generating the Java Classes from the XSD, we are in a position to write some very easy code, based on a very solid foundation.
To marshal to a file, we would do the following:
MyGeneratedType mgt = new MyGeneratedType();
mgt.setSomething("ABC");
ObjectFactory of = new ObjectFactory();
TLptsXmlUtil.marshal("name.of.file.to.write.xml",MyGeneratedType.class,of.createMyGeneratedType(mgt));
To unmarshal a file, we would do the following:
MyGeneratedType mgt = (MyGeneratedType)TLptsXmlUtil.marshal("name.of.file.to.read.xml",MyGeneratedType.class);
String mySomething = mgt.getSomething();
|