Not able to fetch the inner node list data from XML using XML Parsing

229 views Asked by At

I am trying to fetch the FL tag multiple times but when i an trying inner node list it only fetch first item only. For more see my output. This is the XML data i got from web Service

<response uri="/ats/private/xml/JobOpenings/getRecords">
    <result>
        <JobOpenings>
            <row no="1">
                <FL val="JOBOPENINGID">
                    <![CDATA[ 100 ]]>
                </FL>
                <FL val="Published in website">
                    <![CDATA[ true ]]>
                </FL>
                <FL val="Modified by">
                    <![CDATA[ Admmin ]]>
                </FL>
                <FL val="Modified time">
                    <![CDATA[ Yesterday ]]>
                </FL>
            </row>
            <row no="2">
                <FL val="JOBOPENINGID">
                    <![CDATA[ 101 ]]>
                </FL>
                <FL val="Published in website">
                    <![CDATA[ true ]]>
                </FL>
                <FL val="Modified by">
                    <![CDATA[ Admin ]]>
                </FL>
                <FL val="Modified time">
                    <![CDATA[ 2 Days Ago ]]>
                </FL>
            </row>
            <row no="3">
                <FL val="JOBOPENINGID">
                    <![CDATA[ 102 ]]>
                </FL>
                <FL val="Published in website">
                    <![CDATA[ true ]]>
                </FL>
                <FL val="Modified by">
                    <![CDATA[ Admin ]]>
                </FL>
                <FL val="Modified time">
                    <![CDATA[ 2 Days Ago ]]>
                </FL>
            </row>
        <JobOpenings>
    <result>
</response>

And this is the Code i used in my Activity

public class AndroidXMLParsingActivity extends ListActivity {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(AppConfig.URL); // getting XML
        Document doc = parser.getDomElement(xml); // getting DOM element

        NodeList nodeList = doc.getElementsByTagName("row");

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

            Node node = nodeList.item(i);

            Element playlistElement = (Element) node;

            NodeList title = playlistElement.getElementsByTagName("FL");

            for (int kl = 0; kl < title.getLength(); kl++) {

                if (title.item(0).getChildNodes().item(0) != null) {

                    Element nameElement = (Element) title.item(0);
                    title = nameElement.getChildNodes();
                    Log.v("name===>", ((Node) title.item(0)).getNodeValue());

                }
            }

        }

    }
}

Here is the Output :

12-04 15:56:08.694: V/name===>(3918): JOBOPENINGID  100
12-04 15:56:08.694: V/name===>(3918): JOBOPENINGID  101
12-04 15:56:08.704: V/name===>(3918): JOBOPENINGID  102
12-04 15:56:08.704: V/name===>(3918): JOBOPENINGID  103
12-04 15:56:08.704: V/name===>(3918): JOBOPENINGID  107
12-04 15:56:08.704: V/name===>(3918): JOBOPENINGID  108
12-04 15:56:08.704: V/name===>(3918): JOBOPENINGID  118
12-04 15:56:08.704: V/name===>(3918): JOBOPENINGID  200

Help me Please............

2

There are 2 answers

3
David Kasabji On BEST ANSWER

Why don you use XmlPullParser for parsing the XML? I have never used your version, but seem to me that XmlPullParser will do it's job better, since you tell it to focus on START_TAG (for example) and then write the name of the tag you want to be found (FL in your example).

What XPP will do is, he'll read through entire XML document and search for the given START_TAG. Once found, you can do whatever you want with it, inside a switch case.

0
Haresh Chhelana On

Might be you overwrite title in loop :

if (title.item(0).getChildNodes().item(0) != null) {
   Log.v("name===>", ((Node) title.item(0).getChildNodes()).getNodeValue());
}