简体   繁体   中英

Create XML from XSD using Java

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.

    <?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.

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

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.

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

Google for JAXB tutorials.

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