Extract XML names / pairs values ​​from different nodes in Coldfusion

I'm working on some Plesk integration using the XML API, and I'm trying to figure out how to parse the XML response that I am returning. Most of the data is fine, but http://download1.parallels.com/Plesk/PPP9/Doc/en-US/plesk-9.2-api-rpc/index.htm?fileName=28788.htm> Limits and Permissions are different friend. Essentially they are defined as follows:

<data> <limits> <limit> <name>foo</name> <value>bar</value> </limit> <limit> <name>foo2</name> <value>bar2</value> </limit> </limits> </data> 

How to extract "bar" from xml, given that I know that I need the value of "foo", but not the value of "foo2"?

+4
source share
2 answers
 <cfset my_xml = XmlParse(XMLCODE) /> <cfoutput> 1: #my_xml.data.limits[0].limit.value# <br />2: #my_xml.data.limits[1].limit.value# </cfoutput> 
+7
source

If you know exactly what the "name" of the limit is, you can use XPath for this. What you are looking for is a child of the "value" node of any terminal nodes where the name of the child node is "foo". In XPath, it looks like this:

 '/data/limits/limit[name = 'foo']/value' 

This will return an array of nodes (since there may be several matches), so we need to process the array. The whole example:

  <cfset myXML = " <data> <limits> <limit> <name>foo</name> <value>bar</value> </limit> <limit> <name>foo2</name> <value>bar2</value> </limit> </limits> </data> "> <!--- Parse the string into an XML Object ---> <cfset XMLDOM = xmlParse(myXML)> <!--- Search the XML Object for specific nodes ---> <cfset nodeArray = xmlSearch(XMLDOM,"/data/limits/limit[name = 'foo']/value")> <!--- Loop over the returned array of XML Node objects and print the XMLText (the node value) ---> <cfloop array="#nodeArray#" index="thisNode"> <cfoutput>#thisNode.xmlText#</cfoutput> </cfloop> 
+1
source

Source: https://habr.com/ru/post/1306418/


All Articles