【问题标题】:With SAX Parser, get an attribute's value使用 SAX Parser,获取属性的值
【发布时间】:2012-05-01 19:44:29
【问题描述】:

我正在使用 Android 从 Web 解析 XML。下面的代码显示了 XML 的示例。我遇到的问题是我无法获取项目标签的字符串值。当我使用name = attributes.getQName(i); 时,它会输出名称,而不是属性的值。

<weatherdata>
 <timetags>
  <item name="date">
   <value>20/04/2012</value>
   <unit/>
   <image/>
   <class>dynamic</class>
   <description>The current date</description>
  </item>

【问题讨论】:

    标签: java android xml saxparser xml-attribute


    【解决方案1】:

    使用

    attributes.getValue(i);
    

    而不是

    attributes.getQName(i);
    

    因为正如doc 所说:

    getQName返回一个属性的限定(前缀)名称。

    getValue通过限定(前缀)名称查找属性值。

    查看this 获取属性名称和值的示例

    【讨论】:

      【解决方案2】:
       @Override
      public void startElement(String uri, String localName, String qName,
              Attributes attributes) throws SAXException {
           if(localName.equalsIgnoreCase("item")){
              //currentMessage.setMediaUrl(attributes.getValue(BaseFeedParser.Url));
                           String valueis=attributes.getValue("name")
          }
          super.startElement(uri, localName, qName, attributes);
      }
      

      【讨论】:

        【解决方案3】:

        试试attributes.getValue(i)方法

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-03-10
          • 2020-03-08
          • 1970-01-01
          • 2011-12-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多