Alex,
>I am on a project that requires me to loop over the attributes on an XML
>element in the exact order that they appear. For example:
>
>[ELEMENT name="Alex" cellphone="555-555-5555"/]
>
>When you access the XMLAttributes struct when you XMLParse the string, the
>'cellphone' attribute is presented first, because the structure is sorted
>alphabetically.
The xmlAttributes "property" is only a struct. I was hoping it might also
return a hidden array (so that you could do
oXml.parent.child.xmlAttributes[1], but alas that's not the case.)
Anyway, here are some possible solutions.
1) If you know you attribute list is relatively fixed (meaning you have
name, cellphone, etc) and you want them always printed in a specific manor,
you could create a list of the attribute names in the order you want them
processed. Then instead of looping through the xmlAttribute struct as a
collection, you loop through the list. Example:
<cfset lstAttributes = "name,cellphone">
<cfloop index="sAttrib" list="#lstAttributes#">
<cfif structKeyExists(oNode.xmlAttributes, sAttrib)>
<cfoutput>
#sAttrib# = #oNode.xmlAttributes[sAttrib]#<br />
</cfoutput>
</cfif>
</cfloop>
Obviously, you could use an array instead of a list if you want to include
label friendly descriptions.
2) If you have no idea what attributes are going to be present, but you need
to dump them out in a specific order you could use the toString() function
on the node to convert the node to it's original XML. You'll need to strip
out the <?xml version="1.0" encoding="UTF-8"?> declaration, but then you
could parse the tag to manually build a list of the attributes. Not very
elegant.
3) Use a 3rd party Java XML DOM parser like Xerces--which using the
Attributes interfaces should return the attributes in order.
-Dan
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Logware (www.logware.us): a new and convenient web-based time tracking
application. Start tracking and documenting hours spent on a project or with a
client with Logware today. Try it for free with a 15 day trial account.
http://www.houseoffusion.com/banners/view.cfm?bannerid=67
Message: http://www.houseoffusion.com/lists.cfm/link=i:4:226667
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/4
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:4
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54