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

如何在Java中获取节点中的元素文本?,java,Java,我是Android开发新手,希望解析XML并绑定到GoogleMapView。 但是,我无法读取元素之间的文本 XML <?xml version="1.0" encoding="utf8"?> <table name="clinicaddress"> <row> <GeoPoint>22.3852860,113.9664120</GeoPoint> </ro

我是Android开发新手,希望解析XML并绑定到GoogleMapView。 但是,我无法读取元素之间的文本

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>

22.3852860,113.9664120
22.336950,114.1578720
守则:

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());
        }
import javax.xml.parsers.DocumentBuilder;
导入javax.xml.parsers.DocumentBuilderFactory;
导入org.w3c.dom.CharacterData;
导入org.w3c.dom.Document;
导入org.w3c.dom.Node;
导入org.w3c.dom.NodeList;
导入org.xml.sax.InputSource;
试一试{
inputStream=assetManager.open(“clinics.xml”);
字符串xmlRecords=readTextFile(inputStream);
//Log.e(“诊所XML”,xmlRecords);
InputSource is=新的InputSource();
is.setCharacterStream(新的StringReader(xmlRecords));
DocumentBuilderFactory docBuilderFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder=docBuilderFactory.newDocumentBuilder();
Document doc=docBuilder.parse(is);
doc.getDocumentElement().normalize();
NodeList listOfClinics=doc.getElementsByTagName(“行”);
Log.e(“listOfClinics长度”,String.valueOf(listOfClinics.getLength());
//循环XML
对于(int x=0;x

getTextContent()方法返回所有嵌套标记的文本。

文本只是节点的子节点。
您需要遍历GeoPoint节点的所有子节点,检查节点类型是否为Node.TEXT\u Node,并连接文本。最后,我得到了这个结果

我使用
info.getFirstChild().getNodeValue()

for(int y=0;y
第3行很重要,我记录hasChildNodes()并在LogCat中观察,不知道为什么它包含一个名为“#text”的节点,并且没有childnodes(在LogCat中为false)

但我相信我的XML格式是正确的。 谷歌之后,发现了很多解释。


若要在异常上中断,您必须选择运行->添加Java异常断点。然后您可以选择在每个异常或特定异常上中断。我在软件中搜索,有人说设置“Throwable”检查捕获和未捕获,但结果仍然不像Visualstudio那样令人期待。您可能可以将此作为一个单独的问题发布,并附带测试用例和预期结果。如果捕获到Throwable,AFAIK Eclipse应该会在每个选中/未选中的异常上中断。但我找不到它,请查看我更新的屏幕截图。我在.NET world和n上停留了很长时间ew到Java。我在SOF中搜索,似乎我必须使用JDOM名称空间,但它在Android API上不存在。节点实现在哪个jar中?我的JDK 1.6在节点的实现者上有该方法。它可能取决于版本,我使用1.7 Java,但getTextContent方法不再可用
node.getTextContent()
        for (int y = 0; y < ClinicInfo.getLength(); y++) {
            Node info = ClinicInfo.item(y);
            if (info.hasChildNodes()) {
                Log.e(info.getNodeName(), info.getFirstChild().getNodeValue());
            }
        }