我正在尝试使用 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
,它显示为 nullresults
out:
谁能帮我摆脱困境?
更新:任何人都可以帮助我吗?
我使用 stax xml 解析器来解析,results
但我看到它中的所有值都为空。