Java 编组和解组
Employee.xsd
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" jxb:version="1.0" elementFormDefault="qualified">
<xsd:include schemaLocation="Family.xsd"/>
<xsd:element name="NewFields">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="empFirstName" type="xsd:string" />
<xsd:element name="empLastName" type="xsd:string" />
<xsd:element name="family" type="FamilyFields" nillable="true" maxOccurs="unbounded" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Family.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" jxb:version="1.0" elementFormDefault="qualified">
<xsd:complexType name="FamilyFields">
<xsd:sequence>
<xsd:element name="relation" type="xsd:string" />
<xsd:element name="firstName" type="xsd:string" />
<xsd:element name="lastName" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
使用这两个架构的第三方应用程序会生成一个 xml 字符串,例如 -
<NewFields>
<empFirstName>Kevin</empFirstName>
<empLastName>Smith</empLastName>
<family>
<FamilyFields>
<relation>self</relation>
<firstName>New Kevin</firstName>
<lastName>New Smith</lastName>
</FamilyFields>
<FamilyFields>
<relation>wife</relation>
<firstName>Jennifer</firstName>
<lastName>Smith</lastName>
</FamilyFields>
</family>
</NewFields>
第二个架构始终是固定架构。
我的要求是解析关系标签,如果关系是“self”,我需要用相应字段的firstName和lastName覆盖empFirstName和empLastName。
我怎样才能实现这个目标?
编辑 1:Employee.xsd 是动态的,可以是任何东西。但 Family.xsd 是静态的,可以从任何其他 xsd 导入。
Employee.xsd
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" jxb:version="1.0" elementFormDefault="qualified">
<xsd:include schemaLocation="Family.xsd"/>
<xsd:element name="NewFields">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="empFirstName" type="xsd:string" />
<xsd:element name="empLastName" type="xsd:string" />
<xsd:element name="family" type="FamilyFields" nillable="true" maxOccurs="unbounded" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Family.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" jxb:version="1.0" elementFormDefault="qualified">
<xsd:complexType name="FamilyFields">
<xsd:sequence>
<xsd:element name="relation" type="xsd:string" />
<xsd:element name="firstName" type="xsd:string" />
<xsd:element name="lastName" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
A third party application that uses these 2 schemas generate an xml string like -
<NewFields>
<empFirstName>Kevin</empFirstName>
<empLastName>Smith</empLastName>
<family>
<FamilyFields>
<relation>self</relation>
<firstName>New Kevin</firstName>
<lastName>New Smith</lastName>
</FamilyFields>
<FamilyFields>
<relation>wife</relation>
<firstName>Jennifer</firstName>
<lastName>Smith</lastName>
</FamilyFields>
</family>
</NewFields>
The second schema is always a fixed schema.
My requirement is to parse the relation tag and if the relation is "self", I need to overwrite empFirstName and empLastName with firstName and lastName of the respective fields.
How can I achieve this?
EDIT 1 : Employee.xsd is dynamic and could be anything. But Family.xsd is static and can be imported from any other xsd.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
总体概要:(a) 映射 XML 模式文件,(b) 解组给定的 XML,(c) 修改内存中的 XML 对象 > 最后 (d) 将修改后的对象编组到您想要的任何位置。
映射您的 XML 模式文件
(如果您接受建议:手动操作!使用 XJC 生成 JAXB 实体几乎永远不会输出您期望的内容,并且如果您没有的话,可能会非常耗时手头非常简单的架构文件。)
获取
JAXBContext
、Unmarshaller
和Marshaller
处理传入的 XML
The general outline: (a) map you XML schema files, (b) unmarshall the given XML, (c) modify the XML object in memory finally (d) marshall the modified object anywhere you want.
Map your XML schema files
(If you take an advice: do it by hand! Generating JAXB entities with XJC almost never outputs the things you expect and can get time consuming if you don't have very simple schema files at hand.)
Acquire
JAXBContext
,Unmarshaller
andMarshaller
Handle incoming XML
如果您有 XSD,那么使用 XML 数据绑定来操作 XML 是最简单的。使用 XSD,您可以创建代表 XSD 的类似 java bean 的类。现在您可以在 java 类上获取和设置方法,然后输出 XML。有许多 XML 数据绑定解决方案。尝试 XML bean:
http://xmlbeans.apache.org/
If you have an XSD then manipulating XML is the easiest with XML data binding. Using the XSD you can create java bean like classes that represent the XSD. Now you can getters and setters on the java class and then output the XML. There are many XML data binding solutions. Try XML beans:
http://xmlbeans.apache.org/