|
Mailing Lists
|
Home /
Groups /
ColdFusion Talk (CF-Talk)
Cleaner way to retrieve a value from an XML node (XML-RPC)
Hi, folks.Cameron Johnson 06/30/09 09:35 A > I'm retrieving an XML packet from an API that uses the XML-RPC standardDave Watts 07/01/09 02:40 A Thank you, Dave.Cameron Johnson 07/01/09 12:28 P On Wed, Jul 1, 2009 at 12:23 PM, CamerCasey Dougall 07/01/09 02:06 P >Do you always know how many children will be returned for a specific node?Cameron Johnson 07/02/09 05:01 P > I see. Going by the names of the nodes makes sense, and it's easier toDave Watts 07/01/09 05:13 P Hi, folks. I'm retrieving an XML packet from an API that uses the XML-RPC standard (http://www.xmlrpc.com/spec). When I connect to the Login method of this API, I need to pull the session ID out of the packet. I can do that long-hand with this: <cfset local.loginResponseSID = local.requestXMLObject.XMLRoot.XMLChildren[1].XMLChildren[1].XMLChildren[1].XMLChildren[1].XMLChildren[1].XMLChildren[2].XMLChildren[1].XMLText /> But, there must be a cleaner way to do this, right? A note: the node that I am looking for is called <string>, BUT, if the login fails, there are multiple nodes called <string> in the response, so I can't do an XMLSearch for "//string[.]" and get just the one I need. FYI, the specific API that I am hitting is for iThenticate and the example is on page 16 of this PDF: https://ne.edgecastcdn.net/800404/static.ithenticate.com/static/build/media/e356a7e98a516945bba0af5293e8eb48cb_iThenticateAPIGuide.pdf) Any help to streamline this is much appreciated. Cameron > I'm retrieving an XML packet from an API that uses the XML-RPC standard > (http://www.xmlrpc.com/spec). First, have you looked at this? http://support.journurl.com/users/admin/index.cfm?mode=article&entry=362 ----- Excess quoted text cut - see Original Post for more ----- Almost certainly. In general, you can simply refer to XML elements as nested arrays and structures, using the names of the elements; if they're repeated within a single parent, you use array notation to get to the one you want. So, if you had a document like this in myXmlObj: <xmldoc> <child> <grandchild>value1</grandchild> </child> <child> <grandchild>value2</grandchild> </child> </xmldoc> you could get to "value2" using: myXmlObj.child[2].grandchild.xmlText (Actually, since it's nearly 3am, I could very well have a minor error in that, but the basic point stands.) Dave Watts, CTO, Fig Leaf Software http://www.figleaf.com/ Fig Leaf Software provides the highest caliber vendor-authorized instruction at our training centers in Washington DC, Atlanta, Chicago, Baltimore, Northern Virginia, or on-site at your location. Visit http://training.figleaf.com/ for more information! Thank you, Dave. >First, have you looked at this? >http://support.journurl.com/users/admin/index.cfm?mode=article&entry=362 Great! I was searching for a component like that, and I never came across this one; just the two old ones that the author references. ----- Excess quoted text cut - see Original Post for more ----- I see. Going by the names of the nodes makes sense, and it's easier to read/manage. In that case, I've got this: <cfset local.loginResponseSID = local.requestXMLObject.methodResponse.params.param.value.struct.member.value.string.xmlText /> Is that the simplest/cleanest that this is going to get? Much appreciated. Cameron On Wed, Jul 1, 2009 at 12:23 PM, Camer ----- Excess quoted text cut - see Original Post for more ----- Do you always know how many children will be returned for a specific node? If not you want to check this out. It will loop over children and you can work specifically with those items. http://www.bennadel.com/blog/1039-Ask-Ben-Iterating-Over-A-ColdFusion-XML-Document.htm >Do you always know how many children will be returned for a specific node? > >If not you want to check this out. It will loop over children and you can >work specifically with those items. > >http://www.bennadel.com/blog/1039-Ask-Ben-Iterating-Over-A-ColdFusion-XML-Document.htm Yes, but the number is different depending on the success or failure of the login. My instinct is to add logic to determine that success/failure first, then go find the node I need. Or, I'll see if I can use the technique in that blog to make it more compact. Thanks for pointing that out, Casey. (And thanks, Ben, for writing it). Cameron ----- Excess quoted text cut - see Original Post for more ----- Yes, that's as good as it'll get if that's the structure of your XML document, I think. Dave Watts, CTO, Fig Leaf Software http://www.figleaf.com/ Fig Leaf Software provides the highest caliber vendor-authorized instruction at our training centers in Washington DC, Atlanta, Chicago, Baltimore, Northern Virginia, or on-site at your location. Visit http://training.figleaf.com/ for more information!
|
March 21, 2010
|
Latest Fusion Authority Articles
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||