[英]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 数据的对象XML 文件。
最后,您需要几行 Java 代码来调用 JAXBContext.newInstance,创建一个 Marshaller 并调用它的方法 marshal。
谷歌 JAXB 教程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.