Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

XML: sorting XML nodes

Guest
Oct 08, 2008 Oct 08, 2008
How would I sort a bunch of XML nodes? I don't necessarily need to change the existing structure, but I do need to iterate through in a specific order. In AS2 I would use the childNodes array and use Array.sort.
TOPICS
ActionScript
695
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 08, 2008 Oct 08, 2008
Perhaps something like this:

var xml:XML =
<root>
<node id="2">alpha</node>
<node id="3">delta</node>
<node id="5">bravo</node>
<node id="0">foxtrot</node>
<node id="1">echo</node>
<node id="4">charlie</node>
</root>

function XMLList2Array(l:XMLList):Array
{
var result:Array = [];
var n:XML
for each(n in l)
{
result.push(n.copy());
}
return result
}
function XMLListSort(l:XMLList, fieldName:String=null)
{
var a:Array = XMLList2Array(l);
a = fieldName==null ? a.sort() : a.sortOn(fieldName);
var result:XMLList = new XMLList();
for(var i:int=0; i<a.length; i++)
{
result += a ;
}
return result;
}
trace(XMLList2Array(xml.node),"\n================")
trace(XMLListSort(xml.node, "@id"),"\n================")
trace(XMLListSort(xml.node),"\n================")
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 08, 2008 Oct 08, 2008
LATEST
And here's a attribute sort function from
http://freerpad.blogspot.com/2007/07/more-hierarchical-sorting-e4x-xml-for.html.


var xml:XML =
<root>
<node id="2">alpha</node>
<node id="3">delta</node>
<node id="5">bravo</node>
<node id="0">foxtrot</node>
<node id="1">echo</node>
<node id="4">charlie</node>
</root>


sortXmlAttribute(xml,"id",true, Array.CASEINSENSITIVE);
trace(xml)

function sortXmlAttribute
( avXml :XML,
avAttributeName :String,
avPutEmptiesAtBottom :Boolean,
avArraySortArgument0 😘 = 0,
avArraySortArgument1 😘 = 0 )
:void
{
var lvChildrenCount:int
= avXml.children().length();

if( lvChildrenCount == 0 )
return;

if( lvChildrenCount > 1 )
{
var lvAttributeValue :String;
var lvXml :XML;

var lvSortOptions:int
= avArraySortArgument0 is Function
? avArraySortArgument1 : avArraySortArgument0;

var lvSortCaseInsensitive:Boolean
= ( lvSortOptions & Array.CASEINSENSITIVE )
== Array.CASEINSENSITIVE;

var lvArray:Array = new Array();

for each( lvXml in avXml.children() )
{
lvAttributeValue
= lvXml.attribute( avAttributeName );

if( lvSortCaseInsensitive )
lvAttributeValue
= lvAttributeValue.toUpperCase();

if( lvArray.indexOf( lvAttributeValue ) == -1 )
lvArray.push( lvAttributeValue );

} // for each

if( lvArray.length > 1 )
{
lvArray.sort
(
avArraySortArgument0,
avArraySortArgument1
);

if( avPutEmptiesAtBottom )
{
if( lvArray[0] == "" )
lvArray.push( lvArray.shift() );
} // if

} // if

var lvXmlList:XMLList = new XMLList();
for each( lvAttributeValue in lvArray )
{
for each( lvXml in avXml.children() )
{
var lvXmlAttributeValue:String
= lvXml.attribute( avAttributeName );

if( lvSortCaseInsensitive )
lvXmlAttributeValue
= lvXmlAttributeValue.toUpperCase();

if( lvXmlAttributeValue == lvAttributeValue )
lvXmlList += lvXml;

} // for each

} // for each

avXml.setChildren( lvXmlList );

} // if

for each( var lvXmlChild:XML in avXml.children() )
{
sortXmlAttribute
(
lvXmlChild,
avAttributeName,
avPutEmptiesAtBottom,
avArraySortArgument0,
avArraySortArgument1
);
} // for each

} // sortXmlAttribute

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines