0

I am looking for a way to loop through an XML-Body and pass each xml element into a struct. i got access to the xml body in this way:

<cfset var x = arguments.httpRequest />
<cfset cont = xmlparse(x) />
<cfset var body = xmlsearch(cont, "//SOAP-ENV:Body")[1] />
<cfset var args = body.xmlchildren[1].xmlchildren />
<cfset xmlElements = {} />
<cfset xmlElements["#args[1].xmlName#"] = "#args[1].xmlText#" />

so i can get each element of args if it isn't a complexType. To access the children of the complex elements i used this way:

<cfset var children = "" />
<cfset children = args[3].xmlchildren />
<cfset children = args[1].xmlchildren />

XML for the third element looks like this:

<Element>
    <item>
        <child1>XP_RA_10</child1>
        <child2>RA-EXPRESS-KATALOG</Wmvtx>     
    </item>
</Element>

But i would like to create a method which does check if there are any xmlchildren or not. I tried to do it that way...

<cfif ArrayIsDefined(i.xmlchildren, 1)>
    <cfset children = args[i].xmlchildren />
    <cfif ArrayIsDefined(children[1].xmlchildren, 1)>
        <!--- if more xmlchildren exist --->
    <cfelse>
        <!if one xmlchildren exist --->
    </cfif>

<cfelse>
    <!--- if xmlchidren doesn't exist --->
</cfif>

...but i don't get access to the elements to insert them into the struct i created bevor. Always getting an error that the struct is undefined....

Is it the right way to check if there are any child-elements?

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
  • 1
    I've used [jsoup](http://jsoup.org/) against XML before - it's much easier than XPath and walking through structs and arrays - so might be worth considering. – Peter Boughton May 10 '12 at 13:39
  • great suggestion peter... I just didn't think it would give him the result (a flat struct) that he seemed to be looking for. – Mark A Kruger May 10 '12 at 16:30
  • it looks like jsoup is ja javalibrary, but i am programming with coldfusion and try to create that method inside a component. – Alex Cio May 11 '12 at 09:05

2 Answers2

0

unless you know the structure of the XML object ahead of time you will have to test each child object and traverse down the nodes till you know you have a simple object. To help there are the "is" functions as in:

isObject(var);
isStruct(var);
isArray(var);
isSimpleValue(variable);

Once you know you have an array (for example) you loop through it's index by length as in:

<Cfif isArray(children)>
<cfloop form="1" to="#arraylen(children)#" index="i">
      <cfset thisNode = children[i]/>
      <cfif isStruct(thisNode)?
         .... do something with the structkeylist.
      </cfif>
     <Cfif isArray(thisNode)>
        .... more looping...
     </cfif>
</cfloop>
</cfif>

This can be pretty daunting for really complex objects. I'm curious as to why you would do it? XML is designed to be "non-flat" - is there a specific requirement to flatten it?

Mark A Kruger
  • 7,183
  • 20
  • 21
  • I want to add each simple item to a struct, but befor i can do this have to loop through till i get the last item in the "tree". The xml don't has to be flat, but if there is one complex type anywhere in the tree it would be easier if a method would solve this problem for me instead of programming a specific one that just can be used on one file.... – Alex Cio May 11 '12 at 09:29
0

Is this what you were looking for? XmlToStruct on RIAForge

Russ
  • 1,931
  • 1
  • 14
  • 15
  • I just started building a function that will loop through a xml with different depths. But I didn't finish it because I started another project and it was just as an exercise for me. But this project looks like it could solve my problem. I wanted to get an output of everything inside my xml, does this project solve this problem without crashing when there are no children elements? – Alex Cio May 10 '13 at 08:22
  • As far as I can tell, it should work. You'll have to test it out yourself, of course. – Russ May 12 '13 at 00:05