【问题标题】:Android XML parsing in JavaJava中的Android XML解析
【发布时间】:2010-08-18 18:10:14
【问题描述】:

我的 XML 看起来像:

<key>A</key>

如何在 Eclipse 中使用 java 从 XML 中提取字符串“A”? 我的 Java 代码如下所示:

    XmlResourceParser xmlSymptoms = getResources().getXml(R.xml.symptoms);
    while (eventType != XmlResourceParser.END_DOCUMENT) {
        if (eventType == XmlResourceParser.START_TAG) {
            // Get the name of the tag (eg scores or score)
            String strName = xmlSymptoms.getName();
            if (strName.equals("key")) {
                 keyName = xmlSymptoms.getAttributeName(0);
                 keyA = xmlSymptoms.getAttributeValue(0);
            }
         }
         eventType = xmlSymptoms.next();
    }

但我认为 AttributeName 和 AttributeValue 适用于作为关键描述一部分的项目,即它们将用于获取“项目”和“第一”。

<key item="first">A</key>

我用什么来获取之间的字符串 “&lt;key&gt;” 和 “&lt;/key&gt;”?

【问题讨论】:

    标签: android xml parsing


    【解决方案1】:

    除了 START_TAG 事件之外,您还需要注意 XMLResourceParser.TEXT 事件。 START_TAG 仅与 &lt;key item="first"&gt; 有关。 TEXT 关注A。 END_TAG &lt;/key&gt;

    收到 TEXT 事件后,请致电 xmlSymptoms.getText()。这将在您的示例中返回“A”。

    【讨论】:

      猜你喜欢
      • 2020-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-04
      • 1970-01-01
      • 2013-01-22
      相关资源
      最近更新 更多