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:

XPath xpath = XPathFactory.newInstance().newXPath();
String expression = "orderId";
InputSource inputSource = new InputSource(xmlInputStreamOrReaderOrSomethingElse). 
String id = (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)"