Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Marshalling and 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>

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.

like image 657
jagamot Avatar asked Sep 30 '11 03:09

jagamot


1 Answers

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

@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.)

Acquire JAXBContext, Unmarshaller and Marshaller

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

// 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);
like image 152
Kohányi Róbert Avatar answered Sep 22 '22 20:09

Kohányi Róbert