-1

我有以下 XML,我需要在 Android 应用程序中解析它。我已经构建了一个运行良好的解析器,但是对于像这样的 XML 格式http://dl.dropbox.com/u/6751304/Nicosia.xml

现在我必须解析这样的东西http://www.cytanet.com.cy/Services/connectivity/basic/wireless/GoogleMaps/wifimarkers.xml

我以前使用的解析器不显示任何结果。我得到一个空白的 loooooong 列表,所以我猜它读取了标记标签。我也知道 xml 已被解析,因为我可以在 logcat 中记录它,但未读取元素

有人可以识别第二个 XML 文件的格式并指向有关如何解析它的教程/信息吗?

(如果需要,我可以发布当前代码)

4

2 回答 2

2

由于您的 xml 文件较大,因此使用 sax 解析器更好。对于从 sax 解析器使用,您应该使用 DeafaultHandler 并且在其 startElement 方法中您应该编写以下相同的内容:

public void startElement(String arg0, String localName, String arg2,
        Attributes attributes) throws SAXException {
    currentElement = true;

if (localName.equals("marker")) {

        String lat = attributes.getValue("lat");

        String  lng = attributes.getValue("lng");

        String city= attributes.getValue("city");

      //and ...

    }
}

   }
于 2012-12-22T17:32:58.497 回答
1

我通常使用 SAX 解析器,但您正在寻找不同的属性和不同的标签。

看起来这篇文章应该让你开始(涵盖萨克斯解析) http://xjaphx.wordpress.com/2011/10/09/android-xml-adventure-parsing-data-with-saxparser/

基本上在您的 StartElement 中,您将获取属性 atts.getValue(MyAttribute.ID)

拥有两者的一个原因是这样的:并添加到这个.. 拥有两者的一个原因是解析这样的东西:你想给一个元素一个描述它的属性。

<Contact>
    <Name>John Smith</Name>
    <PhoneNumber primary="true">
          <Number>214-555-1235</Number>
          <Type>Work</Type>
    </PhoneNumber>
    <PhoneNumber>
          <Number>214-555-1111</Number>
          <Type>Home</Type>
    </PhoneNumber>
</Contact>
于 2012-12-22T17:34:56.450 回答