Lua
Lua
Demonstrate XML SearchForTag2
Demonstrates how to use SearchForTag2.Chilkat Lua Downloads
local chilkat = require("chilkat")
local xml = chilkat.Xml.new()
-- Load the following XML:
-- <Test>
-- <testchild1>
-- test
-- </testchild1>
-- <testchild1>
-- test1
-- </testchild1>
-- <testchild1>
-- test2
-- </testchild1>
-- <testchild1>
-- test3
-- </testchild1>
-- <testchild1>
-- test4
-- </testchild1>
-- <testchild1>
-- test5
-- </testchild1>
-- <testchild1>
-- test6
-- </testchild1>
-- </Test>
local success = xml:load_xml("<Test><testchild1>test</testchild1><testchild1>test1</testchild1><testchild1>test2</testchild1><testchild1>test3</testchild1><testchild1>test4</testchild1><testchild1>test5</testchild1><testchild1>test6</testchild1></Test>")
local i = 0
local xmlSearch = xml:get_root()
local afterPtr = xml:get_root()
local pTemp
-- Alway begin the search from the root.
while xmlSearch:search_for_tag2(afterPtr, "testchild1") do
-- If successful, xmlSearch now points to the found element.
i = i + 1
-- Set afterPtr = xmlSearch (so we find the next match after the one we just found)
-- Rest xmlSearch to the root of the XML tree to be searched.
print(i .. ": " .. xmlSearch.content)
pTemp = afterPtr
afterPtr = xmlSearch
pTemp:get_root2()
xmlSearch = pTemp
end
print("----")
-- A better way:
local sbState = chilkat.StringBuilder.new()
i = 0
while xml:next_in_traversal2(sbState) do
if xml:tag_equals("testchild1") then
print(i .. ": " .. xml.content)
i = i + 1
end
end
xml:get_root2()