Page 1 of 1

XML - get sub-node of a node

Posted: Tue Jun 10, 2014 9:49 pm
by palanolho
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

Re: XML - get sub-node of a node

Posted: Wed Jun 11, 2014 2:11 pm
by Mark
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

Re: XML - get sub-node of a node

Posted: Wed Jun 11, 2014 2:38 pm
by palanolho
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

Re: XML - get sub-node of a node

Posted: Wed Jun 11, 2014 2:44 pm
by Mark
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