简体   繁体   中英

Generate xml String from a xsd file in Java

I have a big schema file ( .xsd ) that has choices that you can choose from:

<xs:element name="MyData">
    <xs:complexType>
        <xs:choice maxOccurs="unbounded">
            <xs:element name="AAA" type="AAAType" maxOccurs="unbounded" />
            <xs:element name="BBB" type="BBBType" maxOccurs="unbounded" />
            <xs:element name="CCC" type="CCCType" maxOccurs="unbounded" />
            <xs:element name="DDD" type="DDDType" maxOccurs="unbounded" />
            <xs:element name="EEE" type="EEEType" maxOccurs="unbounded" />
            <xs:element name="FFF" type="FFFType" maxOccurs="unbounded" />
            <xs:element name="GGG" type="GGGType" maxOccurs="unbounded" />
            <xs:element name="HHH" type="HHHType" maxOccurs="unbounded" />
            <xs:element name="III" type="IIIType" maxOccurs="unbounded" />
            <xs:element name="JJJ" type="JJJType" maxOccurs="unbounded" />
            <xs:element name="KKK" type="KKKType" maxOccurs="unbounded" />
            <xs:element name="LLL" type="LLLType" maxOccurs="unbounded" />
            <xs:element name="MMM" type="MMMType" maxOccurs="unbounded" />
            <xs:element name="NNN" type="NNNType" maxOccurs="unbounded" />
            <xs:element name="OOO" type="OOOType" maxOccurs="unbounded" />
            <xs:element name="PPP" type="PPPType" maxOccurs="unbounded" />
            <xs:element name="QQQ" type="QQQType" maxOccurs="unbounded" />
            <xs:element name="RRR" type="RRRType" maxOccurs="unbounded" />
            <xs:element name="SSS" type="SSSType" maxOccurs="unbounded" />
            <xs:element name="TTT" type="TTTType" maxOccurs="unbounded" />
            <xs:element name="UUU" type="UUUType" maxOccurs="unbounded" />
            <xs:element name="VVV" type="VVVType" maxOccurs="unbounded" />
            <xs:element name="WWW" type="WWWType" maxOccurs="unbounded" />
            <xs:element name="XXX" type="XXXType" maxOccurs="unbounded" />
            <xs:element name="YYY" type="YYYType" maxOccurs="unbounded" />
            <xs:element name="ZZZ" type="ZZZType" maxOccurs="unbounded" />
        </xs:choice>
        <xs:attribute name="version" type="xs:string" />
    </xs:complexType>
</xs:element>

Each type has its own sequences and complex types. Is there a way I can program something in Java where I give it the choice as a String, the program looks in the schema file and outputs a XML String? Similar to how you can create a XML file in Eclipse given the schema, I want to do it programmatically.

Have a look at JAXB, which reads schema files and generates Java classes that follow the rules set in your XSD. It basically creates POJO objects, which have a bunch of setters to easily populate the required XML data.

JAXB is commonly used, and worthwhile learning anyway.

See the official documentation here

Lesson: Introduction to JAXB

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM