简体   繁体   English

Java 编组和解组

[英]Java Marshalling and UnMarshalling

Employee.xsd员工.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家庭.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 -使用这两种模式的第三方应用程序会生成一个 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>

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.我的要求是解析关系标签,如果关系是“self”,我需要用各自字段的 firstName 和 lastName 覆盖 empFirstName 和 empLastName。

How can I achieve this?我怎样才能做到这一点?

EDIT 1 : Employee.xsd is dynamic and could be anything.编辑 1:Employee.xsd 是动态的,可以是任何东西。 But Family.xsd is static and can be imported from any other xsd.但是 Family.xsd 是静态的,可以从任何其他 xsd 导入。

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 .大纲:(a)映射您的 XML 模式文件,(b)解组给定的 XML ,(c) 最后修改内存中的 XML 对象(d)将修改后的对象编组到您想要的任何位置

Map your XML schema files映射您的 XML 架构文件

@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() {}
}

(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.) (如果你接受一个建议:手工做!用 XJC 生成 JAXB 实体几乎从不输出你期望的东西,如果你手头没有非常简单的模式文件,可能会很耗时。)

Acquire JAXBContext , Unmarshaller and Marshaller获取JAXBContextUnmarshallerMarshaller

JAXBContext 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);

Handle incoming XML处理传入的 XML

// 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);

If you have an XSD then manipulating XML is the easiest with XML data binding.如果您有 XSD,那么使用 XML 数据绑定最容易操作 XML。 Using the XSD you can create java bean like classes that represent the XSD.使用 XSD,您可以创建代表 XSD 的类 java bean。 Now you can getters and setters on the java class and then output the XML.现在您可以在 java 类上使用 getter 和 setter,然后输出 XML。 There are many XML data binding solutions.有许多 XML 数据绑定解决方案。 Try XML beans:尝试 XML bean:

http://xmlbeans.apache.org/ http://xmlbeans.apache.org/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM