Java 编码和解码

标签 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>

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>

使用这 2 个模式的第三方应用程序生成 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 导入。

最佳答案

大纲:(a) 映射您的 XML 模式文件,(b) 解码给定的 XML,(c) 修改内存中的 XML 对象 最后 (d) 将修改后的对象编码到您想要的任何地方

映射您的 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() {}
}

(如果您听取建议:手动操作!使用 XJC 生成 JAXB 实体几乎不会输出您期望的内容,如果您没有非常简单的方法,可能会耗费大量时间手边的模式文件。)

获取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);

处理传入的 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);

关于Java 编码和解码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7605734/

相关文章:

类 Entity 中的 java.lang.IllegalAccessError : tried to access field ConcreteEntity. 实例

xml - xpath获取整个xml树结构

python - 如何在 Python 中验证具有多个命名空间的 XML?

java - 意外的 JAXB 错误

java - jaxb 和 jsr303

java - Jaxb 编码 - 从 java 对象获取 xml 中的所有字段

JavaFX TreeView 展开/折叠披露箭头错误

java - SSLContextImpl 未初始化

java - 如何使用 Square 的 Retrofit 网络库实现异步回调

xml - 什么版本的 XPath 在 XML::LibXML 中实现?