XML 解析 Java (Android)

发布于 2024-11-08 04:57:27 字数 1626 浏览 0 评论 0原文

我有来自 Web 服务的 XML 请求。看起来像这样。那么,如何获取orderId值呢?我从来没有在 Java 中使用过 XML。

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

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆伤 2024-11-15 04:57:27

我建议使用 XPath

它将类似于:

XPath xpath = XPathFactory.newInstance().newXPath();
String expression = "orderId";
InputSource inputSource = new InputSource(xmlInputStreamOrReaderOrSomethingElse). 
String id = (String) xpath.evaluate(expression, inputSource, XPathConstants.STRING);

更新:

表达式必须为 "//orderId"示例

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

真心难拥有 2024-11-15 04:57:27

如果您想使用平台内置的工具来解析它,您可以使用 SAX DefaultHandler。这里有很多指向教程的答案。

如果您只需要 orderId 值而不需要任何其他值,那么我建议仅使用正则表达式,例如:

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

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>  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文