XML - get sub-node of a node

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
palanolho
Posts: 122
Joined: Sat Apr 27, 2013 11:40 pm

XML - get sub-node of a node

Post by palanolho » Tue Jun 10, 2014 9:49 pm

Hi everyone...

I'm having some trouble getting some informations from my XML and maybe someone can help me with it (or point me in the right direction).

This is my XML scructure (example)

Code: Select all

<root>
	<AAA att="1">
		<SubA>...</board>
		<SubB att1="..." att2="...">							
			<SubSubA att="..."> 
				<item1>...</item1>
				<item2>...</item2>
				<item3>...</item3>
			</SubSubA>
		</SubB>
	</AAA>
	<AAA att="2">
		(...)
	</AAA>
	<BBB att="3">
		(...)
	</BBB>
	(...)
</root>
Now, based on this structure

This gives me the path to the the child node (of root) with the attribute att = "1"

Code: Select all

put revXMLMatchingNode(treeID, "/root", , "att", "1", -1) into componentNode
This will give me the Content (value) of the node ".../SubA"

Code: Select all

revXMLNodeContents(treeID, componentNode & "/SubA")

Now ... how do I get the Node "SubB"?
I though that I could use something like this but .. it doesn't work.

Code: Select all

revXMLMatchingNode(treeID, componentNode , "SubB", , , -1)
Any Ideas ??

Many thanks in advance

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: XML - get sub-node of a node

Post by Mark » Wed Jun 11, 2014 2:11 pm

Hi,

It isn't entirely clear to me what the problem is. To get all subnodes, you could use

Code: Select all

put revXMLEvaluateXPath(gTreeID,"/root/AAA/*") into myList
This will tell you that AAA has subnodes SubA and SubB. Similarly, you can get the subnodes of AAA etc. If you want to have the data in item1, you can use

Code: Select all

put revXMLNodeContents(gTreeID, "/root/AAA/SubB/item1") into myData
Does this help?

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

palanolho
Posts: 122
Joined: Sat Apr 27, 2013 11:40 pm

Re: XML - get sub-node of a node

Post by palanolho » Wed Jun 11, 2014 2:38 pm

hmm I thought that Ir could be possible to get the path in a generic way without providing the full path (as you do).
Seems that that is the way to go...


about "revXMLEvaluateXPath" ... I didn't knew that handlers ... it's not on the API Dictionary :( is there any other "new" XML related handlers??
Any up to date list of new/recent handlers ?


Many thanks,
- Miguel

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: XML - get sub-node of a node

Post by Mark » Wed Jun 11, 2014 2:44 pm

Hi Miguel,

It is easy to write a generic script to get all paths:

Code: Select all

on mouseUp
   // here, fld "Filepath" contains the path to the XML file
   put revXMLCreateTreeFromFile(fld "Filepath",false,true,false) into gTreeID
   put "/" into myNode
   put 0 into myCounter
   repeat
      add 1 to myCounter
      put revXMLEvaluateXPath(gTreeID,myNode & "/*") & cr after myList
      filter myList without empty // slightly inefficient, sorry
      if line myCounter of myList is empty then
         exit repeat
      else
         put line myCounter of myList into myNode
      end if
   end repeat
   // clean up
   revXMLDeleteTree gTreeID
   put empty into gTreeID // just testing
   put myList // see result in message box
end mouseUp
This is for a customer's project I'm currently working on. The principle of this script allows you to find any node and then get its data or attributes.

The functions revXMLEvaluateXPath and revXMLDataFromXPathQuery have been added in LC 6.5 and I consider them essential.

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Post Reply