Dom xml parsing in android

I'm new to android development, I don't know how to parse data from xml, so please help.

this is my Xml I have to parse.

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

      

Thanx in advance.

+3


source to share


2 answers


I don't understand why people are asking this question here without searching the net properly. Please remember this web search before asking for anything here. Below is the link where you can find a very good xml parsing tutorial ...



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

+2


source


My suggestion starts with a basic step:

  • think about your connection to the xml: url file? local?
  • DocumentBuilderFactory instance and builder

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

OR

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

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

XML file parsing:



XmlDom document = dBuilder.parse (xmlFile);

After that, it turns the xml file into a DOM or Tree structure and you need to move node to node. In your case, you need to get content. Here's 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;
    }

      

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

Good luck!

0


source







All Articles