如何在Java中获取节点中的元素文本? [英] How can i get element text in node in Java?

查看:184
本文介绍了如何在Java中获取节点中的元素文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android Development的新手,想要解析XML并绑定到Google MapView。
但是,我无法读取元素之间的文本。

I am new to Android Development,and want to parse the XML and bind to Google MapView. However, i cannot read the Text between element.

XML

    <?xml version="1.0" encoding="utf8"?>
    <table name="clinicaddress">
        <row>
            <GeoPoint>22.3852860,113.9664120</GeoPoint>
        </row>
        <row>
            <GeoPoint>22.336950,114.1578720</GeoPoint>
        </row>
    </table>

代码:

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

try {
            inputStream = assetManager.open("clinics.xml");
            String xmlRecords = readTextFile(inputStream);
            //Log.e("Clinics XML", xmlRecords);

            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xmlRecords));

            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(is);

            doc.getDocumentElement ().normalize ();

            NodeList listOfClinics = doc.getElementsByTagName("row");
            Log.e("listOfClinics Length" , String.valueOf(listOfClinics.getLength()));

            //Loop the XML

            for (int x = 0; x < listOfClinics.getLength(); x++) {
                Node ClinicNode = listOfClinics.item(x);
                NodeList ClinicInfo = ClinicNode.getChildNodes();
                for (int y = 0; y < ClinicInfo.getLength(); y++) {
                    Node info = ClinicInfo.item(y);
                    Log.e(info.getNodeName() , info.getNodeValue()); //<--Error on info.getNodeValue()
                }
}
            //End Loop XML
        } catch (Exception e) {
            Log.e("tag", e.getMessage());
        }

那么,getNodeValue()???

So, what wrong with getNodeValue()???

还有一个问题,我怎么能像Eclipse一样设置Eclipse,
如果有任何错误,它会在源代码行文件中断行,而不是在调试窗口上打印出栈消息。

And one more question, how can i set the Eclipse like Visual Studio, If any error, it will break to the line on source line file, instead of just print out the stack message on debug windows.

更新1。
我发现此帖子:
org.w3c.dom.Node,Android版本较少超过2.2
org.w3c。 Android版本小于2.2的dom.Node


node.getTextContext()与
节点相同。 getFirstChild()。getNodeValue()。

node.getTextContext() is the same as node.getFirstChild().getNodeValue().

但是,我试试这个,也是错误。

However, i try this, it is goes error too.

推荐答案

最后,我得到了这个。

我用 info.getFirstChild()。getNodeValue()

        for (int y = 0; y < ClinicInfo.getLength(); y++) {
            Node info = ClinicInfo.item(y);
            if (info.hasChildNodes()) {
                Log.e(info.getNodeName(), info.getFirstChild().getNodeValue());
            }
        }

第3行很重要,我记录了hasChildNodes()并在LogCat中观看,不知道为什么它包含一个名为#text的节点而没有子节点(在logcat上为false)。

Line 3 is important, i Log the hasChildNodes() and watch in LogCat, don't know why it contains a node named "#text" and no childnodes (which is false on logcat).

但我相信我的XML是正确的格式。
Google之后,发现了很多解释。
https://www.google.com/search?q=xml+ %23text

But i believe my XML is correct format. After Google, found many explanation. https://www.google.com/search?q=xml+%23text

这篇关于如何在Java中获取节点中的元素文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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