我正在尝试使用 jaxb 解组肥皂响应。
我尝试使用以下代码来查找根元素
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
in results
由于前缀,它显示为 nullout:
谁能帮我摆脱这个困境?
UPDATE:
谁可以帮我这个事?
我使用 stax xml 解析器来解析results
但我看到其中的所有值都为空。
我认为问题在于名称空间位置。您可以使用 StAX XMLStreamReader 解析 XML 并前进到正确的元素,然后在此时从 XMLStreamReader 解组
http://blog.bdoughan.com/2012/08/handle-middle-of-xml-document-with-jaxb.html http://blog.bdoughan.com/2012/08/handle-middle-of-xml-document-with-jaxb.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)