3

i am new in android development, i don't know how to parse data from xml, so please help.

this is my Xml which i have to parse.

 <MediaFeedRoot>
        <MediaTitle>hiiii</MediaTitle>
        <MediaDescription>hellooooo.</MediaDescription>
        <FeedPath>how r u</FeedPath>
   </MediaFeedRoot>

Thanx in advance.

Kirit Bhayani
  • 65
  • 1
  • 5

2 Answers2

2

I dont understand that why people ask the question here without searching properly on net.Please do remember that search on net before asking anything here.... Below is the link where you can find a very good tutorial about xml parsing...

http://www.androidpeople.com/android-xml-parsing-tutorial-%E2%80%93-using-domparser

himanshu
  • 1,990
  • 3
  • 18
  • 36
  • +1 Agree with you. People want to have blood directly without digesting food :) – Paresh Mayani Mar 28 '12 at 06:54
  • 3
    This needs to be in comment. The reason being the link may go inactive over time and hence may not be helpful to other users with similar problem. One suggestion is to include sample code along with it. – jmishra Mar 28 '12 at 06:56
0

My suggestion is starting from the basic step:

  1. think about your xml file connection: url? local?
  2. instance a DocumentBuilderFactory and a builder

DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

OR

URLConnection conn = new URL(url).openConnection();
InputStream inputXml = conn.getInputStream();

DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance() .newDocumentBuilder(); Document xmlDoc = docBuilder.parse(inputXml);

Parsing XML file:

Document xmlDom = dBuilder.parse(xmlFile);

After that, it turns a xml file into DOM or Tree structure, and you have to travel a node by node. In your case, you need to get content. Here is an example:

String getContent(Document doc, String tagName){
        String result = "";

        NodeList nList = doc.getElementsByTagName(tagName);
        if(nList.getLength()>0){
            Element eElement = (Element)nList.item(0);
            String ranking = eElement.getTextContent();
            if(!"".equals(ranking)){
                result = String.valueOf(ranking);
            }

        }
        return result;
    }

return of the getContent(xmlDom,"MediaTitle") is "hiiii".

Good luck!

Eric
  • 1,271
  • 4
  • 14
  • 21