我正在尝试使用jaxb解组soap响应。
我尝试使用以下代码来查找根元素
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(false);
DocumentBuilder db;
try {
db = dbf.newDocumentBuilder();
Document d = db.parse(new File("response.xml"));
Unmarshaller unmarshaller = null;
Results results = null;
unmarshaller = JAXBContext.newInstance(Results.class).createUnmarshaller();
Node getNumberResponseElt = d.getElementsByTagName("results").item(0);
JAXBElement<Results> rs = unmarshaller.unmarshal(new DOMSource(getNumberResponseElt),Results.class);
results = rs.getValue();
}
以下是我的样本回复
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header/>
<soapenv:Body>
<service:ServiceResponse xmlns:out="http://example.com/Person/PersonData/v1" xmlns:service="http://example.com/Person/PersonData/v1/" xmlns:xs4xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<results>
<out:result>
<out:person>
<out:personName>
<out:firstName>First</out:firstName>
<out:lastName>Last</out:lastName>
</out:personName>
<out:gender>M</out:gender>
</out:person></out:result></results></service:ServiceResponse></soapenv:Body></soapenv:Envelope>
我能够获取results
对象,但当我尝试访问result
中的results
时,由于前缀out:
任何人都可以帮我摆脱这个吗?
更新: 任何人都可以帮我吗?
我使用stax xml解析器解析results
,但我看到它中的所有值都为null。
答案 0 :(得分:1)
我认为问题在于命名空间位置。您可以使用StAX XMLStreamReader来解析XML并前进到正确的元素,然后在此时从XMLStreamReader解组
http://blog.bdoughan.com/2012/08/handle-middle-of-xml-document-with-jaxb.html