Reputation: 187
I need to generate XML file based on XSD template in Java, I can parse the XSD file, but don’t know after parsing, hot to generate XML file. Do you know any example or suggestion how to do it please? I’m not very expert on this, so I appreciate any help.
Many thanks in advance.
Upvotes: 0
Views: 6671
Reputation: 1638
Maybe you could use JAXB.
With xjc, you can convert your XSD file into JAXB annotated Java classes.
xjc -d src -p com.example.jaxb.beans schema.xsd
This would take the types defined in schema.xsd
, and generate in the src
folder the corresponding classes in the com.example.jaxb.beans
package.
With the generated classes, JAXBContext and Marshaller you can generate some XML output.
JAXBContext jc = JAXBContext.newInstance("com.example.jaxb.beans");
Marshaller m = jc.createMarshaller();
OutputStream os = new FileOutputStream("output.xml");
m.marshal(element, os);
element
being an instance of one of the classes generated previously.
Upvotes: 6