加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

如何在Java中获取节点中的元素文本?

发布时间:2020-12-15 02:02:00 所属栏目:Java 来源:网络整理
导读:我是 Android Development的新手,想要解析XML并绑定到Google MapView. 但是,我无法读取元素之间的文本. XML ?xml version="1.0" encoding="utf8"? table name="clinicaddress" row GeoPoint22.3852860,113.9664120/GeoPoint /row row GeoPoint22.336950,114.
我是 Android Development的新手,想要解析XML并绑定到Google MapView.
但是,我无法读取元素之间的文本.

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()有什么问题?

还有一个问题,我如何将Eclipse设置为Visual Studio,
如果有任何错误,它将中断到源行文件中的行,而不是仅在调试窗口上打印出堆栈消息.

更新1.
我发现这篇文章:
Android版本小于2.2的org.w3c.dom.Node
org.w3c.dom.Node with Android version less than 2.2

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

但是,我试试这个,也是错误的.

解决方法

最后,我得到了这个作品.

我使用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).

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读