Xbase++
Xbase++
XML SearchForAttribute Method
See more XML Examples
Demonstrates the SearchForAttribute method.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
LOCAL nSuccess
LOCAL oXml
LOCAL oXSearchRoot
LOCAL oXBeginAfter
LOCAL oXFound
LOCAL oXSearch
nSuccess := 0
oXml := CreateObject("Chilkat.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")
IF (oXml:LastMethodSuccess == 0)
? "searchRoot not found, searching from root."
oXSearchRoot := oXml:GetRoot()
ENDIF
// Search for all "fruit" nodes having a color attribute
// where the name of the color ends in "e";
oXBeginAfter := oXSearchRoot:GetSelf()
oXFound := oXSearchRoot:SearchForAttribute(oXBeginAfter, "fruit", "color", "*e")
DO WHILE (oXSearchRoot:LastMethodSuccess == 1)
? oXFound:Content + ": " + oXFound:GetAttrValue("color")
oXBeginAfter:destroy()
oXBeginAfter := oXFound
oXFound := oXSearchRoot:SearchForAttribute(oXBeginAfter, "fruit", "color", "*e")
ENDDO
// The correct output is:
// grape: purple
// blueberry: blue
oXBeginAfter:destroy()
? "--------------------------"
// ---------------------------------------------------------------------------------
// Now do the same, but instead use SearchForAttribute2
// which updates the internal reference of the caller instead
// of returning the found node.
oXBeginAfter := oXSearchRoot:GetSelf()
oXSearch := oXSearchRoot:GetSelf()
nSuccess := oXSearch:SearchForAttribute2(oXBeginAfter, "fruit", "color", "*e")
DO WHILE nSuccess == 1
? oXSearch:Content + ": " + oXSearch:GetAttrValue("color")
// Copy the internal references so that the next search
// begins after the found node.
oXBeginAfter:CopyRef(oXSearch)
oXSearch:CopyRef(oXSearchRoot)
nSuccess := oXSearch:SearchForAttribute2(oXBeginAfter, "fruit", "color", "*e")
ENDDO
oXSearch:destroy()
oXBeginAfter:destroy()
oXSearchRoot:destroy()
oXml:destroy()