Skip to content

jbartlett777/XMLToStruct

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

XMLToStruct

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>

writedump(XMLToStruct(XML));
image

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));
image

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>

writedump(XMLToStruct(XML));
image

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>

writedump(XMLToStruct(XML));
image

About

Convert an XML to a ColdFusion structure

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published