How to generate an XML template from an XSD SCHEMA using JAXB?

Asked

Viewed 1,019 times

1

I have a Schema . XSD and would like to generate an XML from this template. Below I have this code, however I am using the Jlibs library. I wonder if it is possible to do something similar to the JAXB library. The XSD file is being passed as parameter to the method makeXmlModel().

public static boolean makeXmlModel(File file)
{
    final XSModel xsModel = new XSParser().parse(file.getPath());
    final XSInstance xsInstance = new XSInstance();
    xsInstance.generateOptionalElements = Boolean.TRUE; // null means random
    final QName rootElement = new QName("http://www.portalfiscal.inf.br/nfe", "NFe");
    XMLDocument sampleXml;
    try {
         sampleXml = new XMLDocument(new StreamResult("model_xml.xml"), true, 4, null);
         xsInstance.generate(xsModel, rootElement, sampleXml);
         return true;
     } catch (TransformerConfigurationException e) {
         e.printStackTrace();
         return false;
     } catch (IllegalArgumentException e) {
         return false;
     }
}
  • Is your goal to generate an XML template from XSD? If so, use the XJC utility to create the JAXB class set from . XSD, and then write the code creating these objects, at the end, use the JAXB blioteca to make the objects Marshall, with the values stoned.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.