在字符串中的Andr​​oid(Java)使用XPath [英] Using XPath on String in Android (JAVA)

查看:113
本文介绍了在字符串中的Andr​​oid(Java)使用XPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找在Android中使用XPath的一些例子吗?或者,如果任何人都可以分享他们的经验。我一直在struggeling使尾部这个问题还是头: - (

I am looking for some examples of using xpath in Android? Or if anyone can share their experiences. I have been struggeling to make tail or head of this problem :-(

我有一个包含一个标准的XML文件的字符串。我相信,我需要将其转换成XML文档。我发现这个code,我认为会做的伎俩:

I have a string that contains a standard xml file. I believe I need to convert that into an xml document. I have found this code which I think will do the trick:

public static Document stringToDom(String xmlSource) 
throws SAXException, ParserConfigurationException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(new InputSource(new StringReader(xmlSource)));
}

下一步 假设code以上就OK了,我需要申请的XPath来从猫值:/动物/哺乳动物/猫/猫

Next steps Assuming the code above is OK, I need to apply xpath to get values from cat: "/animal/mammal/feline/cat"

我看开发文档浏览:的http://开发商。 android.com/reference/javax/xml/xpath/XPath.html 也是在网上看,但我不知道从哪里开始!

I look at the dev doc here: http://developer.android.com/reference/javax/xml/xpath/XPath.html and also look online, but I am not sure where to start!

我曾尝试使用下面的code:

I have tried to use the following code:

 XPathFactory xPathFactory = XPathFactory.newInstance();
// To get an instance of the XPathFactory object itself.

XPath xPath = xPathFactory.newXPath();
// Create an instance of XPath from the factory class.

String expression = "SomeXPathExpression";
XPathExpression xPathExpression = xPath.compile(expression);
// Compile the expression to get a XPathExpression object.

Object result = xPathExpression.evaluate(xmlDocument);
// Evaluate the expression against the XML Document to get the result.

但我得到解决不了。 Eclipse中似乎并没有能够解决这个问题的进口。我试图手动输入:

But I get "Cannot be resolved". Eclipse doesn't seem to be able to fix this import. I tried manually entering:

javax.xml.xpath.XPath

但这并没有工作。

But this did not work.

有谁知道任何良好来源$ C ​​$ C,我可以利用,为Android平台? 1.5

Does anyone know any good source code that I can utilise, for Android platform? 1.5

推荐答案

您使用的是什么版本的SDK?的XPath中引入了SDK 8(2.2)。如果你没有建立针对该版本,则该类不存在。

What version of the SDK are you using? XPath was introduced in SDK 8(2.2). If you aren't building against that version then the class doesn't exist.

这篇关于在字符串中的Andr​​oid(Java)使用XPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆