1

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............

Sandeep Singh
  • 1,117
  • 2
  • 11
  • 29

2 Answers2

1

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.

David Kasabji
  • 1,049
  • 3
  • 15
  • 32
  • Thanks! Now I am able to fetch the data from that using XmlPullParser, but still not able to fetch FL tag value <![CDATA[ Yesterday ]]> Now i have just able to fetch "Yesterday" but i also need the value "Modified time". – Sandeep Singh Dec 04 '14 at 12:14
  • Try to fetch the entire TAG (START_TAG) and then do a substring on it, cause he might fetch everything from that TAG. – David Kasabji Dec 05 '14 at 12:05
  • To get "something" from `val="something"` use [getAttributeValue(null, "val")](http://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html#getAttributeValue(java.lang.String,%20java.lang.String)) when you're on a `START_TAG` and `getName()` is "FL". – TWiStErRob Dec 07 '14 at 10:52
0

Might be you overwrite title in loop :

if (title.item(0).getChildNodes().item(0) != null) {
   Log.v("name===>", ((Node) title.item(0).getChildNodes()).getNodeValue());
}
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67