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.
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.
@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.)
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);
// 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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With