0

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.

<order1s uri="http://mywebsite/rest/create_order">
<order1 uri="http://mywebsite/rest/create_order/401/"> 
<customer uri="http://mywebsite/rest/create_order/401/customer/">
<name/>
<passengerId>123</passengerId>
<phone/>
<rating>0.0</rating>
<restToken>
985y3289y5v43535v032m0v3v0b0jcrmjsa9w20m02umc3
</restToken>
<surname/>
</customer>
<offerCollection uri="http://mywebsite/rest/create_order/401/offerCollection/"/>
<orderId>401</orderId>
<orderWaypointsCollection uri="http://mywebsite/rest/create_order/401/orderWaypointsCollection/">
<orderWaypoints uri="http://mywebsite/rest/create_order/401/orderWaypointsCollection/null/">
<isStartPoint>true</isStartPoint>
<latitude>50.505463</latitude>
<longitude>30.44121</longitude>
<norder uri="http://mywebsite/rest/create_order/401/orderWaypointsCollection/null/norder/"/>
</orderWaypoints>
<orderWaypoints uri="http://mywebsite/rest/create_order/401/orderWaypointsCollection/null/">
<isStartPoint>false</isStartPoint>
<latitude>50.4501</latitude>
<longitude>30.5234</longitude>
<norder uri="http://mywebsite/rest/create_order/401/orderWaypointsCollection/null/norder/"/>
</orderWaypoints>
</orderWaypointsCollection>
<status>1</status>
<toDate>19/05/2011 17:49:12</toDate>
<toNow>true</toNow>
</order1>
</order1s>
1
  • I have edited your XML to remove the location of your website, and changed the security token. I was worried it might allow a hacker to submit malicious requests to your platform. Commented May 19, 2011 at 17:37

2 Answers 2

2

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

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

1 Comment

@Dima Svinarenko: yes, sorry. It should be String expression = "//orderId"; I check on this example.
0

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>  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.