Jaxb 多个 xml 解组
Jaxb multiple xml unmarshalling
我有 2 个 xsd 文件,一个声明作者,一个声明书籍并从另一个 xsd 导入作者。
作者:
<xs:schema targetNamespace="ro.utcluj.cs.model.author"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="author">
<xs:complexType>
<xs:sequence>
<xs:element name="authorName" type="xs:string"/>
<xs:element name="authorAge" type="xs:integer"/>
<xs:element name="CNP" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
对于书籍:
<xs:schema targetNamespace="ro.utcluj.cs.model.book"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:author="ro.utcluj.cs.model.author"
xmlns:tns="ro.utcluj.cs.model.book"
elementFormDefault="qualified">
<xs:import namespace="ro.utcluj.cs.model.author" schemaLocation="author.xsd"/>
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="bookName" type="xs:string"/>
<xs:element name="title" type="xs:string"/>
<xs:element ref="author:author"/>
<xs:element name="genre" type="tns:bookGenre"/>
<xs:element name="quantity" type="xs:integer"/>
<xs:element name="price" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:simpleType name="bookGenre">
<xs:restriction base="xs:string">
<xs:enumeration value="ACTION"/>
<xs:enumeration value="HISTORY"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
我想写一些 xml 文件并用 Jaxb 解组它们。我可以在一个 xml 中写作者,在另一个 xml 中写书,让 Jaxb 解组它们然后以某种方式合并它们吗?或者由于书籍对作者有依赖性,它们都必须用相同的 xml 编写并指定它们的命名空间?
我不确定你想做什么但是如果你写了两个 *.xml 文件并且你想解组它们你必须知道其中哪些应该根据 "books.xsd" 和这反对 "authors.xsd"。
假设您已经从 xsds 生成了 类(如果不是,您可以阅读:https://springframework.guru/you-should-use-jaxb-generated-classes-for-restful-web-services/ OR http://www.beingjavaguys.com/2013/04/create-spring-web-services-using-maven.html)
接下来您必须知道如何使用 unmarshal/marshal 功能。 (http://www.source4code.info/2013/07/jaxb-marshal-unmarshal-with-missing.html)
在你拥有 Book 和 Author 类型的对象后,你可以使用反射将 Book 对象中的字段 "author" 设置为 Author 类型的未编组对象(正如我从 Author 类型中看到的那样)由你的 author.xml.
组成
另一种解决方案是将您的 book.xml 和 author.xml 文件作为字符串并手动编写一个函数来查找和替换字符串中的元素。然后你可以 "put the author in the book" 并且当你解组字符串时,你将拥有来自 Book 类型的完整对象,其中包含作者的完整信息(来自 author.xml)。
希望我给了你一些指导,祝你好运!
我有 2 个 xsd 文件,一个声明作者,一个声明书籍并从另一个 xsd 导入作者。
作者:
<xs:schema targetNamespace="ro.utcluj.cs.model.author"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="author">
<xs:complexType>
<xs:sequence>
<xs:element name="authorName" type="xs:string"/>
<xs:element name="authorAge" type="xs:integer"/>
<xs:element name="CNP" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
对于书籍:
<xs:schema targetNamespace="ro.utcluj.cs.model.book"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:author="ro.utcluj.cs.model.author"
xmlns:tns="ro.utcluj.cs.model.book"
elementFormDefault="qualified">
<xs:import namespace="ro.utcluj.cs.model.author" schemaLocation="author.xsd"/>
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="bookName" type="xs:string"/>
<xs:element name="title" type="xs:string"/>
<xs:element ref="author:author"/>
<xs:element name="genre" type="tns:bookGenre"/>
<xs:element name="quantity" type="xs:integer"/>
<xs:element name="price" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:simpleType name="bookGenre">
<xs:restriction base="xs:string">
<xs:enumeration value="ACTION"/>
<xs:enumeration value="HISTORY"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
我想写一些 xml 文件并用 Jaxb 解组它们。我可以在一个 xml 中写作者,在另一个 xml 中写书,让 Jaxb 解组它们然后以某种方式合并它们吗?或者由于书籍对作者有依赖性,它们都必须用相同的 xml 编写并指定它们的命名空间?
我不确定你想做什么但是如果你写了两个 *.xml 文件并且你想解组它们你必须知道其中哪些应该根据 "books.xsd" 和这反对 "authors.xsd"。
假设您已经从 xsds 生成了 类(如果不是,您可以阅读:https://springframework.guru/you-should-use-jaxb-generated-classes-for-restful-web-services/ OR http://www.beingjavaguys.com/2013/04/create-spring-web-services-using-maven.html)
接下来您必须知道如何使用 unmarshal/marshal 功能。 (http://www.source4code.info/2013/07/jaxb-marshal-unmarshal-with-missing.html)
在你拥有 Book 和 Author 类型的对象后,你可以使用反射将 Book 对象中的字段 "author" 设置为 Author 类型的未编组对象(正如我从 Author 类型中看到的那样)由你的 author.xml.
组成另一种解决方案是将您的 book.xml 和 author.xml 文件作为字符串并手动编写一个函数来查找和替换字符串中的元素。然后你可以 "put the author in the book" 并且当你解组字符串时,你将拥有来自 Book 类型的完整对象,其中包含作者的完整信息(来自 author.xml)。
希望我给了你一些指导,祝你好运!