[英]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.