Skip to main content
WolfShade
Legend
June 6, 2011
Question

CF9 and XML

  • June 6, 2011
  • 1 reply
  • 1522 views

Hello, everyone.

I've been working with CF for over 10 years, now, but I've never had to work with XML, before.  I'm totally new to this.

What I am appointed to do is design/develop a form that can be pre-populated with data by uploading an XML document.

Right now, what I have in place (mostly for testing) is a form that allows a user to upload an XML document; upon successful upload, I immediately read the contents of the file into a client variable, allowing the temp directory to delete the file after that is done.  It will then XMLparse() the contents and I'm using a CFDUMP to display.

What I need to do is loop through the data and populate the fields of another form.

Has anyone done something like this?  Any pointers, tips, tricks, etc.?  Are there any good tutorials for such a task that aren't either kindergarten level, nor "only-those-who-live-eat-breathe-shoot-s%*t-ColdFusion-can-understand" level?

Thanks,

^_^

    This topic has been closed for replies.

    1 reply

    Owainnorth
    Inspiring
    June 6, 2011

    Yup, best advice I can ever give someone who uses XML and CF is this - forget completely that it was ever XML.

    Get the file, call isXml() to check it's valid, validateXml() with a schema file if you're doing so, then xmlParse() it to a struct. From that point on, just forget it ever came from an XML document. People get too stuck in deep with what's a node, which are elements, where do I get the attributes from - just use CFDUMP then see what you need to do one by one.

    Oh look, my structure has a property called XmlAttributes, which is a struct. If you need one, get to it exactly as you would any other struct.

    I need to look through its children. Oh look, it has a property called XmlChildren, which is an array. Loop over it just like you would any other array.

    One of the fantastic things about ColdFusion is its ability to transform so many data types (file uploads, java objects, xml, webservices, file operations) into structs. That way, you don't particularly need to know about working with Xml if all you're looking to do is get a few values out. It's worth learning XPath if you're looking for particular values, but an hour or so reading through the W3 Schools on it should sort you out there.

    Just take it one step at a time, don't try and be too clever and you'll be surprised how easy you find it.

    O.

    WolfShade
    WolfShadeAuthor
    Legend
    June 6, 2011

    Thanks for the suggestion, Owain.  I am checking it with isXml(); if valid, I'm putting it to a client variable, then setting a variable to XMLparse(xml).

    But the xpath doesn't seem to be working, for me.  I'm picking the most common element in the xml (called "TransFunction") and trying to CFDUMP the results, but all I'm getting is "array [empty]".

         <Transaction size="3">
            <TransFunction AType="Add" BType="External" Comp="Low" Size="3"/>
            <TransFunction AType="Add" BType="External" Comp="Average" Size="4"/>
            <TransFunction AType="Add" BType="External" Comp="High" Size="6"/>
         </Transaction>

    <cfset xmlDoc = XMLParse(client.xmlVar)>

    <cfset trans = xmlSearch(xmlDoc, "//TransFunction")>

    <cfdump var="#trans#">

    Am I missing something?

    Thanks,

    ^_^

    Owainnorth
    Inspiring
    June 6, 2011

    Every time I've had XPath trouble, it's because your XML file is not using a default namespace. Is it using a custom one?