Skip navigation
Currently Being Moderated

Maintain attribute order in XML with a structure?

Dec 5, 2011 6:23 PM

Tags: #xml #structure #attributes #ordering #linkedhashmap

I'm using xmlParse() to read an XML file into a structure, and then process the structure (replace some of the XML attributes) and then write the structure back out as an XML file. The problem is that I loose the original ordering of XML attrbutes when I convert to a structure, and instead end up with a new attribute order for each element that is alphabetical.

 

In otherwords: <element c="text" d="text" a="text" />

gets rewritten as: <element a="text" c="text" d="text" />

which is a problem for this application.

 

Is there a way to work with XML in CF but maintain attribute ordering (LinkedHashMap instead of a structure, maybe)?

 

Thanks.

Walter

 
Replies
  • Currently Being Moderated
    Dec 6, 2011 5:00 AM   in reply to walter@whardy.com

    If you need to control the order of the attributes, you'll need to write your xml out by hand... according to the XML specification, attribute ordering must not be significant, so strictly-speaking your requirement here requires something of XML that's contrary to its intent.  And, accordingly, there's no way of making CF respect something that's intrinsically not supposed to be respected.

     

    NB: "I'm using xmlParse() to read an XML file into a structure,"... xmlParse() creates an XML object, not a struct, so what you're asking in your last para doesn't really make sense.  xmlParse() will only create an XML object. If you want to read the XML data as something other than XML, you'll need to write your own function.

     

    The best solution here, if poss, is to remove the significance of the attribute order in your app, because it's "wrong" to infer any ordering, and you're making a bit of a logical rod for your own back in relying on this ordering.

     

    Not a "just do it like this" response, sorry, but that's what one gets when the question is submerged in a can of worms... ;-)

     

    --
    Adam

     
    |
    Mark as:
  • Currently Being Moderated
    Dec 7, 2011 5:17 AM   in reply to walter@whardy.com

    but using ColdFusion to process XML in this way is making a pretty big change in the XML structure.

     

    No, it's not changing the structure in any meaningful way: the attribute ordering isn't meaningful in XML, so if the order of them changes, then that's not a meaningful change.  In any way that XML should be used, it doesn't matter.

     

    When CF reads an string (like file contents) and parses it as XML, it needs to create an XML object; that XML object will adhere to the rules of XML; one of which is "atribute order has no significance", so it will not (and ought not) bother to "remember" the order that the attributes were in in the original string.  When one converts that object back into a string, some sort of ordering needs to be used to write them back, and CF seems to arbitrarily use alphabetical order.  But it cannot refer back to information that it doesn't have (the original ordering), so the original ordering can't be preserved.

     

    There's nothing unreasonable or that shouldn't be expected in this process, unfortunately (for you, I mean).

     

    --

    Adam

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 14, 2012 7:19 AM   in reply to walter@whardy.com

    I'm having a very similar problem, except in my case if for human readability.  I have a property called number, and it would be nice if that was displayed first, and not alphabetically towards the end of the list of properties.  Just hard to double check everything on screen when the order is all moved around when using <CFXML>

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 14, 2012 7:21 AM   in reply to pixelwiz19801

    As has been mentioned, it's just not how XML works. Does XSLT allow for ordering?

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 14, 2012 7:25 AM   in reply to Owain North

    I understand that, but it only seems to be a problem with coldfusion.  Like our vendor can send us an XML feed and the attributes in it are not order alphabetically so it makes it really easy to read.  But when I send back the same feed with updated info all the parameters are moved around alphabetically.  Everything still works, so that's not the issue, but during testing it's just really hard to read and compare the feeds.  I thought the whole original purpose behind XML was to make a format that allowed for easy data sharing that was easily human readable.  Oh well, hopefully they can add some attribute in CF10 to say don't change the order that's explicitly specified when the XML is created.

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 14, 2012 7:32 AM   in reply to pixelwiz19801

    hopefully they can add some attribute in CF10 to say don't change the order that's explicitly specified when the XML is created.

    It would appear not.

     

    I'd be surprised if anyone made a change like that, as it's not a requirement of XML it's more that your other systems "happen" to display them in order; they equally could not and still be valid. XML was never meant to be displayed as such, and as far as I know it's only human-readable as a by-product of it being a text-based standard; I'm not sure this was so it could be "easily read" by people.

     

    Generally you'd parse the XML and display it in your own format, so ordering wouldn't be an issue.

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 14, 2012 7:32 AM   in reply to pixelwiz19801

    Ah, found a solution on another thread.  Using CFSAVECONTENT instead of CFXML totally resolved this issue for me.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points