2012-07-10 66 views
1

我是Android开发新手,想要解析XML并绑定到Google MapView。 但是,我无法读取元素之间的文本。如何在Java节点中获取元素文本?

的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()???

enter image description here

还有一个问题,我怎么能设置在Eclipse像Visual Studio, 如果有任何错误,它会破坏对源代码行文件中的行,而不是只打印出调试栈消息视窗。

1.更新 我发现这个职位: org.w3c.dom.Node中与Android版本小于2.2 org.w3c.dom.Node with Android version less than 2.2

node.getTextContext()是相同的 node.getFirstChild ().getNodeValue()。

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

+0

要打破你必须选择运行 - >添加Java异常断点例外。然后,您可以选择打破每个例外或特定的例外。 – navlelo 2012-07-10 13:50:19

+0

我在SOF中搜索,有人说设置了“Throwable”并检查了被捕获和未被捕获,但结果仍然不像Visualstudio那样期望。 – Cheung 2012-07-10 13:57:01

+0

您也许可以将此作为单独的问题与测试用例和预期结果一起发布。 AFAIK如果您捕获Throwable,则Eclipse应该在每个已检查/未检查的异常中断开。 – navlelo 2012-07-10 14:24:18

回答

11

最后,我得到了这个工程。

我用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”,没有的childNodes(这是false在logcat上)。

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

enter image description here

8

使用getTextContent()方法。

node.getTextContent() 

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

+0

但我找不到它,请参阅我更新的截图。我很长一段时间在.NET世界和Java新手。我在SOF中搜索,似乎我必须使用JDOM命名空间,但它不存在于Android API上。 – Cheung 2012-07-10 14:06:50

+0

Node中的实现是什么jar?我的JDK 1.6在Node的实现者上有这种方法。 – 2012-07-10 15:10:23

+0

它可能依赖于一个版本,我使用1.7的Java但getTextContent方法不可用了 – simonC 2015-12-14 13:10:08

1

文本只是节点的子节点。 您需要遍历GeoPoint节点的所有子节点,检查节点类型为Node.TEXT_NODE并连接文本