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 using these 2 schemes generates an xml string, for example -

 <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 scheme is always a fixed scheme.

My requirement is to parse the relationship tag and if the relationship is "I", I need to rewrite empFirstName and empLastName with the firstName and lastName of the corresponding fields.

How can i achieve this?

EDIT 1: Employee.xsd is dynamic and can be anything. But Family.xsd is static and can be imported from any other xsd.

+4
source share
2 answers

General schema: (a) displays XML schema files, (b) cancels the XML data mapping, (c) modifies the XML object in memory, and finally (d) sorts the modified object anywhere.

Map 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 ask for advice: do it manually ! Generating JAXB objects using XJC almost never displays the expected things and can take a long time if you do not have very simple schema files on 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); 

Inbound XML Processing

 // 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 content newFields.empLastName = ... // marshal the modified data anywhere you want marshaller.marshal(newFields, System.out); 
+5
source

If you have XSD, then XML manipulation is the easiest with XML data binding. Using XSD, you can create java bean-like classes that represent XSD. Now you can get and configure java classes and then output XML. There are many solutions for binding XML data. Try XML beans:

http://xmlbeans.apache.org/

0
source

All Articles