Skip to content

Instantly share code, notes, and snippets.

@bitfade
Created September 18, 2010 08:22

Revisions

  1. bitfade renamed this gist Sep 22, 2010. 1 changed file with 0 additions and 0 deletions.
  2. bitfade renamed this gist Sep 22, 2010. 1 changed file with 0 additions and 0 deletions.
  3. bitfade revised this gist Sep 18, 2010. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions actionscript 3.0 xml parser
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    /*
    Author: bitfade

    this class will convert a XML configuration in to a simpler object

    this class will convert a XML configuration in to a simpler object
    */
    package bitfade.utils {

  4. bitfade created this gist Sep 18, 2010.
    54 changes: 54 additions & 0 deletions actionscript 3.0 xml parser
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    /*

    this class will convert a XML configuration in to a simpler object

    */
    package bitfade.utils {

    import flash.xml.*

    public class XmlParser {
    public static function toObject(xml:XML):Object {

    var obj:Object = {}
    var el:XML
    var name:String
    var value:Object

    // for each attribute
    for each(el in xml.@*) {
    name = el.localName()
    obj[name] = String(xml.attribute(name));
    }

    // if have text, add it
    value = xml.text().toString()
    if (value) obj.content = value

    // for each element
    if (xml.hasComplexContent()) {
    for each(el in xml.*) {
    name = el.localName()
    value = XmlParser.toObject(el)
    if (obj[name] is Array) {
    obj[name].push(value)
    } else {
    obj[name] = [value]
    }
    }
    }

    return obj

    }

    // strips namespaces for simpler parsing
    public static function clean(input:XML):XML {
    var purified:String = input.toString()
    purified = purified.replace(/<(\/)?(\w+:)(\w+)/g, "<$1$3");
    purified = purified.replace(/\w+:(\w+)=\"/g, "$1=\"");
    purified = purified.replace(/xmlns(:\w+)?=\"([^\"]+)\"/g, "");
    return new XML(purified)
    }
    }
    }