Skip to main content

Getting XML Text From XML Nodes

I've been working with XML very heavily the past couple of weeks, and for whatever reason this is just one of those things I haven't had to do a whole lot of until now. Don't get me wrong, XML comes up pretty regularly for most people, but I haven't had to live and breathe it this much before.


As with most things in ColdFusion it's incredibly easy to jump in and get going, and for what I'm doing anyway, XmlSearch makes it absolutely trivial to tear through the XML and get the data I need.


I did run into one annoyance and thought I'd see if anyone had a better solution. Again, since I seem to have dodged working with XML this heavily until now I could just be missing something. Specifically I'm referring to the seeming necessity of two lines to get at most things when you use XmlSearch. If you've worked with XML much in CF you probably already know what I mean. Let's assume there's a single node (meaning really a single element array with an XML node in it) returned by this:

<cfset myXmlNode = XmlSearch(myDoc, "/*/myNode") />


To get at the XmlText, I then have to do this:

<cfset myXmlText = myXmlNode[1].XmlText />


What I'd like to be able to do is this:

<cfset myXmlText = XmlSearch(myDoc, "/*/myNode")[1].XmlText />


But unfortunately that syntax isn't valid. To solve the problem I wrote a little UDF called getXmlTextFromXmlSearch (couldn't think of a longer name ;-) that takes in the array and the element number from which you want the XmlText. It works, but I'm a tad irked that this seems to be necessary. What CF could use IMO is an ArrayGetAt *function* so you could wrap arrays in the function instead of having to jump through these other hoops:

<cfset myXmlText = ArrayGetAt(XmlSearch(myDoc, "/*/myNode"), 1).XmlText />


That's essentially what my UDF does but again, seems a bit klunky. Maybe I'm just nitpicking because ColdFusion is so great and I want it to be perfect. ;-)


Comments



I'm in the same boat - I barely use XML but here's what I came up with for an XPath expression to get the xml text:


hi


I'm not sure if it's the perfect solution but I do know XPath is pretty powerful stuff (and I've yet to find a *really* good resource for it).





crud...looks like you stripped my code...let me try this:


<cfxml variable="test">


<nodes>


<node>hi</node>


</nodes>


</cfxml>


<cfset text = xmlSearch(test, "string(//nodes/node[text()])") />


<cfdump var="#text#">





Thanks Todd--I'm a bit of an XPath moron so I'll need to look into that more.





You could also do this:





<cfset myXmlText = XmlSearch(myDoc, "/*/myNode").get(0).XmlText />





Ah--didn't think of tapping into the Java that way. That's very slick! Thanks radek!





This will work as well





I wasn't sure how to put code in a comment






or


#HTMLEditFormat( '')#





Sorry Qasim--I need to tidy up the code handling in the comments!





i don't think it;s the best solution





not sure how i happened upon this, as i'm in virtually the same boat this morning, and your post is already indexed in google - well done!


anyhow, radek is right, arrayGetAt IS array.get(index), and fwiw, arrayFind would be array.indexOf(string)


@todd - I think that the xPath functions such (as string()) are only available in CF8, but I have no way to test on 7 atm.


this xmlSearch will expose whether a function is avail in CF xPath: xmlSearch(search, "function-available('testthisfunction')")


My situation was a *little* different, I wanted to get an array of just the text values in a particular node.


mytext


xmlSearch(search, '//nodes/node/text()') returns an array that looks like this:


[{xmlName='#text',xmlType='TEXT',xmlValue='mytext'},{xmlName='#text',xmlType='TEXT',xmlValue='mytext'}]


and i coulnd't for the life of me figure out how to get the xpath search down to just xmlValue...becasue it appears that CF is building that DOM node structure back in to the xmlSearch response. so, xPath finds the text, gives it back to CF, and CF goes 'well, hell, this looks like xml, so let's send it back as native CF XML DOM node structure. as it turns out, in CF8, at least, if i output arr[1], i get a string, and not the XML object shown in the dump of the array.



Comments

Popular posts from this blog

Running a Django Application on Windows Server 2012 with IIS

This is a first for me since under normal circumstances we run all our Django applications on Linux with Nginx, but we're in the process of developing an application for another department and due to the requirements around this project, we'll be handing the code off to them to deploy. They don't have any experience with Linux or web servers other than IIS, so I recently took up the challenge of figuring out how to run Django applications on Windows Server 2012 with IIS.

Based on the dated or complete lack of information around this I'm assuming it's not something that's very common in the wild, so I thought I'd share what I came up with in case others need to do this.


This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Assumptions and CaveatsThe operating system is Windows Server 2012 R2, 64-bit. If another variant of the operating system is being used, these instructions may not work properly.All of the soft…

Installing and Configuring NextPVR as a Replacement for Windows Media Center

If you follow me on Google+ you'll know I had a recent rant about Windows Media Center, which after running fine for about a year suddenly decided as of January 29 it was done downloading the program guide and by extension was therefore done recording any TV shows.

I'll spare you more ranting and simply say that none of the suggestions I got (which I appreciate!) worked, and rather than spending more time figuring out why, I decided to try something different.

NextPVR is an awesome free (as in beer, not as in freedom unfortunately ...) PVR application for Windows that with a little bit of tweaking handily replaced Windows Media Center. It can even download guide data, which is apparently something WMC no longer feels like doing.

Background I wound up going down this road in a rather circuitous way. My initial goal for the weekend project was to get Raspbmc running on one of my Raspberry Pis. The latest version of XBMC has PVR functionality so I was anxious to try that out as a …

Fixing DPI Scaling Issues in Skype for Business on Windows 10

My setup for my day job these days is a Surface Pro 4 and either an LG 34UC87M-B or a Dell P2715Q monitor, depending on where I'm working. This is a fantastic setup, but some applications have trouble dealing with the high pixel density and don't scale appropriately.
One case in point is Skype for Business. For some reason it scales correctly as I move between the Surface screen and the external monitor when I use the Dell, but on the LG monitor Skype is either massive on the external monitor, or tiny on the Surface screen.
After a big of digging around I came across a solution that worked for me, which is to change a setting in Skype's manifest file (who knew there was one?). On my machine the file is here: C:\Program Files\Microsoft Office\Office16\LYNC.EXE.MANIFEST
And the setting in question is this:
<dpiAware>True/PM</dpiAware>
Which I changed to this: <dpiAware>False/PM</dpiAware>
Note that you'll probably have to edit the file as administr…