Skip to main content
Participant
May 21, 2008
Question

XML element names vs CF variables names

  • May 21, 2008
  • 2 replies
  • 324 views
I am running CF 8 and using XMLParse to create an XMLDoc Object and referencing it in structure notation. When the XML contains elements that start with an underscore, references to that element throw an error. I have tried array notation and several other structure "hacks" I have seen on other blogs. I am certain this is a difference between what CF sees as a valid variable name and what XML allows.

I have attached code but for example and element name of <_element1> can not be referenced in CF as xmlobject["parentelement"]["_element1"].XmlText

Anyone have any work arounds? Is this a bug?
    This topic has been closed for replies.

    2 replies

    Inspiring
    May 23, 2008
    Another solution might be to apply an XSL transformation to your original XML file to rename the problem elements.
    mccreajaAuthor
    Participant
    May 27, 2008
    Thank you for the "workarounds". They all seem unnecessary, given that you should just be able to access the element by name. I really wanted to see if this is a bug as I planned to submit it to Adobe. In the meantime, the simplest solution for me is the access thru XMLChildren. However, if the XML ever changes that will be incorrect. Fortunately, in this case, we control the XML.

    Thanks,
    Jason
    Inspiring
    May 21, 2008
    I get the same thing you do (FWIW), and I'd agree it's a bug. And the same
    in both CFMX7.0.2 and CF8.

    What sort of workaround are you after? Refering to the node by its
    position in the child-node array of the parent works, as you demonstrate.

    You could also xmlSearch() for the node by name, and that works too, eg:

    <cfset a = xmlSearch(Variables.XMLObject, "/parent/child/_e1")>
    <cfdump var="#a#">
    <cfdump var="#a[1].xmlText#">

    --
    Adam