【问题标题】:JAXB Parent and Child node Same name. Child node returns null valuesJAXB 父子节点同名。子节点返回空值
【发布时间】:2019-06-04 06:06:36
【问题描述】:

我使用了一个 Web 服务,并且收到了一个带有同名父节点和子节点的 XML 响应。问题是最后一个层次结构没有值。

在我看来,JAXB 应该处理 List TestDetails。

类信封:

@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;

}

类体:

@XmlAccessorType(XmlAccessType.FIELD)
public class Body {

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

public Body() {}

}

类 GetTestlistWithConnectionsResponse:

@XmlAccessorType(XmlAccessType.FIELD)
public class GetTestlistWithConnectionsResponse {

public GetTestlistWithConnectionsResponse() {}

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


}

类 GetTestlistWithConnectionsResult:

@XmlAccessorType(XmlAccessType.FIELD)
public class GetTestlistWithConnectionsResult {

public GetTestlistWithConnectionsResult() {}

@XmlElement(name="TestDetails", namespace="http://schemas.datacontract.org/XXX")
private TestDetails 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 结构:

    <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>

解组方法:

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;
}

当我调用我的 unmarshall 方法时,我收到一个对象,该对象带有一个:TestDetails Item 和一个空列表。我原以为该列表包含一个具有值 A 和 B 的元素。

【问题讨论】:

    标签: java xml web-services jaxb


    【解决方案1】:

    XML 中的AB 是元素,而不是属性。尝试改变

    @XmlAttribute
    private String A;
    private String B;
    

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

    【讨论】:

    • 错误是什么?你能提供一个完整的工作例子吗?您的示例没有命名空间、@XmlRootElement 等,因此很难尝试修复。
    • 我没有收到任何错误消息。返回的对象有一个带有空数组列表的 TestDetails 元素。我本来希望该列表中有一个元素。现在我提供了一个完整的例子。
    • 我忘记在 TestDetails.java 中引用正确的命名空间。现在它起作用了!非常感谢!
    • @rwxrwxrwx 很好,不要忘记发布您更新的课程作为答案以帮助未来的访问者
    【解决方案2】:

    如果您没有问题,请通过更改子元素或子名称来试试这个。 (我认为这是因为标题和子元素的名称相同。)

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

    【讨论】:

    • 不幸的是,我无法更改 XML 元素。我必须使用这种结构。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多