Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
240 views
in Technique[技术] by (71.8m points)

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.

<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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...