【问题标题】:Android Sax Xml Parser attributesAndroid Sax Xml 解析器属性
【发布时间】:2013-04-02 07:01:49
【问题描述】:

我正在使用 SAX 解析 xml 文件,但无法获取“y”的值

这里是xml,

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type= "text/html" ?>
<Chart1 count="3">
            <Sales_x0020_Amount Label="Sales Amount">
               <Chart1_CategoryGroup_Collection>
                  <Chart1_CategoryGroup Label="URC 1">
                     <Value Y="30434929.1" />
                     <name>keeevin</name>
                  </Chart1_CategoryGroup>
                  <Chart1_CategoryGroup Label="URC 2">
                     <Value Y="39757503.83" />
                     <name>kevin2</name>
                  </Chart1_CategoryGroup>
                  <Chart1_CategoryGroup Label="URC 3">
                     <Value Y="19611069.73" />
                     <name>kevin</name>
                  </Chart1_CategoryGroup>
               </Chart1_CategoryGroup_Collection>
            </Sales_x0020_Amount>
</Chart1>

我可以得到属性“name”的值,但不能得到“y”的值。

获取值的部分代码..

Element e = (Element)nodes.item(i);
map.put("Name", "Name:" + XMLfunctions.getValue(e, "name"));
map.put("Y", "Y Coord:" + XMLfunctions.getValue(ee, "y"));

谢谢。

编辑: 我这里设置了断点,这里不会通过。

static class MyHandler extends DefaultHandler {
    public void startElement(String namespaceURI, String localName,
                             String qName, Attributes atts)  {
        // Get the number of attribute
        int length = atts.getLength();

    // Process each attribute
    for (int i=0; i<length; i++) {
        // Get names and values for each attribute
        String name = atts.getQName(i);
        String value = atts.getValue(i);
        if(qName == "Value"){
             y = atts.getValue(i);
        }
        String nsUri = atts.getURI(i);
        String lName = atts.getLocalName(i);
    }
}

}

用这个解决了:

    public void startElement(String uri, String localName, String qName,
                Attributes attributes) throws SAXException {

            elementOn = true;

    if (localName.equals("Chart"))
            {
                data = new XMLGettersSetters();
            }
            if (qName.equals("Value")) {
                String attributeValue = attributes.getValue(0);
                data.setValue(attributeValue);
            }
}

【问题讨论】:

    标签: android xml-parsing saxparser


    【解决方案1】:

    您的起始元素将是图表,在该计数中将是属性,它将出现在 if 部分中,而不是在 else if 部分中,Sales_x0020_Amount 将出现,并且该属性将是标签。参考这个tutorial

    【讨论】:

      【解决方案2】:

      在你的 Handler(extended DefaultHandler) 的 startElement 方法中写

      public void startElement(String uri, String localName, String qName,
              Attributes attributes) throws SAXException {
          if (qName.equals("Value")) {
                       map.put("Y", "Y Coord:" + attributes.getValue(0));
                  }
          }
      

      【讨论】:

      • 可以解释一下参数吗?
      • qName -tag 名称(在这一行 qName=Value)属性 - 属性数组,元素从索引 0 开始(在这一行 本次调用attributes.getValue(0)返回(Y属性值)39757503.83)
      • 但是其他参数呢?我应该传递什么值?
      • 谢谢。我还发现了这个example。如果我要加载托管的 xml,而不是本地的,你将如何实现这一点?
      猜你喜欢
      • 1970-01-01
      • 2011-04-30
      • 2013-03-16
      • 2011-04-16
      • 2013-12-05
      • 1970-01-01
      • 2012-12-26
      • 2011-09-14
      • 2012-12-19
      相关资源
      最近更新 更多