Using the DOM parser in Android

I use the DOM parser to extract information from an XML file that looks like this:

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

The problem is that I do not know how to analyze only the first part <metData>. I don’t need the second and third parts, but the parser displays them anyway.

The xml file is on the weather website: http://www.meteo.si/uploads/probase/www/fproduct/text/sl/fcast_SLOVENIA_MIDDLE_latest.xml
and I only need the following line:<nn_shortText>oblačno</nn_shortText>

+2
source share
2 answers

Pls cares whether your XML file is well-formed or not,

You should notice three methods which I showed below, they

    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

1. _Information_Value,

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) {         }

    }
+2

, SAX Stax, , , , DOM.

, SAX Android, . . .

0

All Articles