Another sample for using JAXB

Recently I blogged a sample for using JAXB and thought of sharing it on my blog. The sample for JAXB can be found here.

3 thoughts on “Another sample for using JAXB”

  1. Hi Mohamed,
    I have gone through your jaxb blog and it was nice. I have one requirement and need your help on this.
    Requirement: Convert XSD file to XML file using Java.
    I have seach through google and found the below solution. I am getting compilation error when i compile the below program.



    import javax.xml.transform.TransformerConfigurationException;

    import org.apache.xerces.xs.*;
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;

    import jlibs.xml.sax.XMLDocument;
    import javax.xml.namespace.QName;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;

    import jlibs.xml.xsd.XSInstance;
    import jlibs.xml.xsd.XSParser;

    public class Test1{
    public static void main(String[] pArgs) {
    try {
    String filename = “policy.xsd”;
    // instance.

    final Document doc = loadXsdDocument(filename);

    //Find the docs root element and use it to find the targetNamespace
    final Element rootElem = doc.getDocumentElement();
    String targetNamespace = null;
    if (rootElem != null && rootElem.getNodeName().equals(“xs:schema”))
    targetNamespace = rootElem.getAttribute(“targetNamespace”);

    //Parse the file into an XSModel object
    XSModel xsModel = new XSParser().parse(filename);

    //Define defaults for the XML generation
    XSInstance instance = new XSInstance();
    instance.minimumElementsGenerated = 1;
    instance.maximumElementsGenerated = 1;
    instance.generateDefaultAttributes = true;
    instance.generateOptionalAttributes = true;
    instance.maximumRecursionDepth = 0;
    instance.generateAllChoices = true;
    instance.showContentModel = true;
    instance.generateOptionalElements = true;

    //Build the sample xml doc
    //Replace first param to XMLDoc with a file input stream to write to file
    QName rootElement = new QName(targetNamespace, “HighSchoolTranscript”);
    XMLDocument sampleXml = new XMLDocument(new StreamResult(System.out), true, 4, null);
    instance.generate(xsModel, rootElement, sampleXml);
    } catch (TransformerConfigurationException e)
    // TODO Auto-generated catch block

    public static Document loadXsdDocument(String inputName) {
    final String filename = inputName;

    final DocumentBuilderFactory factory = DocumentBuilderFactory
    Document doc = null;

    try {
    final DocumentBuilder builder = factory.newDocumentBuilder();
    final File inputFile = new File(filename);
    doc = builder.parse(inputFile);
    } catch (final Exception e) {
    // throw new ContentLoadException(msg);

    return doc;


    Compliation error :

    Exception in thread “main” jlibs.core.lang.ImpossibleException: java.lang.ClassCastException: org.apache.xerces.dom.DOMXSImplementationSourceImpl cannot be cast to org.w3c.dom.DOMImplementationSource
    at jlibs.xml.xsd.XSParser.(
    at jlibs.xml.xsd.XSParser.(
    at Test1.main(
    Caused by: java.lang.ClassCastException: org.apache.xerces.dom.DOMXSImplementationSourceImpl cannot be cast to org.w3c.dom.DOMImplementationSource
    at org.w3c.dom.bootstrap.DOMImplementationRegistry.newInstance(
    at jlibs.xml.xsd.XSParser.(
    … 2 more

    Line number 36 means, at XSModel xsModel = new XSParser().parse(filename); of above program.

    Kindly help me. Appreciate your support. Kindly let me know if any information required. Looking forward for your support.


Leave a Reply

%d bloggers like this: