简体   繁体   中英

xmlns attribute makes the object null while unmarshalling with JAXB

The code below works perfectly and i am able to get the success value when i remove the namesace xmlns(xmlns="http://w/p-print") from the xml. But when xml comes with xmlns attribute, response object becomes null after unmarshalling. I tried to map the xmlns attrubute in Response class too but it did not make any difference. Seems like whenever I run into xmlns attrubute in the xml, entire Response object being null. Any idea about it? Thank you all in advance.

The xml that I am unmarshalling `

<?xml version="1.0" encoding="utf-8"?>
<PRInfo Version="2.00">
    
        <ePrint>
            <Params>
                <devid>test</devid>
                <printjobid>2323</printjobid>
            </Params>
            <PResonse>
                <response xmlns="http://w/p-print" success="true"/>. 
                </response>
            </PResonse>
        <ePrint>

</PRInfo>

`

Class Based Mapping

@XmlAccessorType(XmlAccessType.FIELD)
public class PRInfo {

    // required XmlElements comes here

    @XmlAccessorType(XmlAccessType.FIELD)
    public static class ePrint {

        //required XmlElementa comes here

        @XmlAccessorType(XmlAccessType.FIELD)
        public static class Params {
            // required XmlElement come here...
        }

        @XmlAccessorType(XmlAccessType.FIELD)
        @XmlType(name = "", propOrder = {"response"})
        public static class PResonse {

            @XmlElement
            private Response response;
           // Getter Setter
        }
    }
}

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "response")
public class Response {

    @XmlAttribute(name = "success")
    protected Boolean success;
// Getter and Setter
}

Unmarshal the xml and get the success value from response object.

When the xmlns attribute is present in the response element, it creates a default namespace for the element and its children. This means that the success attribute is in the default namespace, and the Response class you have defined is not able to map it because it does not have a namespace declaration.

Try to add namespace declarations to the Response class. You can do this by annotating the class with the @XmlType annotation and setting the namespace attribute to the value of the xmlns attribute in the XML document. For example:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "response", namespace = "http://w/p-print")
public class Response {

    @XmlAttribute(name = "success")
    protected Boolean success;
    // Getter and Setter
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM