简体   繁体   中英

JAXB Parent and Child node Same name. Child node returns null values

I consume a webservice and I'm receiving an XML response with a parent and a child node with the same name. The problem is that the last hierarchie has no values.

From my point of view JAXB should handle a List TestDetails.

Class Envelope:

@XmlRootElement(name="Envelope", namespace="http://schemas.xmlsoap.org/soap/envelope/") 
@XmlAccessorType(XmlAccessType.FIELD)
public class Envelope {

@XmlElement(name="Body", namespace="http://schemas.xmlsoap.org/soap/envelope/")
private Body Body;

}

Class Body:

@XmlAccessorType(XmlAccessType.FIELD)
public class Body {

@XmlElement(name="GetTestlistWithConnectionsResponse", namespace="http://tempuri.org/")
private GetTestlistWithConnectionsResponse GetTestlistWithConnectionsResponse;

public Body() {}

}

Class GetTestlistWithConnectionsResponse:

@XmlAccessorType(XmlAccessType.FIELD)
public class GetTestlistWithConnectionsResponse {

public GetTestlistWithConnectionsResponse() {}

@XmlElement(name="GetTestlistWithConnectionsResult", 
namespace="http://tempuri.org/")
private GetTestlistWithConnectionsResult GetTestlistWithConnectionsResult;


}

Class GetTestlistWithConnectionsResult:

@XmlAccessorType(XmlAccessType.FIELD)
public class GetTestlistWithConnectionsResult {

public GetTestlistWithConnectionsResult() {}

@XmlElement(name="TestDetails", namespace="http://schemas.datacontract.org/XXX")
private TestDetails TestDetails ;

}

Class TestDetails:

@XmlAccessorType(XmlAccessType.FIELD)
public class TestDetails{

public TestDetails() {}

@XmlElement(name="A", namespace="http://schemas.datacontract.org/XXX")
private String A;

@XmlElement(name="B", namespace="http://schemas.datacontract.org/XXX")
private String B;   

@XmlElement(name="TestDetails")
private List<TestDetails> TestDetails = new ArrayList<TestDetails>();

}

XML Structure:

    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
       <s:Body>
          <GetTestlistWithConnectionsResponse xmlns="http://tempuri.org/">
             <GetTestlistWithConnectionsResult xmlns:a="http://schemas.datacontract.org/XXX" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
                <a:Error i:nil="true"/>
                <a:TestDetails>
                   <a:TestDetails>
                      <a:A>A</a:A>
                      <a:B>B</a:B>
                   </a:TestDetails>
                </a:TestDetails>
            </GetTestlistWithConnectionsResult>
           </GetTestlistWithConnectionsResponse>
       </s:Body>
    </s:Envelope>

Unmarshall Method:

public Envelope unmarshallFromFile(){
    Envelope testDetail= null;
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(Envelope.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        InputStream inStream = null;
        try {
            inStream = new FileInputStream(this.fileLoc);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        flightDetailsSN = (Envelope) jaxbUnmarshaller.unmarshal( inStream );
    } catch (JAXBException e) {
        e.printStackTrace();
    }

    return testDetail;
}

When I invoke my unmarshall method I receive an object with a:TestDetails Item with an empty list. I was expecting that the list contains one element with values A and B.

Try out this, by changing sub element or child name if you have no problem. (I think its because of same name for header and sub element.)

<a:TestDetails>
    <a:TestDetail>
       <a:A>A</a:A>
       <a:B>B</a:B>
    </a:TestDetail>
</a:TestDetails>

A and B in your XML are elements, not attributes. Try changing

@XmlAttribute
private String A;
private String B;

to

@XmlElement(name = "A")
private String a;

@XmlElement(name = "B")
private String b;

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