Skip to content Skip to sidebar Skip to footer

Xml Parsing Java (android)

I have XML-request from web service. It looks like this. So, how can I get orderId value? I never work with XML in Java. &l

Solution 1:

I suggest to use XPath:

It will be something like:

XPathxpath= XPathFactory.newInstance().newXPath();
Stringexpression="orderId";
InputSourceinputSource=newInputSource(xmlInputStreamOrReaderOrSomethingElse). 
Stringid= (String) xpath.evaluate(expression, inputSource, XPathConstants.STRING);

Updated:

expression must be "//orderId": example

Solution 2:

If you want to parse it using the facilities built into the platform, you can use a SAX DefaultHandler. There are a bunch of answers here that point to tutorials.

If you just need the orderId value and none of the other values, then I would suggest just using a regular expression like:

<orderId>(\d+)<\/orderId>  

Post a Comment for "Xml Parsing Java (android)"