首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android DOMParser getElementsByTagName("*")

Android DOMParser getElementsByTagName("*")
EN

Stack Overflow用户
提问于 2014-03-13 14:10:43
回答 1查看 1.9K关注 0票数 0

我对使用getElementsByTagName("*")有问题

Documentaion说

-getElementsByTagName NodeList getElementsByTagName(字符串名) 按文档顺序返回具有给定标记名的所有子代元素的NodeList。 参数:名称-要匹配的标记的名称。特殊值"*“匹配所有标记。返回:匹配元素节点的列表。

虽然它只返回xml中的第一个元素Node,例如:

代码语言:javascript
复制
<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>

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

解析器

代码语言:javascript
复制
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 "";
    }
}

代码

代码语言:javascript
复制
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;

    }
EN

回答 1

Stack Overflow用户

发布于 2014-03-13 14:17:18

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

编辑:

我还注意到还有另一个顶级元素-- entry。您可能也需要获取该元素,然后获取entry中的所有子节点。

EDIT2:

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

代码语言:javascript
复制
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;
}

输出:

代码语言:javascript
复制
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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22381223

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档