As am using jdk 8u144, when i tried to unmarkshal the xml to object i am getting null value for the field having namespace. Tried with NamespaceFilter class which takes namespaceUri and addNamespace fields as a parameters, but seems that it works only for top level fields in xml. Bit confused at this point what can be done to parse the child field that having namespace.
Given this string as an input:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Company xmlns:ns2="http://www.openapplications.org/oagis/9">
<Staff>
<BasicInfo>
<Information>
<firstName>ABC</firstName>
<lastName>Primary</lastName>
<ns2:nickName>sedembest@gmail.com</ns2:nickName>
</Information>
</BasicInfo>
</Staff>
</Company>
Company Class:
@XmlRootElement(name="Company")
public class Company {
private List<Staff> staff;
public List<Staff> getStaff() {
return staff;
}
@XmlElement(name="Staff")
public void setStaff(List<Staff> staff) {
this.Staff = staff;
}
}
Staff Class:
@XmlRootElement(name="Staff")
public class Staff {
private BasicInfo basicInfo;
public BasicInfo getBasicInfo() {
return basicInfo;
}
@XmlElement(name="BasicInfo")
public void setBasicInfo(BasicInfo basicInfo) {
this.basicInfo = basicInfo;
}
}
BasicInfo Class:
@XmlRootElement(name="BasicInfo")
public class BasicInfo {
private Information information;
public Information getInformation() {
return information;
}
@XmlElement(name="Information")
public void setInformation(Information information) {
this.information = information;
}
}
Information Class:
@XmlRootElement(name="Information")
public class Information {
private String firstName;
private String lastName;
private String nickName;
public String getFirstName() {
return firstName;
}
@XmlElement(name="firstName")
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
@XmlElement(name="lastName")
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getNickName() {
return nickName;
}
@XmlElement(namespace="http://www.openapplications.org/oagis/9", name="nickName")
public void setNickName(String nickName) {
this.nickName = nickName;
}
}
MyUnmarshaller Class:
public class MyUnmarshaller {
public Company getComapany(String xmlString){
StringReader reader = new StringReader(xmlString);
JAXBContext jaxbContext = JAXBContext.newInstance(Company.class);
SAXParserFactory spf = SAXParserFactory.newInstance();
Source xmlSource = new SAXSource(spf.newSAXParser().getXMLReader(), new InputSource(reader));
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
return (Company) jaxbUnmarshaller.unmarshal(xmlSource);
}
}