Skip to main content
Inspiring
January 9, 2011
Question

Flex XMLList to CFC

  • January 9, 2011
  • 1 reply
  • 1246 views

I have an XMLList dataProvider in Flex that I want to pass to a cfc and insert the data into the database.  How do I received the XMLList on the CF side and insert the data into the database?

my hierarchical XML is pretty basic:

<Nodes>

<Node level="0" tabID="1" tab="reports" cb="true">

     <Node level="1" tabID="11" tab="reports quality" cb="true">

          <Node level="2" tabID="21" tab="quality design" cb="true">

                 <Node level="3" tabID="33" tab="quality design summary" cb="true"/>

                 <Node level="3" tabID="32" tab="quality design reviews" cb="false"/>

          </Node>

     </Node>

</Node>

...

...

</Nodes>

I pass an XML string from Flex via RemoteObject call

Flex client side

private function saveRole():void{

var myObj:Object = new Object();

myObj.roleID = roleID.text;

myObj.role = role.text;

myObj.roleTabs = tabsXML.toXMLString();

cfdata.saveRoleUsers({mydata:myObj});

}

cfdata is the remoteObject call not shown

coldfusion cfc side

<CFFUNCTION NAME="SAVEROLEUSERS" ACCESS="REMOTE" RETURNTYPE="VOID">

<CFARGUMENT NAME="MYDATA" TYPE="STRUCT" REQUIRED="YES">

I need to read in #mydata.roleTabs# , which is the XML String and loop trough all nodes and insert

how do read the XML string and loop through it, and insert?

<CFQUERY NAME="INSERT" DATASOURCE="#DSN#">

   INSERT INTO ROLES_TABS (roleID,tabID,cb)

  VALUES (#mydata.roleID#,#?????#,#???#)

  </CFQUERY>

Secondly,  as a test I'm just trying to see what is contained in mydata.roleTabs on the cfc side.   I keep getting error message that say mydata.roleTabs is empty when I try to dump the variable to a PDF.  But, I see in the Flex Network monitor that the roleTabs XML string is there and being sent.  CF9 just isn't seeing it or my code is wrong?

<cfdocument format="PDF"  overwrite="yes" filename="xml.pdf">

<cfdump var="#mydata.roleTabs#">

</cfdocument>

or

<cfdocument format="PDF"  overwrite="yes" filename="xml.pdf">

<cfoutput>

'#mydata.roleTabs#'

</cfoutput>

</cfdocument>

So, I need to figure out why CF isn't getting the XML and they learn how to loop through it to insert.

I'd appreciate any assistance with either issue.

Thanks,

Don Kerr

    This topic has been closed for replies.

    1 reply

    Inspiring
    January 9, 2011

    Well, I solved one of the issues.  I can now see the xml in cfc

    <cfset ar = #xmlsearch(MYDATA.ROLETABS,'Nodes/Node')#>

    <cfdocument format="pdf"  overwrite="yes" filename="xmldoc.pdf">

    <cfdump var="#ar#">

    </cfdocument>

    I had to modify the flex side to get the xml string formatted correctly

    myObj.roleTabs =  '<Nodes>'+tabsXlc.toXMLString().toString()+'</Nodes>';

    Now, how do I loop through the xmlsearch to do the insert into the database?

    Don

    Inspiring
    January 10, 2011

    Hi,

    A Simple example is given in the Adobe site.

    Its given below.

    This is the XML,

    <?xml version="1.0" encoding="UTF-8"?> 
    <employee>
    <!-- A list of employees -->
        <name EmpType="Regular">
    <first>Almanzo</first>
    <last>Wilder</last>
        </name>
        <name EmpType="Contract">
    <first>Laura</first>
    <last>Ingalls</last>
        </name>
    </employee>

    <cfscript> 
        myxmldoc = XmlParse("C:\CFusionMX7\wwwroot\examples\employeesimple.xml");
        selectedElements = XmlSearch(myxmldoc, "/employee/name/last");
        for (i = 1; i LTE ArrayLen(selectedElements); i = i + 1)
            writeoutput(selectedElements.XmlText & "<br>");
    </cfscript>

    In this SelectedElements is an array and it will contain the number of arrays of"employee/name/last and
    in this case it will have 2; from that you can get the exact XMLtext and insert into your database.

    I hope it will be helpful
    Inspiring
    January 10, 2011

    I do appreciate the input and will flag it as helpful.  Unfortunately, as with most Adobe examples, this one is oversimplified. I've seen many like this with one level XML. Hierarchical XML (nested nodes) is a different beast that requires drilling down the tree and looping through each children set.  This was the part that hung me up.

    However, I do now have a working solution, but it is most likely not optimal.  But it works.

    In my case, I have a property called level on each node which tells me which level in the tree the node lives.

    So, I was able to grab the nodes at each level using xmlsearch, like this:

    <cfset ar3 = #xmlsearch(MYDATA.ROLETABS,'//Node[@level=3]')#>

    Then I looped through each level's flat results and did the insert

    <cfloop index="nodeXML3" array="#ar3#">

       <cfset nodeXML3 = xmlparse(nodeXML3)>

    <CFQUERY NAME="INSERT" DATASOURCE="#DSN#">

       INSERT INTO ROLES_TABS (roleID,tabID,cb)

    VALUES (

    <cfqueryparam cfsqltype="cf_sql_integer" value="#RID#"/>,

    <cfqueryparam cfsqltype="cf_sql_integer" value="#nodeXML3.Node.XmlAttributes.tabID#"/>,

    <cfqueryparam cfsqltype="cf_sql_bit" value="#nodeXML3.Node.XmlAttributes.cb#"/>

    )

       </CFQUERY>

    </cfloop>

    This was cheating.  It is not ideal to repeat the search four times, but it worked.  A better solution would be to insert every node at every level with only one xmlsearch using the XmlChildren, for the cases where you don't know how many levels you have in the tree.

    The fire is out for my deadline, but if anyone has a better way, please share.

    Thanks!

    Don