-2

This is my XML String :

<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>&lt;balanceResponse&gt;&lt;balance&gt;	&lt;accountNo&gt;800000123800&lt;/accountNo&gt;		&lt;accountCurrency&gt;IDR&lt;/accountCurrency&gt;	&lt;date&gt;20180917&lt;/date&gt;	&lt;ledgerBalance&gt;23842209566.02&lt;/ledgerBalance&gt;	&lt;availableBalance&gt;23257643542.65&lt;/availableBalance&gt;    	&lt;holdAmount&gt;584566023.37&lt;/holdAmount&gt;        	&lt;overdraftLimit&gt;0&lt;/overdraftLimit&gt;        	&lt;/balance&gt;&lt;/balanceResponse&gt;</ns2:txnData>
         </ns1:output>
      </HostCustomerResponse>
   </soapenv:Body>
</soapenv:Envelope>

How i can get the value of ns2:statusCode ?

2
  • You can use Sax, Stax or DOM parsers. Different parsers - different aproachs. Commented Sep 17, 2018 at 4:20
  • can you show to me how to use it ? Commented Sep 17, 2018 at 4:22

1 Answer 1

0

If you want to use Stax-parser, you have to do something like this:

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
                }
            }

        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.