0

I want to parse xml file as follow:

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

I use DOM to parse file xml as follow:

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

But getStatus method return null.

5
  • Please read the following 2 min tutorial: xstream.codehaus.org/tutorial.html Commented Jun 6, 2012 at 6:49
  • He isnt asking to parse XML rather he is asking to parse Attributes. Commented Jun 6, 2012 at 6:59
  • He's first line is: I want to parse file xml, and also, in his question itself, he said he does not know how to parse xml file. I was trying to help him, so please see his question and then criticize. Commented Jun 6, 2012 at 7:04
  • Parse file XML special If haven't status=ok ...? Commented Jun 6, 2012 at 7:05
  • So I guess, its part of parsing XML file itself, right? Commented Jun 6, 2012 at 7:07

2 Answers 2

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"));
        }   
    } 
Sign up to request clarification or add additional context in comments.

7 Comments

I edited my question?.I want to parse file xml by DOM becasue SAX is very long?Can you help me?
I done as you write but it's not work..I change status=String.valueOf(((Attr)curNode.getAttributes().item(0)).getValue()); it also not work :(
can you put the breakpoint at this line curNode.getAttributes("status") and check curNode attributes and their keys ?
|
0

try this method

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();
            }
        }
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.