Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB converting XSD to Java classes

When I run the following command:

xjc -b xmlSchema.xjb -d src -p com.q1labs.qa.xmlgenerator.model.generatedxmlclasses xmlSchema.xsd

It creates Java classes however I've found that my root class does not have the correct name and does not have @XmlRootElement which declares it as a root element which means when I use the classes to generate XML it is not formed properly.

XSD Schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified"
    elementFormDefault="qualified" targetNamespace="http://ibm.org/seleniumframework"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="Test" type="sel:TestType" xmlns:sel="http://ibm.org/seleniumframework"/>

    <xs:complexType name="TestType">
        <xs:choice minOccurs="1" maxOccurs="unbounded">
            <xs:element type="sel:Option1" name="Option1" xmlns:sel="http://ibm.org/seleniumframework"/>
            <xs:element type="sel:Option2" name="Option2" xmlns:sel="http://ibm.org/seleniumframework"/>
            <xs:element type="sel:Option3" name="Option3" xmlns:sel="http://ibm.org/seleniumframework"/>
        </xs:choice>
    </xs:complexType>

This is the output I am getting:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<testType xmlns="http://ibm.org/seleniumframework"/>
like image 987
Colin747 Avatar asked Aug 29 '26 09:08

Colin747


1 Answers

Generated classes correspond to complex types. Anonymous complex types that are declared as part of global elements will get an @XmlRootElement annotation. Others will have a @XmlElementDecl annotation generated on the ObjectFactory class. This is because there may be more than one global element that corresponds to the same complex type.

For More Information

  • http://blog.bdoughan.com/2012/07/jaxb-and-root-elements.html
like image 115
bdoughan Avatar answered Aug 31 '26 02:08

bdoughan