Sample code for 30+ languages & platforms
Xbase++

XML SearchForTag Method

See more XML Examples

Demonstrates the SearchForTag and SearchForTag2 methods.

The input XML, available at http://www.chilkatsoft.com/data/fruitSearch.xml, is this:

<root>
    <searchRoot>
        <fruit color="red">apple</fruit>
        <fruit color="green">pear</fruit>
        <veg color="orange">carrot</veg>
        <meat animal="cow">beef</meat>
        <xyz>
            <fruit color="blue">blueberry</fruit>
            <veg color="green">broccoli</veg>
        </xyz>
        <fruit color="purple">grape</fruit>
        <cheese color="yellow">cheddar</cheese>
    </searchRoot>
    <fruit color="red">strawberry</fruit>
    <fruit color="orange">peach</fruit>
</root>

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oXml
LOCAL oXSearchRoot
LOCAL oXBeginAfter
LOCAL oXFound
LOCAL oXSearch

nSuccess := 0

oXml := CreateObject("Chilkat.Xml")

//  The sample input XML is available at http://www.chilkatsoft.com/data/fruitSearch.xml
nSuccess := oXml:LoadXmlFile("qa_data/xml/fruitSearch.xml")
IF (nSuccess != 1)
    ? oXml:LastErrorText
    oXml:destroy()
    RETURN
ENDIF

//  Search the sub-tree rooted at "searchRoot";
oXSearchRoot := oXml:FindChild("searchRoot")

//  Search for all nodes having the tag "fruit";
oXBeginAfter := oXSearchRoot:GetSelf()
oXFound := oXSearchRoot:SearchForTag(oXSearchRoot, "fruit")
DO WHILE (oXSearchRoot:LastMethodSuccess == 1)

    ? oXFound:Content

    oXBeginAfter:destroy()
    oXBeginAfter := oXFound
    oXFound := oXSearchRoot:SearchForTag(oXBeginAfter, "fruit")
ENDDO

oXBeginAfter:destroy()

? "--------------------------"

//  ---------------------------------------------------------------------------------
//  Now do the same, but instead use SearchForTag2
//  which updates the internal reference of the caller instead
//  of returning the found node.

oXBeginAfter := oXSearchRoot:GetSelf()
oXSearch := oXSearchRoot:GetSelf()

nSuccess := oXSearch:SearchForTag2(oXBeginAfter, "fruit")
DO WHILE nSuccess == 1

    ? oXSearch:Content

    //  Copy the internal references so that the next search
    //  begins after the found node.
    oXBeginAfter:CopyRef(oXSearch)
    oXSearch:CopyRef(oXSearchRoot)

    nSuccess := oXSearch:SearchForTag2(oXBeginAfter, "fruit")
ENDDO

oXSearch:destroy()
oXBeginAfter:destroy()

oXSearchRoot:destroy()

oXml:destroy()