2012-11-27 66 views
0

我有這個xml文件,並且我獲得了一個包含所有模型的NodeList,當我撥打myNodeList.getLength()時,它給了我正確的數字2.但是當我撥打myNodeList.item(0).getNodeValue()應該返回url1時,它不顯示任何值。有什麼問題??myNodeList.item(0).getNodeValue()不返回任何值。 android

<?xml version="1.0" encoding="UTF-8"?> 
    <results> 
    <object id="1"> 
     <title>back parking lot</title> 
     <assets3d> 
      <model> 
      url1 
      </model> 
      <model> 
      url2 
      </model> 
     </assets3d> 
    </object> 
    </results> 

解析器類,我用NodeList models = myParser.getValue(e, "model",0);得到模型

public class XMLParser { 

    // constructor 
    public XMLParser() { 

    } 

    /** 
    * Getting XML from URL making HTTP request 
    * @param url string 
    * */ 
    public String getXmlFromUrl(String url) { 
     String xml = null; 

     try { 
      // defaultHttpClient 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(url); 

      HttpResponse httpResponse = httpClient.execute(httpPost); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      xml = EntityUtils.toString(httpEntity); 

     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     // return XML 
     return xml; 
    } 

    /** 
    * Getting XML DOM element 
    * @param XML string 
    * */ 
    public Document getDomElement(String xml){ 
     Document doc = null; 
     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
     try { 

      DocumentBuilder db = dbf.newDocumentBuilder(); 

      InputSource is = new InputSource(); 
       is.setCharacterStream(new StringReader(xml)); 
       doc = db.parse(is); 

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

    /** Getting node value 
     * @param elem element 
     */ 
    public final String getElementValue(Node elem) { 
     Node child; 
     if(elem != null){ 
      if (elem.hasChildNodes()){ 
       for(child = elem.getFirstChild(); child != null; child = child.getNextSibling()){ 
        if(child.getNodeType() == Node.TEXT_NODE ){ 
         Log.d("child.getNodeValue() ", "child.getNodeValue() "+child.getNodeValue()); 
         return child.getNodeValue(); 
        } 
       } 
      } 
     } 
     return "0000"; 
    } 

    /** 
     * Getting node value 
     * @param Element node 
     * @param key string 
     * */ 
    public String getValue(Element item, String str) {  
      NodeList n = item.getElementsByTagName(str);  
      Log.d("this.getElementValue(n.item(0))", "this.getElementValue(n.item(0)) "+this.getElementValue(n.item(0))); 
      return this.getElementValue(n.item(0)); 
     } 

    public NodeList getValue(Element item, String str,int a) {  
      NodeList n = item.getElementsByTagName(str);  
      Log.d("item.getElementsByTagName", "item.getElementsByTagName "+ n.getLength()); 
      //return this.getElementValue(n.item(0)); 
      return n; 
     } 
} 


} 
+0

如何將xml內容放入節點列表中? –

+0

我已添加解析器類 – Blake

回答

1

使用

myNodeList.item(0).getTextContent() 

myNodeList.item(0).getChildNodes().item(0).getNodeValue() 

的 「URL1」 是模型的子節點節點。

+0

不工作.... – Blake

+0

它真的在我的電腦上工作。 DOM XML樹與你的想象有點不同。編寫一個循環來輸出控制檯中的所有節點以瞭解結構。 – faylon