在XML文件搜索与XPath的Android中 [英] Search in XML File with XPath in Android
本文介绍了在XML文件搜索与XPath的Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的深化发展的应用程序在Android上!
I'm developping an application on android!
嗯,我有一个小冲突,现在,我想执行一个XPath查询,但我没有到达来解决这个问题。
Well I have a little conflict now, I want to execute an XPath query but I didn't arrive to solve this problem.
该XML的filethat我用一个例子:
This an example of XML filethat I use:
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress.</description>
</book>
<book id="bk103">
<author>Corets, Eva</author>
<title>Maeve Ascendant</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-11-17</publish_date>
<description>After the collapse of a nanotechnology
society in England.</description>
</book>
</catalog>
我该怎么办??
How can I do??
在此先感谢!
推荐答案
请看下面的例子:
import java.io.FileReader;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
public class GuestList {
public static void main(String[] args) throws Exception {
XPathFactory factory = XPathFactory.newInstance();
XPath xPath = factory.newXPath();
NodeList shows = (NodeList) xPath.evaluate("/schedule/show", new InputSource(new FileReader(
"tds.xml")), XPathConstants.NODESET);
for (int i = 0; i < shows.getLength(); i++) {
Element show = (Element) shows.item(i);
String guestName = xPath.evaluate("guest/name", show);
String guestCredit = xPath.evaluate("guest/credit", show);
System.out.println(show.getAttribute("weekday") + ", " + show.getAttribute("date") + " - "
+ guestName + " (" + guestCredit + ")");
}
}
}
的例子其余的都是在这里: http://jexp.ru/index.php / Java_Tutorial / XML / XPath的
这篇关于在XML文件搜索与XPath的Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文