使用Java从XSD创建XML

时间:2021-07-21 21:48:36

I am very much new to Java. I have an xsd and I need to create xml based on the xsd. I have seen that we can use JAXB to do the stuff. But I have seen xml examples which are simple in nature. I have a sample xsd as shown below which i need to convert to xml.

我是Java的新手。我有一个xsd,我需要根据xsd创建xml。我已经看到我们可以使用JAXB来完成这些工作。但是我看过xml的例子很简单。我有一个示例xsd,如下所示,我需要转换为xml。

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <xs:schema xmlns:addml="http://www.arkivverket.no/standarder/addml"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      targetNamespace="http://www.arkivverket.no/standarder/addml" elementFormDefault="qualified"
      version="8.2">
      <xs:annotation>
        <xs:documentation xml:lang="en">
          Changes made in versions up to 8.2 are not documented in this document.
          Updated 2014-08-15 and 2014-09-29, Terje Pettersen-Dahl:
          Version 8.3:
          1. Element reference in dataset made optional.
          2. Optional possibility for header-lines.
          3. FieldDefinitionReference made unique within an instance.
          4. Created this documentation section.

        </xs:documentation>
      </xs:annotation>
      <xs:element name="addml">
        <xs:complexType>
          <xs:sequence>
            <xs:element ref="addml:objectStore" maxOccurs="1"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="objectStore">
        <xs:complexType>
          <xs:sequence>
            <xs:element ref="addml:folder" minOccurs="0"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="folder">
        <xs:complexType>
          <xs:sequence>
             <xs:element ref="addml:folderProperties" minOccurs="0"/>
             <xs:element ref="addml:documents" minOccurs="0"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="documents">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="document" maxOccurs="unbounded" minOccurs="2">
              <xs:complexType>
                <xs:sequence>
                  <xs:element ref="addml:docProperties"/>
                </xs:sequence>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="docProperties">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="documentId" type="xs:string"/>
            <xs:element name="documentTitle" type="xs:string"/>
            <xs:element name="dateCreated" type="xs:string"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
       <xs:element name="folderProperties">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="documentId" type="xs:string"/>
            <xs:element name="documentTitle" type="xs:string"/>
            <xs:element name="dateCreated" type="xs:string"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:schema>

     <?xml version="1.0" encoding="utf-8"?>
    <addml>
      <objectStore>
        <folder>
          <folderProperties>
            <documentId>str1234</documentId>
            <documentTitle>str1234</documentTitle>
            <dateCreated>str1234</dateCreated>
          </folderProperties>
          <documents>
            <document>
              <docProperties>
                <documentId>str1234</documentId>
                <documentTitle>str1234</documentTitle>
                <dateCreated>str1234</dateCreated>
              </docProperties>
            </document>
            <document>
              <docProperties>
                <documentId>str1234</documentId>
                <documentTitle>str1234</documentTitle>
                <dateCreated>str1234</dateCreated>
              </docProperties>
            </document>
          </documents>
        </folder>
      </objectStore>
      </addml>

I need a XML like above. Note: I got the below XML using online converter.

我需要一个像上面这样的XML。注意:我使用在线转换器获得了以下XML。

Please help in creating the xml using Java. Any help is much appreciated. Thanks, Mark

请帮助使用Java创建xml。任何帮助深表感谢。谢谢,马克

1 个解决方案

#1


1  

You call

xjc sample.xsd

and it will generate, in folder no/arkivverket/standarder/addml/ a set of Java source files in that package, which you use to create a set of objects representing the XML data you want to be serialized ("marshalled") into an XML file.

它将在文件夹no / arkivverket / standarder / addml /中生成该包中的一组Java源文件,用于创建一组对象,表示要将其序列化(“编组”)的XML数据XML文件。

Finally you need a few lines of Java code for calling JAXBContext.newInstance, create a Marshaller and call its method marshal.

最后,您需要几行Java代码来调用JAXBContext.newInstance,创建Marshaller并调用其方法marshal。

Google for JAXB tutorials.

谷歌的JAXB教程。

#1


1  

You call

xjc sample.xsd

and it will generate, in folder no/arkivverket/standarder/addml/ a set of Java source files in that package, which you use to create a set of objects representing the XML data you want to be serialized ("marshalled") into an XML file.

它将在文件夹no / arkivverket / standarder / addml /中生成该包中的一组Java源文件,用于创建一组对象,表示要将其序列化(“编组”)的XML数据XML文件。

Finally you need a few lines of Java code for calling JAXBContext.newInstance, create a Marshaller and call its method marshal.

最后,您需要几行Java代码来调用JAXBContext.newInstance,创建Marshaller并调用其方法marshal。

Google for JAXB tutorials.

谷歌的JAXB教程。