Options

Selecting a particular node in XML document

shettarvikasshettarvikas Member Posts: 106
Hi,

Please let me know, how to navigate in XML doc to find a particular node and its value,

XML file sample,
item>
<title>AED/INR</title>
<link>http://xxx.com/INR/AED.aspx</link&gt;
<guid>f770299c-1d99-4c84-981e-3eb95d7bf4fd</guid>
<pubDate>Thu, 27 May 2010 21:26:45 GMT</pubDate>
<description>1 Indian Rupee = 0.07883 United Arab Emirates Dirham</description>
<category>Middle East</category>
</item>
<item>
<title>ARS/INR</title>
<link>http://xxx.com/INR/ARS.aspx</link&gt;
<guid>05d0b0aa-c5c2-49a1-828f-820a03734f70</guid>
<pubDate>Thu, 27 May 2010 21:26:45 GMT</pubDate>
<description>1 Indian Rupee = 0.08379 Argentine Peso</description>
<category>South America</category>
</item>
.... so on

Here I am not able to user selectSingleNode function because it is giving me the 1st XML value (i.e. AED/INR). I wan to navigate to a particular node say for US dollar, its 46th item, how can I navigate to that item and select the value of description in the child node

Nav Code is,
IF ISCLEAR(xmlHTTP) THEN
  CREATE(xmlHTTP);
IF ISCLEAR(XMLDoc) THEN
  CREATE(XMLDoc);
xmlHTTP.open('GET','http://themoneyconverter.com/INR/rss.xml',FALSE);
xmlHTTP.send;
XMLDoc := xmlHTTP.responseXML;

XMLNode :=XMLDoc.selectSingleNode('//rss/channel/item/description');

IF NOT ISCLEAR(XMLNode) THEN
  MESSAGE(XMLNode.text);

XMLDoc.save('C:\NavRSS2.xml');
CLEAR(XMLNode);
CLEAR(XMLDoc);
CLEAR(xmlHTTP);

Please suggest.

Thnaks in advance,

Regards,
Vikas

Comments

  • Options
    FAFLFAFL Member Posts: 2
    There are different ways.

    First you can use a NodeList and go through its child-elements:
    using WHILE
    XMLNodeList := XMLDoc.selectNodes('//rss/channel/item');
    XMLNode := XMLNodeList.nextNode;
    WHILE NOT ISCLEAR(XMLNode) DO BEGIN
      MESSAGE(XMLNode.selectSingleNode('description').text);
    XMLNode := XMLNodeList.nextNode;
    END;
    
    ... or if you prefer FOR
    XMLNodeList := XMLDoc.selectNodes('//rss/channel/item');
    FOR i := 0 TO XMLNodeList.length DO BEGIN
      XMLNode := XMLNodeList.item(i);
      MESSAGE(XMLNode.selectSingleNode('description').text);
    END;
    

    Or the "direct" way:
    XMLNode := XMLDoc.selectSingleNode('//rss/channel/item[title="USD"]/description');
    

    Best regards
    Falk
  • Options
    Navsyst2Navsyst2 Member Posts: 29
    Hi FAFL,

    what can I do to prove if one node exists or not?

    For example: the node description by USD doesn't exists.
    XMLNode := XMLDoc.selectSingleNode('//rss/channel/item[title="USD"]/description');
  • Options
    Navsyst2Navsyst2 Member Posts: 29
    FAFL wrote:
    Or the "direct" way:
    XMLNode := XMLDoc.selectSingleNode('//rss/channel/item[title="USD"]/description');
    

    Best regards
    Falk

    Hi FAFL,

    what can I do to prove if one node exists or not?

    For example: the node description by USD doesn't exists.
  • Options
    IsakssonMiIsakssonMi Member Posts: 77
    Hi Navsyst2,

    After your xpath-query, you check if the node is null (ISCLEAR(XmlNode)) to find it out .
Sign In or Register to comment.