【问题标题】:Create XML from XSD using Java使用 Java 从 XSD 创建 XML
【发布时间】:2015-10-29 05:36:58
【问题描述】:

我对 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。任何帮助深表感谢。 谢谢,马克

【问题讨论】:

  • 到目前为止你做了什么,你在哪里卡住了? StackOverflow 不是代码(或 XML)编写服务;您需要提出一个具体且可回答的问题。
  • 嗨 Erwin,我对 Java 非常陌生。我需要有关如何从 xsd 创建 xml 的输入。我使用 JAXB 为示例 xsd 创建了一个 xml。在上面的例子中, 标签是重复元素,也是动态的。有时我可能会得到 3 个元素,有时是 5 个。所以我需要知道如何实现这种行为。谢谢
  • 我认为你的做法是错误的。您不能从 XSD 文件创建 XML 文档(至少没有意义)。您需要从要在 XML 中编码的数据开始,并且在创建 XML 时,您需要确保它满足模式。如果您使用 JAXB,它已经对您有所帮助,因为它只会为 XSD 中的元素和属性提供 Java 属性。
  • @user1194310 看看stackoverflow.com/questions/15417330/…。虽然它与您的问题不同,但接受的答案显示了使用访问者模式导航 XSD 架构的好方法。

标签: java xml xsd jaxb


【解决方案1】:

你打电话

xjc sample.xsd

它会在 no/arkivverket/standarder/addml/ 文件夹中生成该包中的一组 Java 源文件,您可以使用这些文件创建一组表示您想要序列化的 XML 数据的对象(“编组” ) 到 XML 文件中。

最后你需要几行 Java 代码来调用 JAXBContext.newInstance,创建一个 Marshaller 并调用它的方法 marshal。

Google 获取 JAXB 教程。

【讨论】:

    猜你喜欢
    • 2012-10-05
    • 2010-10-22
    • 2015-12-12
    • 1970-01-01
    • 2011-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-25
    相关资源
    最近更新 更多