1

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);
    }
}
Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49
  • I don't know if it just a copy-paste error or part of the problem. The elements `` and `` of your XML example don't match the `@XmlElement`s of your `Information` class (`name = "LastName"` and `name="NickName"`). – Thomas Fritsch Jan 20 '18 at 23:52
  • it was copy past error. Corrected the question – Pankaj Khairnar Jan 22 '18 at 05:33

1 Answers1

1

The problem is in your getComapany(String xmlString) method in class MyUnmarshaller: Your SAXParserFactory is not aware of namespaces. You need to call setNamespaceAware(true) on it for fixing the problem:

public Company getComapany(String xmlString) throws Exception {
    StringReader reader = new StringReader(xmlString);
    JAXBContext jaxbContext = JAXBContext.newInstance(Company.class);
    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setNamespaceAware(true);  // !!!!
    Source xmlSource = new SAXSource(spf.newSAXParser().getXMLReader(), new InputSource(reader));
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    return (Company) jaxbUnmarshaller.unmarshal(xmlSource);
}

But in my opinion you don't need the stuff with SAXParserFactory and SAXSource at all. You can simplify the method like this:

public Company getComapany(String xmlString) throws Exception {
    StringReader reader = new StringReader(xmlString);
    JAXBContext jaxbContext = JAXBContext.newInstance(Company.class);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    return (Company) jaxbUnmarshaller.unmarshal(reader);
}

Both methods above work fine with the rest of your code, i.e. the nickName of class Information is correctly set to "sedembest@gmail.com" as given in the XML input.

Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49