gpt4 book ai didi

java - 无法从 xml 解码元素列表

转载 作者:行者123 更新时间:2023-12-02 04:10:51 25 4
gpt4 key购买 nike

我尝试通过标准 javax 库解码 SOAP 响应。响应的xml是这样的

    <?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<ActivityId xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics" CorrelationId="465733a9-9fb7-4287-89c5-5250d5697194">00000000-0000-0000-0000-000000000000</ActivityId>
</s:Header>
<s:Body>
<BResponse xmlns="http://tempuri.org/">
<Result xmlns:a="http://someurl.ru/">
<a:Element>
<a:ID>11488477</a:ID>
<a:Name>Object 1</a:Name>
</a:Element>
<a:Element>
<a:ID>11488453</a:ID>
<a:Name>Object 2</a:Name>
</a:Element>
</Result>
</BResponse>
</s:Body>
</s:Envelope>

我的代码可以在那里访问 http://tpcg.io/JXuwHB我做错了什么?

    package test;

import java.io.ByteArrayInputStream;
import java.util.List;

import javax.xml.bind.*;
import javax.xml.bind.annotation.*;
import javax.xml.soap.*;

@XmlRootElement ( name = "Element" )
class Element {
@Override
public String toString() {
return "Element [id=" + id + ", name=" + name + "]";
}

@XmlElement ( name = "ID" )
public int id;

@XmlElement ( name = "Name" )
public String name;
}

@XmlRootElement ( name = "BResponse", namespace = "http://tempuri.org/" )
class BResponse {

@XmlElementWrapper ( name = "Result", namespace = "http://someurl.ru/" )
@XmlElement ( name = "Element", type = Element.class )
public List<Element> result;

}

public class Test {

public static void main(String[] args) throws Exception {
System.out.println(res);
SOAPMessage message = MessageFactory.newInstance().createMessage(new MimeHeaders(),new ByteArrayInputStream(res.getBytes("utf-8")));
Unmarshaller unmarshaller = JAXBContext.newInstance(BResponse.class).createUnmarshaller();
BResponse bResponse = (BResponse)unmarshaller.unmarshal(message.getSOAPBody().extractContentAsDocument());
for ( Element element : bResponse.result )
System.out.println(element);
}

public static String res =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" +
"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">\r\n" +
" <s:Header>\r\n" +
" <ActivityId xmlns=\"http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics\" CorrelationId=\"465733a9-9fb7-4287-89c5-5250d5697194\">00000000-0000-0000-0000-000000000000</ActivityId>\r\n" +
" </s:Header>\r\n" +
" <s:Body>\r\n" +
" <BResponse xmlns=\"http://tempuri.org/\">\r\n" +
" <Result xmlns:a=\"http://someurl.ru/\">\r\n" +
" <a:Element>\r\n" +
" <a:ID>11488477</a:ID>\r\n" +
" <a:Name>Object 1</a:Name>\r\n" +
" </a:Element>\r\n" +
" <a:Element>\r\n" +
" <a:ID>11488453</a:ID>\r\n" +
" <a:Name>Object 2</a:Name>\r\n" +
" </a:Element>\r\n" +
" </Result>\r\n" +
" </BResponse>\r\n" +
" </s:Body>\r\n" +
"</s:Envelope>";

}

我像标准示例中一样注释了所有元素,并尝试通过 jaxb 对其进行解码,但解码器返回 null 结果

Exception in thread "main" java.lang.NullPointerException at test.Test.main(Test.java:39)

最佳答案

编写正确的注释有帮助

    @XmlRootElement( name = "Element", namespace = "http://someurl.ru/" )
class Element {
@Override
public String toString() {
return "Element [id=" + id + ", name=" + name + "]";
}

@XmlElement ( name = "ID", namespace = "http://someurl.ru/" )
public int id;

@XmlElement ( name = "Name", namespace = "http://someurl.ru/" )
public String name;
}


@XmlRootElement ( name = "BResponse", namespace = "http://tempuri.org/" )
class BResponse {

@XmlElementWrapper ( name = "Result", namespace = "http://tempuri.org/" )
@XmlElement ( name = "Element" , type = Element.class, namespace = "http://someurl.ru/" )
public List<Element> result;

}

并且需要创建包含内容的 package-info.java

    @javax.xml.bind.annotation.XmlSchema (
xmlns = {
@javax.xml.bind.annotation.XmlNs ( prefix = "", namespaceURI = "http://tempuri.org/" ),
@javax.xml.bind.annotation.XmlNs ( prefix = "a", namespaceURI = "http://someurl.ru/" )
}
)
package test;

其中描述了 xml 前缀和命名空间

关于java - 无法从 xml 解码元素列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56688205/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com