Android DOMParser getElementsByTagName(" *")

时间:2014-03-13 14:10:43

标签: android xml getelementsbytagname domparser

我在使用getElementsByTagName(“*”)

时遇到问题

Documentaion说

  

-getElementsByTagName NodeList getElementsByTagName(String name)

     

返回具有给定标记名称的所有后代元素的NodeList,   按文件顺序。

     

参数:name - 要匹配的标记的名称。特殊值“*”匹配所有标签。返回:匹配的Element节点列表。

虽然它只返回xml中的FIRST元素节点,如:

<ui>
<entry>
<img>
<icon>...</icon>
<action>image</action>
<data/>
<xpos>2</xpos>
<ypos>47</ypos>
</img>
<btn>
<icon>
http://epic-demo.com/testxml/images/200214050213call.png
</icon>
<action>call</action>
<data>19019</data>
<xpos>128</xpos>
<ypos>61</ypos>
</btn>
<btn>
<icon>
http://epic-demo.com/testxml/images/200214050237map.png
</icon>
<action>url</action>
<data>http://goo.gl/SPBvt</data>
<xpos>236</xpos>
<ypos>165</ypos>
</btn>
<btn>
<icon>
http://epic-demo.com/testxml/images/200214050221video.png
</icon>
<action>video</action>
<data>tMVE2MaLe8I</data>
<xpos>14</xpos>
<ypos>173</ypos>
</btn>
</entry>
</ui>

为什么不返回所有元素?!

分析器

public class XMLDOMParser {
    //Returns the entire XML document
    public Document getDocument(InputStream inputStream) {
        Document document = null;
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder db = factory.newDocumentBuilder();
            InputSource inputSource = new InputSource(inputStream);
            document = db.parse(inputSource);
        } catch (ParserConfigurationException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        } catch (SAXException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        } catch (IOException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        }
        return document;
    }


    public String getValue(Element item, String name) {
        NodeList nodes = item.getElementsByTagName(name);
        return this.getTextNodeValue(nodes.item(0));
    }

    private final String getTextNodeValue(Node node) {
        Node child;
        if (node != null) {
            if (node.hasChildNodes()) {
                child = node.getFirstChild();
                while(child != null) {
                    if (child.getNodeType() == Node.TEXT_NODE) {
                        return child.getNodeValue();
                    }
                    child = child.getNextSibling();
                }
            }
        }
        return "";
    }
}

代码

private ArrayList<UIElement> loadXmlFromNetwork(String urlString)
            throws XmlPullParserException, IOException {

        InputStream stream = null;

        XMLDOMParser parser = new XMLDOMParser();
        stream = downloadUrl(urlString);
        Document doc = parser.getDocument(stream);

        ArrayList<UIElement> UI_array = new ArrayList<UIElement>();

        // Get elements by name btn
        NodeList btns_entries = doc.getElementsByTagName("*");

        for (int j = 0; j < btns_entries.getLength(); j++) {

            Element e = (Element) btns_entries.item(j);

            UIElement btn = new UIElement();

            btn.setIcon(parser.getValue(e, NODE_ICON));
            btn.setAction(parser.getValue(e, NODE_ACTION));
            btn.setData(parser.getValue(e, NODE_DATA));
            btn.setXpos(parser.getValue(e, NODE_XPOS));
            btn.setYpos(parser.getValue(e, NODE_YPOS));

            UI_array.add(btn);          

        }

        return UI_array;

    }

1 个答案:

答案 0 :(得分:2)

我假设它只返回<ui>元素?如果是这样 - 那是预期的。文档的根只包含一个节点 - <ui>。但是<ui>元素有很多孩子。 getElementsByTagName()不是递归的。因此,如果您首先尝试获取<ui>节点,然后将所有节点从此节点中移出 - 您将得到您期望的结果。

修改

我还注意到你有另一个顶级元素 - entry。您可能还需要获取该元素,然后获取entry

中的所有子节点

<强> EDIT2:

我尝试使用你的代码和你的xml,它对我有用 - 我得到了XML中的所有节点。您的XML是否有可能不是您期望的那样?

private void parse()
{
   String xml =  "<ui>\n" +
            "  <entry>\n" +
            "    <img>\n" +
            "      <icon>...</icon>\n" +
            "      <action>image</action>\n" +
            "      <data />\n" +
            "      <xpos>2</xpos>\n" +
            "      <ypos>47</ypos>\n" +
            "    </img>\n" +
            "    <btn>\n" +
            "      <icon>http://epic-demo.com/testxml/images/200214050213call.png</icon>\n" +
            "      <action>call</action>\n" +
            "      <data>19019</data>\n" +
            "      <xpos>128</xpos>\n" +
            "      <ypos>61</ypos>\n" +
            "    </btn>\n" +
            "    <btn>\n" +
            "      <icon>http://epic-demo.com/testxml/images/200214050237map.png</icon>\n" +
            "      <action>url</action>\n" +
            "      <data>http://goo.gl/SPBvt</data>\n" +
            "      <xpos>236</xpos>\n" +
            "      <ypos>165</ypos>\n" +
            "    </btn>\n" +
            "    <btn>\n" +
            "      <icon>http://epic-demo.com/testxml/images/200214050221video.png</icon>\n" +
            "      <action>video</action>\n" +
            "      <data>tMVE2MaLe8I</data>\n" +
            "      <xpos>14</xpos>\n" +
            "      <ypos>173</ypos>\n" +
            "    </btn>\n" +
            "  </entry>\n" +
            "</ui>\n";



    Document doc = getDocument(xml);

    // Get elements by name btn
    NodeList btns_entries = doc.getElementsByTagName("*");

    for (int j = 0; j < btns_entries.getLength(); j++) {

        Element e = (Element) btns_entries.item(j);
        Log.d("log", e.getTagName());
    }
}

public Document getDocument(String xml) {
    Document document = null;
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    try {
        DocumentBuilder db = factory.newDocumentBuilder();
        InputSource inputSource = new InputSource(new ByteArrayInputStream(xml.getBytes("UTF-8")));
        document = db.parse(inputSource);
    } catch (ParserConfigurationException e) {
        Log.e("Error: ", e.getMessage());
        return null;
    } catch (SAXException e) {
        Log.e("Error: ", e.getMessage());
        return null;
    } catch (IOException e) {
        Log.e("Error: ", e.getMessage());
        return null;
    }
    return document;
}

<强>输出:

03-13 15:13:19.528    1540-1540/? D/log﹕ ui
03-13 15:13:19.528    1540-1540/? D/log﹕ entry
03-13 15:13:19.528    1540-1540/? D/log﹕ img
03-13 15:13:19.528    1540-1540/? D/log﹕ icon
03-13 15:13:19.528    1540-1540/? D/log﹕ action
03-13 15:13:19.528    1540-1540/? D/log﹕ data
03-13 15:13:19.528    1540-1540/? D/log﹕ xpos
03-13 15:13:19.528    1540-1540/? D/log﹕ ypos
03-13 15:13:19.528    1540-1540/? D/log﹕ btn
03-13 15:13:19.528    1540-1540/? D/log﹕ icon
03-13 15:13:19.528    1540-1540/? D/log﹕ action
03-13 15:13:19.528    1540-1540/? D/log﹕ data
03-13 15:13:19.528    1540-1540/? D/log﹕ xpos
03-13 15:13:19.528    1540-1540/? D/log﹕ ypos
03-13 15:13:19.528    1540-1540/? D/log﹕ btn
03-13 15:13:19.528    1540-1540/? D/log﹕ icon
03-13 15:13:19.528    1540-1540/? D/log﹕ action
03-13 15:13:19.528    1540-1540/? D/log﹕ data
03-13 15:13:19.528    1540-1540/? D/log﹕ xpos
03-13 15:13:19.528    1540-1540/? D/log﹕ ypos
03-13 15:13:19.528    1540-1540/? D/log﹕ btn
03-13 15:13:19.528    1540-1540/? D/log﹕ icon
03-13 15:13:19.528    1540-1540/? D/log﹕ action
03-13 15:13:19.528    1540-1540/? D/log﹕ data
03-13 15:13:19.528    1540-1540/? D/log﹕ xpos
03-13 15:13:19.528    1540-1540/? D/log﹕ ypos