2014-03-13 43 views
0

我使用的getElementsByTagName( 「*」)的Android的DOMParser的getElementsByTagName( 「*」)

文件建立一個說問題

-getElementsByTagName節點列表的getElementsByTagName(字符串名稱)

返回一個節點列表所有具有給定標記名稱的後代元素,文檔順序爲 。

參數:name - 要匹配的標記的名稱。特殊值「*」匹配所有標籤。返回:匹配Element節點的列表。

而它在XML像只返回的第一個元素節點:

<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; 

    } 
+0

有啥你的問題? – tyczj

+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 
+0

它只返回第一個元素

+0

你可以發佈你的代碼嗎?我好奇你怎麼解析 –

+0

@ShymaaOthman你需要遍歷所有你想要的每個標籤的子節點 – tyczj