0

I have this XML :

var myNode:XML = <node attribute="123"/>

I know how to add an attribute to the node :

myNode.@otherAttribute = "abc";

But this adds the attribute to the end. What if I want to add it as the first attribute ? I would like to have :

<node otherAttribute="abc" attribute="123"/>

I know it is possible to add a child node to a specific position with insertChildAfter() or insertChildBefore() but I don't know of it's possible for an attribute.

taskinoor
  • 45,586
  • 12
  • 116
  • 142
user611072
  • 11
  • 2

2 Answers2

3

If the order is important then you should be using elements not attributes. The XML recommendation says that attribute order is "not significant" (see section 3.1).

Note that the order of attribute specifications in a start-tag or empty-element tag is not significant.

Elements, on the other hand, do have a specified ordinal position within their parent.

But if I had to guess at a work around, I would say removing all attributes and then readding them in the order you want might work. But then again they may be emitted in alphabetical order as well, or even some other seemingly arbitrary order; it's behavior is non deterministic at best, and could change in future implementations of the API.

Even if you could do it in ActionScript, running it through any other XML tool, like an XSLT, then there is no guarantee that they will respect attribute order.

The right answer is "don't do that."

Also see this question for a similar point of reference.

To borrow from that answer, if you are dealing with a document where attribute order is relevant, then you aren't using XML -- just something that superficially looks like XML. Therefore, you should probably not be using XML based tools/libraries to process or generate that document.

Community
  • 1
  • 1
J. Holmes
  • 18,466
  • 5
  • 47
  • 52
  • Indeed the order of the attributes is not important for the code to process the XML, but we want the XML files to be easily human readable, so we want to always have the attributes in the same order. I was hoping there was a different way than deleting them all and adding them again in the right order, but I think I'll have to do that. – user611072 May 02 '12 at 16:31
  • @user611072 you would probably be best to use another visualization tool rather than having human beings reading raw xml documents. Just because XML is "human readable" -- in comparison to raw binary packing -- it doesn't mean that its the best way to consume that information. – J. Holmes May 02 '12 at 16:40
0

We had the same issue. The key was to use XML, not XMLNodes, and add ALL attributes with xml.@attributeName . (I've cut out and rewritten code so I can publish it here, but the original is working for sure)

var xml : XML = <Control/>;

xml.@name = pButton.aGetSkinName();
xml.@x = pButton.aGetSkinPosition().x;
xml.@y = pButton.aGetSkinPosition().y;          
xml.@text = pButton.textField ? pButton.textField.text : "";
[...]
xml.@enabled = pButton.enabled;
trace(xml)

and this traces:

<Control name="fullscreenButtonSkin" x="428" y="12" text="" [...] enabled="true"/>
csomakk
  • 5,369
  • 1
  • 29
  • 34