这个问题在这里已有答案:
这是我的XML字符串:
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<HostCustomerResponse xmlns="http://10.25.143.133">
<ns1:output xmlns:ns1="http://10.25.136.152"
xmlns:ns2="java:prismagateway.service.HostCustomer" xsi:type="ns2:Output">
<ns2:bankReffNo/>
<ns2:statusCode>00</ns2:statusCode>
<ns2:statusMsg/>
<ns2:txnResponseDateTime>20180917110403</ns2:txnResponseDateTime>
<ns2:txnData><balanceResponse><balance> <accountNo>800000123800</accountNo> <accountCurrency>IDR</accountCurrency> <date>20180917</date> <ledgerBalance>23842209566.02</ledgerBalance> <availableBalance>23257643542.65</availableBalance> <holdAmount>584566023.37</holdAmount> <overdraftLimit>0</overdraftLimit> </balance></balanceResponse></ns2:txnData>
</ns1:output>
</HostCustomerResponse>
</soapenv:Body>
</soapenv:Envelope>
我如何获得ns2:statusCode的值?
如果你想使用Stax-parser,你必须做这样的事情:
public static void main(String[] args) throws XMLStreamException {
XMLEventReader xmlEventReader = XMLInputFactory.newInstance().createXMLEventReader(
Test.class.getResourceAsStream("test.xml")
);
while(xmlEventReader.hasNext()) {
XMLEvent xmlEvent = xmlEventReader.nextEvent();
if(xmlEvent.isStartElement()){
if(Objects.equals(xmlEvent.asStartElement().getName().getLocalPart(),"statusCode")) {
xmlEvent = xmlEventReader.nextEvent();
if (xmlEvent.isCharacters()) {
System.out.println(xmlEvent.asCharacters().getData()); // 00
}
}
}
}
}