简体   繁体   English

从Java中的xsd文件生成xml字符串

[英]Generate xml String from a xsd file in Java

I have a big schema file ( .xsd ) that has choices that you can choose from: 我有一个很大的架构文件( .xsd ),可以从以下选项中进行选择:

<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? 有没有一种方法可以用Java编程,可以选择将其作为字符串,然后在模式文件中查找并输出XML字符串? Similar to how you can create a XML file in Eclipse given the schema, I want to do it programmatically. 与您可以在给定架构的Eclipse中创建XML文件的方式类似,我想以编程方式进行操作。

Have a look at JAXB, which reads schema files and generates Java classes that follow the rules set in your XSD. 看一下JAXB,它读取模式文件并生成遵循XSD中设置的规则的Java类。 It basically creates POJO objects, which have a bunch of setters to easily populate the required XML data. 它基本上创建了POJO对象,该对象具有许多设置器,可以轻松地填充所需的XML数据。

JAXB is commonly used, and worthwhile learning anyway. JAXB是常用的,无论如何都值得学习。

See the official documentation here 在这里查看官方文档

Lesson: Introduction to JAXB 课程:JAXB简介

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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