2012-06-06 5 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> 

내가 다음과 같이 파일 XML을 구문 분석 DOM을 사용

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

DOMParser를 사용하는 경우 XmlPullParser를 사용하는 경우 http://stackoverflow.com/questions/4827168/how-to-parse-the-value-in-the-attribute-in-xml-parsing –

+0

http://androiddevelopement.blogspot.in /2011/06/android-xml-parsing-tutorial-using.html –

+0

내 질문을 편집 했습니까? SAX가 매우 길기 때문에 DOM을 사용하여 파일 xml을 구문 분석하고 싶습니다. 도와 주실 수 있습니까? – 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(); 
      } 
     } 
    } 

}