2012-06-06 87 views
0

我想解析XML文件如下:解析XML文件返回null;

 <?xml version='1.0' encoding='UTF-8'?> 
    <rsp status='ok'> 
      <status_id>1111</status_id> 
      <user_id>TwitUsername</user_id> 
      <media_id>ZZ83F</media_id> 
     </rsp> 

我使用DOM來解析XML文件如下:

public String getStatus() 
    {  
    String status=""; 
    try {   
     InputStream is=this.getResources().openRawResource(R.raw.json); 
     Document xmlDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is); 
     Element root = xmlDoc.getDocumentElement();   
     NodeList rsp = root.getElementsByTagName("rsp");    
     for(int i=0;i<rsp.getLength();i++) 
     { 
      Node curNode = rsp.item(i); 
      // this tag is <study>, get `id` attribute first 
      status=String.valueOf(((Attr)curNode.getAttributes().item(0)).getValue());    
     } 

    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (SAXException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (ParserConfigurationException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    }  
    return status; 
} 

的getStatus方法返回null。

+0

請閱讀下列2分鐘教程:http://xstream.codehaus.org/tutorial.html – Shrikant

+0

他不是要求解析XML而他要求解析屬性。 –

+0

他的第一行是:我想解析文件xml,並且在他的問題本身中,他說他不知道如何解析xml文件。我試圖幫助他,所以請看他的問題,然後批評。 – Shrikant

回答

1

http://www.jondev.net/articles/Android_XML_SAX_Parser_Example

@Override 
    public void startElement(String namespaceURI, String localName, String qName, 
      Attributes atts) throws SAXException { 
     if (localName.equals("rsp")) { 
       System.out.println("The value of attribute 'status' is: " + atts.getValue("status")); 
     } 
    } 
+0

如果使用XmlPullParser http://stackoverflow.com/questions/4827168/how-to-parse-the-value-in-the-attribute-in-xml-parsing –

+0

如果使用DOMParser http://androiddevelopement.blogspot.in /2011/06/android-xml-parsing-tutorial-using.html –

+0

我編輯了我的問題?。我想通過DOM解析文件xml,因爲SAX很長嗎?你能幫我嗎? – user1423447

0

試試這個方法

public void getGeoLocation(Document doc)throws IOException { 

    NodeList nodeList, nchild; 
    Node currentNode, thisNode; 
    nodeList = doc.getElementsByTagName("rsp"); 
    int length = nodeList.getLength(); 
    for (int i = 0; i < length; i++) { 

     currentNode = nodeList.item(i); 
     nchild = currentNode.getChildNodes(); 
     int clength = nchild.getLength(); 

     for (int j = 0; j < clength; j++) { 

      thisNode = nchild.item(j); 

      if ("rsp".equals(thisNode.getNodeName())) { 

         org.w3c.dom.Element e1 = (org.w3c.dom.Element) thisNode; 
         String status = e1.getAttribute("status"); 

      } 
      if ("status_id".equals(thisNode.getNodeName())) { 

       String status_id = thisNode.getTextContent(); 
      } 
      if ("user_id".equals(thisNode.getNodeName())) { 

       String user_id = thisNode.getTextContent(); 
      } 
      if ("media_id".equals(thisNode.getNodeName())) { 

       String media_id = thisNode.getTextContent(); 
      } 
     } 
    } 

}