使用 Java 从 XSD 创建 XML
Create XML from XSD using Java
我对 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>
我需要像上面那样的 XML。注意:我使用在线转换器得到以下 XML。
请使用 Java 帮助创建 xml。任何帮助深表感谢。
谢谢,马克
你打电话
xjc sample.xsd
它将在文件夹 no/arkivverket/standarder/addml/ 中生成该包中的一组 Java 源文件,您可以使用这些文件创建一组代表您的 XML 数据的对象想要序列化 ("marshalled") 到 XML 文件中。
最后你需要几行Java代码来调用JAXBContext.newInstance,创建一个Marshaller并调用它的方法marshal。
Google 用于 JAXB 教程。
我对 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>
我需要像上面那样的 XML。注意:我使用在线转换器得到以下 XML。
请使用 Java 帮助创建 xml。任何帮助深表感谢。 谢谢,马克
你打电话
xjc sample.xsd
它将在文件夹 no/arkivverket/standarder/addml/ 中生成该包中的一组 Java 源文件,您可以使用这些文件创建一组代表您的 XML 数据的对象想要序列化 ("marshalled") 到 XML 文件中。
最后你需要几行Java代码来调用JAXBContext.newInstance,创建一个Marshaller并调用它的方法marshal。
Google 用于 JAXB 教程。