Professional Web Applications Themes

Looping through a structure with duplicate key names - Coldfusion - Getting Started

It may be that I'm barking up the wrong tree here, and there's a nice function that will do this, but I want to turn an XML doent returned to me via a SOAP call into a ColdFusion data structure. What I am getting returned is an array of elements (one for each group) with sub keys of "name" and "id". So I want to create a CF array, each element being a two keyed structure (id and name) with their corresponding values. Unfortunately, what I'm getting returned is a list of groups, each element called "Group", with "ID" and ...

  1. #1

    Default Looping through a structure with duplicate key names

    It may be that I'm barking up the wrong tree here, and there's a nice function
    that will do this, but I want to turn an XML doent returned to me via a SOAP
    call into a ColdFusion data structure. What I am getting returned is an array
    of elements (one for each group) with sub keys of "name" and "id". So I want
    to create a CF array, each element being a two keyed structure (id and name)
    with their corresponding values.

    Unfortunately, what I'm getting returned is a list of groups, each element
    called "Group", with "ID" and "Name" sub values. So I can't do a <cfloop
    collection="#SOAPresponse#" or <cfloop list="#StructKeyList( SOAPresponse )#"
    because each element is called "Group" so I get the correct number of array
    elements, but each one contains the details for the first group in the list.

    Is there a way I can loop through this without referring to the key name, or
    is there a function that will turn this hideous mess into a nice CF structure
    for me?

    Cheers,
    Phil

    Pseudo formatted return value from SOAP call:

    Group
    ID "1"
    Name "First Group"
    Group
    ID "2"
    Name "Second Group"
    Group
    ID "3"
    Name "Third Group"

    Loop code I'm trying to use:
    <cfset tmp = ArrayNew( 1 )>
    <cfloop collection="#SOAPresponse#" item="i">
    <cfset tmp2 = StructNew()>
    <cfset StructInsert( tmp2, "id", SOAPresponse[ i ][ "id" ][ "XMLText" ] )>
    <cfset StructInsert( tmp2, "name", SOAPresponse[ i ][ "name" ][ "XMLText" ] )>
    <cfset ArrayAppend( tmp, tmp2 )>
    </cfloop>

    And I get:

    1
    ID 1
    Name First Group
    2
    ID 1
    Name First Group
    3
    ID 1
    Name First Group

    Fychan66 Guest

  2. #2

    Default Re: Looping through a structure with duplicate key names

    Solved it with the following:

    <cfset tmp = ArrayNew( 1 )
    <cfloop from="1" to="#ArrayLen( SOAPresponse.XmlChildren )#" index="i">
    <cfset tmp2 = StructNew()>
    <cfset StructInsert( tmp2, "id", SOAPresponse.XmlChildren[ i ][ "id" ][
    "XMLText" ] )>
    <cfset StructInsert( tmp2, "name", SOAPresponse.XmlChildren[ i ][ "name" ][
    "XMLText" ] )>
    <cfset ArrayAppend( tmp, tmp2 )>
    </cfloop>

    Fychan66 Guest

Similar Threads

  1. looping over a structure using cfscript
    By zubair in forum Coldfusion - Advanced Techniques
    Replies: 5
    Last Post: November 29th, 06:35 PM
  2. Accessing duplicate field names from different tables
    By cakesy in forum Coldfusion Database Access
    Replies: 7
    Last Post: March 10th, 09:45 AM
  3. Data Source & Duplicate Names
    By SharondelaRey in forum Coldfusion - Getting Started
    Replies: 2
    Last Post: November 4th, 03:23 PM
  4. CFMX 7 Creating Duplicate Form Names
    By mooresm in forum Macromedia ColdFusion
    Replies: 0
    Last Post: May 1st, 03:49 AM
  5. Please Help... reffering to duplicate using names in array
    By exorș webforumsuser@macromedia.com in forum Macromedia Flash Actionscript
    Replies: 4
    Last Post: January 15th, 09:07 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139