List all XML nodes

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
theotherbassist
Posts: 115
Joined: Thu Mar 06, 2014 9:29 am
Location: UK

List all XML nodes

Post by theotherbassist » Sat Dec 09, 2017 12:29 pm

Hello all,

Is there a simple way to separately list out the paths to all nodes, regardless of depth, in an XML document?

revXMLMatchingNode does what I want, but unfortunately it stops at the first match and I need all the nodes with a given attribute value. The source XML often has multiple siblings of the same name with the attribute value.

Maybe the answer lies with revXMLEvaluatePath, but the dictionary entry on that one isn't exactly clear.

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: List all XML nodes

Post by bogs » Sat Dec 09, 2017 2:24 pm

I *think* the easiest might be a repeat loop, something like (not tested) Image

Code: Select all

put "" into (variable or field used as container)
repeat for each revXMLMatchingNode(your parameters)
   put (value looked for) & cr after (variable or field)
end repeat 
Image

theotherbassist
Posts: 115
Joined: Thu Mar 06, 2014 9:29 am
Location: UK

Re: List all XML nodes

Post by theotherbassist » Sat Dec 09, 2017 2:44 pm

Thanks for the reply. That makes sense... and that was exactly how I ran my first attempt. But the issue with revXMLMatchingNode is that it only returns the first matching node. So if I have
Root/nodeX/nodeY and
Root/nodeX/nodeY
both with the same attribute and value, only the path to the first will be returned. But I need to get a different attribute value from both nodes and combine it later on.

I basically just need a version of revXMLMatchingNode that returns a list of all matching nodes. Should I copy the whole tree and just delete the matched nodes from the copy as I go?

theotherbassist
Posts: 115
Joined: Thu Mar 06, 2014 9:29 am
Location: UK

Re: List all XML nodes

Post by theotherbassist » Sat Dec 09, 2017 5:07 pm

Guess I'll just use an XPATH expression unless someone has a better idea. I was hoping to avoid those though as they seem a bit temperamental in LC (causing LC to close without saving and without explanation if the xpath expression is formatted incorrectly in v8.1).

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: List all XML nodes

Post by bogs » Sat Dec 09, 2017 6:45 pm

I'm sorry it took so long to reply. Unfortunately, I don't do much wihth xml (read that nothing), so short of your reply nothing is hitting me off the top of my head. Hopefully one of the xml'ers will chime in shortly :)
Image

theotherbassist
Posts: 115
Joined: Thu Mar 06, 2014 9:29 am
Location: UK

Re: List all XML nodes

Post by theotherbassist » Sun Dec 10, 2017 12:37 am

Got it... you wouldn't think so from the name, but the answer lies in revXMLChildContents.

Code: Select all

function listNodes pContent
   put revXMLCreateTree(pContent,false,true,false) into tTreeID
   put revXMLRootNode(tTreeID) into tRoot
   put revXMLChildContents(tTreeID,tRoot,COMMA,CR,"full",-1) into tNodesList
   
   repeat with n = 1 to the number of lines of tNodesList
      put item 1 of line n of tNodesList into line n of tNodesList
   end repeat
   
   revXMLDeleteAllTrees
   return tNodesList
   
end listNodes

I'll definitely be putting the lines into an array before running the repeat loop above, but this demonstrates the concept.

:)
Last edited by theotherbassist on Sun Dec 10, 2017 2:36 pm, edited 1 time in total.

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: List all XML nodes

Post by bogs » Sun Dec 10, 2017 12:41 am

Actually, that makes sense to me. I probably should have looked in the dictionary a bit longer <sigh> but congrats on figuring it out :)
Image

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”