jag*_*mot 5 java xml jaxb marshalling unmarshalling
的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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
使用这两个模式的第三方应用程序生成一个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>
Run Code Online (Sandbox Code Playgroud)
第二个模式始终是固定模式.
我的要求是解析关系标记,如果关系是"self",我需要用相应字段的firstName和lastName覆盖empFirstName和empLastName.
我怎样才能做到这一点?
编辑1:Employee.xsd是动态的,可以是任何东西.但Family.xsd是静态的,可以从任何其他xsd导入.
总体概述:(a)映射XML模式文件,(b)解组给定的XML,(c)最终修改内存中的XML对象(d)将修改后的对象编组到任何你想要的地方.
@XmlRootElement(name = "FamilyFields") public class FamilyFields {
@XmlElement public String relation;
@XmlElement public String firstName;
@XmlElement public String lastName;
public FamilyFields() {}
}
@XmlRootElement(name = "NewFields") public class NewFields {
@XmlElement public String empFirstName;
@XmlElement public String empLastName;
@XmlElementWrapper(name = "family")
@XmlElement(name = "FamilyFields")
public List<FamilyFields> familyFields;
public NewFields() {}
}
Run Code Online (Sandbox Code Playgroud)
(如果您接受建议:手动完成!使用XJC生成JAXB实体几乎从不输出您期望的内容,如果您手边没有非常简单的模式文件,则会花费很多时间.)
JAXBContext,Unmarshaller和MarshallerJAXBContext context = JAXBContext.newInstance(NewFields.class, FamilyFields.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
Marshaller marshaller = context.createMarshaller();
// set optional properties
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
Run Code Online (Sandbox Code Playgroud)
// the XML content you gave
String xml = ...
StringReader reader = new StringReader(xml);
NewFields newFields = (NewFields) unmarshaller.unmarshal(reader);
// modify the unmarshalled data to your heart's content
newFields.empLastName = ...
// marshal the modified data anywhere you want
marshaller.marshal(newFields, System.out);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10975 次 |
| 最近记录: |