我尝试通过标准javax库解组肥皂响应。 响应的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解组,但是unmarsheller返回空结果
test.Test.main(Test.java:39)处的线程“ main”中的异常java.lang.NullPointerException
答案 0 :(得分:0)
编写正确的注释会有所帮助
@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前缀和名称空间