3

I am parsing the xml but i can't parse the xml tags which are within and another tags. Here is the XML structure.

<Entry>
  <MediaID>434234242342</MediaID>
  <MediaName>Brazil</MediaName>
  <PhoneNo>
     <Ip>23232323232</Ip>
     <Ip>32323232323</Ip>
     <Ip>323232323232</Ip>
  </PhoneNo>
</Entry>

Here is the Java code. I m successfully parsing MediaID and MediaName but how can I parse the tags within

Document doc = parser.getDomElement(return_string); // getting DOM element
NodeList nl2 = doc.getElementsByTagName("Entry");

   for (int i = 0; i < nl2.getLength(); i++) {

        Element e = (Element) nl2.item(i); 
        media_name = parser.getValue(e,"MediaName");
        mediaID    = parser.getValue(e,"MediaID");
        phoneNo =  parser.getValue(e,"PhoneNo"); //it is not working
   }
5
  • How do you expect the value of phoneNo be? The value of first tag or value of all child tags or as an xml? Commented Jul 7, 2014 at 14:58
  • Try making a NodeList for the tag Ip (the way you did for entry) and then iterate over the list to get the phone numbers. Commented Jul 7, 2014 at 15:00
  • Which xml parser are you using? Commented Jul 7, 2014 at 15:08
  • @SyamS I want to retrieve the value of <IP>. I want to get value of app all sub tags of <PhoneNo> Commented Jul 7, 2014 at 15:09
  • @alkis I m using this example androidhive.info/2011/11/android-xml-parsing-tutorial Commented Jul 7, 2014 at 15:11

2 Answers 2

1

Try recurssion. You can format the output the way you want it.

import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class DOMParser {
    public static void main(final String[] args) throws SAXException, IOException, ParserConfigurationException {
        String xml = "<Entry>"
                + "<MediaID>434234242342</MediaID>"
                + "<MediaName>Brazil</MediaName>"
                + "<PhoneNo>"
                    + "<Ip>23232323232</Ip>"
                    + "<Ip>32323232323</Ip>"
                    + "<Ip>323232323232</Ip>"
                + "</PhoneNo>"
            + "</Entry>";
        DOMParser parser = new DOMParser();
        final Document doc = parser.getDomElement(xml);
        parser.parse(doc.getDocumentElement());
    }

    public void parse(final Element e) {
        final NodeList children = e.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            final Node n = children.item(i);
            if(n.getNodeType() == Node.TEXT_NODE){
            System.out.println(n.getTextContent());
            } else if (n.getNodeType() == Node.ELEMENT_NODE) {
            System.out.print(n.getNodeName() + " : ");
            parse((Element) n);
            }
        }
    }

    public Document getDomElement(String xml){
        Document doc = null;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {

            DocumentBuilder db = dbf.newDocumentBuilder();

            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xml));
            doc = db.parse(is); 

            } catch (ParserConfigurationException e) {
            return null;
            } catch (SAXException e) {
            return null;
            } catch (IOException e) {
            return null;
            }

            return doc;
        }
}
Sign up to request clarification or add additional context in comments.

Comments

1

I don't know what parser are you working with, I would recommend XMLPullParser :

XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        XmlPullParser xpp = factory.newPullParser();





        xpp.setInput(reader);// the reader you are using to read the xml file 


        int eventType = xpp.getEventType();

        // Loop through pull events until we reach END_DOCUMENT
        while (eventType != XmlPullParser.END_DOCUMENT) {
            // Get the current tag
            String tagname = xpp.getName();

            // React to different event types appropriately
            switch (eventType) {
            case XmlPullParser.START_TAG:
                if (tagname.equalsIgnoreCase(THE_TAG_YOUR_LOOKING_FOR)) {
                    //anything you want to do at the start of the tag
                }
                break;

            case XmlPullParser.TEXT:

                //normally you would retrive here the text which is between the tags
                //with xpp.getText()
                break;

            case XmlPullParser.END_TAG:
                //generally a serie of if else depending on which tag you are on and 
                //what you want to do with the content
                break;

            default:
                break;
            }

1 Comment

how can i get Ip tag using this code ?? and store different variable ??

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.