2

I'm using the DOM parser to retrive information from a XML file that looks like this:

<data>
    <metData>
        <wantedInformation>
    </metData>
    <metData>
        <Information>
    </metData>
    <metData>
        <Information>
    </metData>
<data>

The problem is because I don't know how to parse only the first part of <metData>. I don't need the second and the third part, but the parser displays them anyway.

The xml file is from a weather forcast site: http://www.meteo.si/uploads/probase/www/fproduct/text/sl/fcast_SLOVENIA_MIDDLE_latest.xml
and I need just the following line: <nn_shortText>oblačno</nn_shortText>

4
  • Your file content did not come through - you need to quote that as code. Commented Feb 28, 2011 at 10:10
  • Tomazin: Which tag data you want to parse Commented Feb 28, 2011 at 10:15
  • That kind of XML document would throw an exception as it is not well formed. Commented Feb 28, 2011 at 10:29
  • I've edited my question and added link to a XML file Commented Feb 28, 2011 at 12:55

2 Answers 2

2

Pls take care whether your XML file is well formed or not,

You have to the notice three methods which i had shown below, they are

    1. getElementsByTagName - Mention the tag which you want to parse
    2.getChildNodes - retervies the child node 
    3.getNodeValue()- with the help of this method you can access the
 value of particular tag

Step 1: Create a Method to parse _Information_Value ,inorder to parse the data of Information tag

String[] infoId=null;

public  void  parse_Information_Value() throws UnknownHostException{


        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        try {

            DocumentBuilder builder = factory.newDocumentBuilder();
            Document dom = builder.parse(this.getInputStream());
            org.w3c.dom.Element root = dom.getDocumentElement();
            NodeList items = root.getElementsByTagName("metData");
            int a=items.getLength();
            int k=0;

            for (int i = 0; i < items.getLength(); i++) {
                Message_category message = new Message_category();
                Node item = items.item(i);
                NodeList properties = item.getChildNodes();
                for (int j = 0; j < properties.getLength(); j++) {
                    Node property = properties.item(j);
                    String name = property.getNodeName();
                    if (name.equalsIgnoreCase("wantedInformation")) {
                        message.setId(property.getFirstChild()
                                .getNodeValue());
                        infoId[k]=property.getFirstChild().getNodeValue();
                        k++;
                    }
                }

            }
        } catch (Exception e) {         }

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

1 Comment

For More Information, Refer my Blog sankarganesh-info-exchange.blogspot.com/2011/04/…
0

Depending on the size of your document, you may also want to use at a streaming oriented parser like SAX or Stax, which does not pull the whole document into memory and thus needs less memory than DOM.

Good thing is that SAX is already built into Android, so you can use it right away. See this link for a usage example.

1 Comment

I know this, but sax parser has more classes. With DOM I can just include it in one class (Activity) to check one wanted parameter and than use toast to display it.

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.