sujay
sujay

Reputation: 1845

Getting value from xml file

i have a xml file

<?xml version="1.0" encoding="utf-8"?> 
<NewDataSet> <Password>abcd</Password> </NewDataSet>

how to get the string "abcd" from the above xml file . I am very new to android platform, please help me out thanks in advance

Upvotes: 0

Views: 1302

Answers (2)

rekaszeru
rekaszeru

Reputation: 19220

in my opinion it's easier to understand something via an example, and even more, if we know example what we expect from that example to do.

So I'll post a little sample, which does nothing else but what you need: reads the password from that xml file and returns it in it's readPassword() method:

import java.io.StringReader;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

public class XmlSample
{
    private static final String xmlSource = 
        "<?xml version='1.0' encoding='utf-8'?>" +
        "<NewDataSet>" +
        "   <Password>abcd</Password>" +
        "</NewDataSet>";
    public final class MyXmlHandler  extends DefaultHandler
    {
        /**
         * the Password tag's value
         */
        private String  password;
        /**
         * for keeping track where the cursor is right now
         */
        private String currentNodeName;

        public MyXmlHandler()
        {
        }

        /**
         * It is called when starting to process a new element (tag)
         * At this point you change the currentNodeName member
         */
        @Override
        public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException
        {
            this.currentNodeName = localName;
        }

        /**
         * It is called when an element's processing has finished 
         *      ("</ _tag>" or "... />" is reached) 
         * Clear the currentNodeName member
         */
        @Override
        public void endElement(String namespaceURI, String localName, String qName) throws SAXException
        {
            this.currentNodeName = null;
        }

        /**
         * It is called when the currentNodeName tag's body is processed.
         * In the ch[] array are the character values of that element.
         */
        @Override
        public void characters(char ch[], int start, int length)
        {
            if (this.currentNodeName.equals("Password"))
                password = new String(ch, start, length);
        }

        public String getPassword()
        {
            return password;
        }
    }

    public String readPassword() throws Exception
    {
        //create an inputSource from the xml value;
        //when you get this xml from the server via http, you should use something like:
        //HttpEntity responseEntity = response.getEntity();
        //final InputSource input = new InputSource(responseEntity.getContent());
        final InputSource input = new InputSource(new StringReader(xmlSource));

        SAXParserFactory parserFactory = SAXParserFactory.newInstance();
        SAXParser parser = parserFactory.newSAXParser();
        XMLReader reader = parser.getXMLReader();

        MyXmlHandler myHandler = new MyXmlHandler();
        //attach your handler to the reader
        reader.setContentHandler(myHandler);

        //parse the input InputSource. It will fill your myHandler instance
        reader.parse(input);
        return myHandler.getPassword();
    }
}

The code itself is pretty small, i just inserted some comments for better understanding. Let me know if you need more help.

Upvotes: 0

Paresh Mayani
Paresh Mayani

Reputation: 128428

From your comment and question, i will suggest you to refer this links:

  1. http://www.anddev.org/parsing_xml_from_the_net_-_using_the_saxparser-t353.html
  2. http://www.ibm.com/developerworks/opensource/library/x-android/
  3. http://www.androidpeople.com/android-xml-parsing-tutorial-%E2%80%93-using-domparser

From first link, you can go step-by-step and second link includes examples for all the parsing techniques.

I suggest to go with SAX(Simple API for XML) Parser.

Upvotes: 1

Related Questions