简体   繁体   English

Android - 使用 XPath 解析 XML

[英]Android - Parsing XML with XPath

First of all, thanks to all the people who's going to spend a little time on this question.首先,感谢所有愿意花一点时间讨论这个问题的人。

Second, sorry for my english (not my first language! :D).其次,对不起我的英语(不是我的母语!:D)。

Well, here is my problem.嗯,这是我的问题。

I'm learning Android and I'm making an app which uses a XML file to store some info.我正在学习 Android 并且我正在制作一个使用 XML 文件来存储一些信息的应用程序。 I have no problem creating the file, but trying to read de XML tags with XPath (DOM, XMLPullParser, etc. only gave me problems) I've been able to read, at least, the first one.我在创建文件时没有问题,但是尝试使用 XPath(DOM、XMLPullParser 等只会给我带来问题)读取 XML 标记时,我至少能够读取第一个。

Let's see the code.让我们看看代码。

Here is the XML file the app generates:这是应用程序生成的 XML 文件:

<dispositivo>
    <id>111</id>
    <nombre>Name</nombre>
    <intervalo>300</intervalo>
</dispositivo>

And here is the function which reads the XML file:这是读取 XML 文件的函数:

private void leerXML() {
    try {
        XPathFactory  factory=XPathFactory.newInstance();
        XPath xPath=factory.newXPath();

        // Introducimos XML en memoria
        File xmlDocument = new File("/data/data/com.example.gps/files/devloc_cfg.xml");
        InputSource inputSource = new InputSource(new FileInputStream(xmlDocument));

        // Definimos expresiones para encontrar valor.
        XPathExpression  tag_id = xPath.compile("/dispositivo/id");
        String valor_id = tag_id.evaluate(inputSource);

        id=valor_id;

        XPathExpression  tag_nombre = xPath.compile("/dispositivo/nombre");
        String valor_nombre = tag_nombre.evaluate(inputSource);

        nombre=valor_nombre;
    } catch (Exception e) {
        e.printStackTrace();
    }
}

The app gets correctly the id value and shows it on the screen ("id" and "nombre" variables are assigned to a TextView each one), but the "nombre" is not working.该应用程序正确获取 id 值并将其显示在屏幕上(“id”和“nombre”变量分别分配给一个 TextView),但“nombre”不起作用。

What should I change?我应该改变什么? :) :)

Thanks for all your time and help.感谢您的所有时间和帮助。 This site is quite helpful!这个网站很有帮助!

PD: I've been searching for a response on the whole site but didn't found any. PD:我一直在整个网站上寻找回复,但没有找到。

You're using the same input stream twice, but the second time you use it it's already at the end of file.您两次使用相同的输入流,但第二次使用它时它已经在文件末尾。 You have to either open the stream once more or buffer it eg in a ByteArrayInputStream and reuse it.您必须再次打开流或缓冲它,例如在ByteArrayInputStream用它。

In your case doing this:在你的情况下这样做:

inputSource = new InputSource(new FileInputStream(xmlDocument));

before this line在这条线之前

XPathExpression  tag_nombre = xPath.compile("/dispositivo/nombre");

should help.应该有帮助。

Be aware though that you should properly close your streams.请注意,您应该正确关闭流。

The problem is that you cannot re-use the stream-input-source multiple times - the first call to tag_id.evaluate(inputSource) already has read the input up to the end.问题是您不能多次重复使用流输入源 - 对tag_id.evaluate(inputSource)的第一次调用已经读取输入到最后。

One solution would be to parse Document in advance:一种解决方案是提前解析 Document:

DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
Document document = documentBuilderFactory.newDocumentBuilder().parse(inputSource);

Source source = new DOMSource(document);

// evalute xpath-expressions on the dom source

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM