Convert an XML to a ColdFusion structure
This function was more of an exercise after stumbling across a similar function on Ben Nadel's page to handle XML that also includes multiple elements and attributes. This function, however, does not hanlde an XML node that has child elements as well as node text.
Syntax:
XMLToStruct(XML, <CreateArrays [true|false]>)
<notes>
<sent>
<to>Mike</to>
<message>Please don't forget the donuts</message>
</sent>
<received>
<from>Roger</from>
<message>I'll look for them</message>
</received>
<received>
<from>Roger</from>
<message>Found them!</message>
</received>
</notes>XML Nodes that exist only once are not created as an array by default, but if true is passed as the second parameter to XMLToStruct, all nodes are created as arrays.
writedump(XMLToStruct(XML,true));

Taking a partial sample from Microsoft Learn's site that contains complex structures including attributes.
<PurchaseOrder OrderID="PO12345">
<Customer>
<Name>John Doe</Name>
<Address>
<Street>123 Main St</Street>
<City>Anytown</City>
<ZipCode>12345</ZipCode>
</Address>
</Customer>
<Items>
<Item ID="A001">
<Description>Laptop</Description>
<Quantity>1</Quantity>
<Price>1200.00</Price>
</Item>
<Item ID="B002">
<Description>Mouse</Description>
<Quantity>2</Quantity>
<Price>25.00</Price>
</Item>
</Items>
<TotalAmount Currency="USD">1250.00</TotalAmount>
</PurchaseOrder>The XML Attributes variable is defined as an array if the node is also of an array. If a node doesn't have attributes while others do, the array element is defined as a null. The function arrayIsDefined can be used to determine the existance of it.
writedump(XML);
<doc>
<item>a</item>
<item d="1">b</item>
<item>c</item>
</doc>

