Zurück   Flashforum > Flash > ActionScript > ActionScript 1

Antwort
 
LinkBack Themen-Optionen Ansicht
Alt 17-02-2006, 18:24   #1 (permalink)
Neuer User
 
Registriert seit: Oct 2002
Beiträge: 102
Lightbulb Die Mutter aller XML Parser ... ein Schritt weiter?

Hallo!

nunja, besser gehts immer

... aber ich habe trotzdem mal versucht, einen "XML 2 FlashObject" Parser zu schreiben, der über die Funktionalität von dem was ich bisher kannte etwas hinausgeht.

Folgendes sollte implementiert werden:
  • Automatisches Parsen fast beliebiger XML Dokumente in ein Flash Object
  • erhalten sämtlicher "Relationships", also FirstChild, LastChild, Parent, NextSibling bzw PreviousSibling im resultierenden Flash Objekt
  • Möglichkeit, auch "leere" Knoten in der Art <xml>This "has no node!"<node>This has!</node></xml> zu parsen
  • Automatisches Generieren von Arrays, wenn mehrere gleichnamige Objekte in einer Hierarchiestufe verwendet werden
  • Generierung von indizierten Arrays, wenn "id" Attribut vorhanden
  • Typenkonvertierung der Inhalte nach Boolean oder Number, wenn möglich
  • Umwandeln von ungültigen Nodenamen
  • Filtermöglichkeit von Nodes, z.B. soll ein "html" Node nicht weiter geparst werden

Im Resultat erhalte ich zwar eine komplexere Objektstruktur als "normal" (halt gespickt mit Referenzen) aber kann super iterieren und vor allem ist so jetzt auch eine Rückkonvertierung in XML wesentlich näher am Original, wenn nicht sogar identisch möglich. Alle "special nodes" sind übrigens auch gleich vor for...in Schleifen versteckt.

Und so schauts aus:

ActionScript:
  1. public function parse($node:XMLNode, $parent:Object):Object {
  2.        
  3.     // Parse XML data and returns corresponding Object
  4.    
  5.     do {
  6.        
  7.         var obj = new Object(); // create empty Object we parse into
  8.         var nName = $node.nodeName; // buffer Nodename
  9.         trace(nName+" < building node: "+" type: "+$node.nodeType);
  10.  
  11.         for (var i in $node.attributes) { // set Attributes
  12.             trace(nName+" < att: "+i+" - value: "+eval("$node.attributes."+i));
  13.             obj[i] = convertType($node.attributes[i]);
  14.         }
  15.        
  16.         switch (nName) { // node "Filter Section"
  17.        
  18.         case null : // It's a content Node, rename Nodename to "cNode"
  19.             nName = "cNode";
  20.             trace(nName+" < content: "+$node.nodeValue);
  21.             addHiddenAttribute(obj, "_ct", convertType($node.nodeValue)); // Assign Content we tried to convert to a Number or Boolean before
  22.             break;
  23.        
  24.         case "html" : // do not recurse into html, set as content instead
  25.             addHiddenAttribute(obj, "_ct", $node.toString());
  26.             break;
  27.        
  28.         default : // check if node has any Child Nodes
  29.             if ($node.firstChild != null ) {
  30.                 trace($node.firstChild.nodeName+" < recursing into - type: "+$node.firstChild.nodeType);
  31.                 obj._fc = true; // set flag for firstchild evaluation
  32.                 addHiddenAttribute(obj, "_lc", parse( $node.firstChild, obj)); // set "lastChild" Relation and Parse
  33.             }
  34.         }
  35.        
  36.         // set "parent, firstChild, nextSibling, previousSibling" Relations     
  37.         if ($parent != undefined) { addHiddenAttribute(obj, "_pt", $parent); }
  38.         if ($parent._fc == true)  { addHiddenAttribute($parent, "_fc", obj); }
  39.         if (oldObj != undefined)  { addHiddenAttribute(obj, "_ps", oldObj); addHiddenAttribute(oldObj, "_ns", obj); }
  40.         var oldObj = obj; // remember current object for setting "Sibling" Relations   
  41.  
  42.         // Now it's time to insert the newly generated Object in the Object Tree!
  43.        
  44.         var id = obj.id; // check for "id" Attribute and try to convert to Number, if possible
  45.         if (obj.id != undefined) { // id, so we have to create an indexed array
  46.             delete obj.id; // dont need this anymore
  47.             if (!isNaN(id)) { id = Number(id); } // try to convert id to Number, if possible
  48.             if ( $parent[nName] == undefined) { $parent[nName] = new Array; } // create Array if it doesn't exist
  49.             $parent[nName][id] = obj; // insert into indexed Array
  50.             continue; // continue do...while loop with next Sibling
  51.         }
  52.  
  53.         if ($parent[nName] == undefined) { // check if there is already another Object with the same name
  54.            
  55.             $parent[nName] = obj; // no, first Object to insert
  56.            
  57.         }  else { // another Object with the same name exists
  58.  
  59.                 if ($parent[nName].length == undefined) { // no id, does "regular" Array exist?
  60.                     var temp = $parent[nName]; // no, create Array first
  61.                     $parent[nName] = new Array();
  62.                     $parent[nName].push(temp);
  63.                 }
  64.                
  65.                 $parent[nName].push(obj); // push Object into regular Array
  66.             }
  67.  
  68.     } while ($node=$node.nextSibling);
  69.    
  70.     return obj; // return Object after no more Siblings to follow
  71. }
  72. private function setValidObjectName($name:String):String {
  73.     // replace "." and "-" in Nodenames with "_"
  74.     $name = $name.split(".").join("_");
  75.     $name = $name.split("-").join("_");
  76.     return $name;
  77. }
  78. private function convertType($value) { // tries to convert a String into a Number or a Booelan
  79.     if (isNaN($value)) { // first, check if Value is a Number
  80.         switch ($value)  { // no, check if Attribute can be converted to a Boolean
  81.         case "true"  : return true;
  82.         case "false" : return false;
  83.         default      : return $value; // no Boolean, return original value
  84.         }
  85.     } else {
  86.         return Number($value); // yes, assign converted Number
  87.     }
  88. }
  89. // the Next Function actuallay resides in the global Library
  90. private function addHiddenAttribute($object:Object, $att:String, $value:Object):Void {
  91.     // adds a hidden Property and assign a value to it
  92.     $object[$att] = $value;
  93.     var attArr = $att.split();
  94.     _global.ASSetPropFlags($object, attArr, 1, true);
  95. }

DOWNLOAD: Xml2linkObj.as.zip

TODO:
  • Erstellen einer Objektklasse, mit der man die verlinkte Objektstruktur bearbeiten kann
  • LinkObj 2 XML Parser schreiben

Wem ist langweilig Feedback, Bugreport und Tipps ebenfalls sehr willkommen!

Und noch ne Frage: kann man die Anzeige bestimmer Objekte auch im Debugger "verstecken" ???


Frank Kudermann
------------------------------------------
http://www.alphanull.de
frankiee ist offline   Mit Zitat antworten
Alt 18-02-2006, 05:19   #2 (permalink)
agedoubleju
Gast
 
Beiträge: n/a
Zitat:
kann man die Anzeige bestimmer Objekte auch im Debugger "verstecken"
Ja, mit ASSetPropFlags. Siehe z.B. hier...
  Mit Zitat antworten
Alt 19-02-2006, 11:18   #3 (permalink)
Neuer User
 
Registriert seit: Oct 2002
Beiträge: 102
Ja, schon klar, das habe ich im Code oben ja auch verwendet, um Attribute vor einer for..in Schleife zu verstecken. Hier geht es aber um das Verstecken im Debugger, so dass ich diese Variablen nicht mehr im entsprechenden Panel sehe. Habe bei ASSetPropFlags kein entsprechendes Flag dazu gefunden.

Geändert von frankiee (19-02-2006 um 16:44 Uhr) Grund: Typos
frankiee ist offline   Mit Zitat antworten
Antwort

Lesezeichen

Themen-Optionen
Ansicht

Forumregeln
Es ist Ihnen nicht erlaubt, neue Themen zu verfassen.
Es ist Ihnen nicht erlaubt, auf Beiträge zu antworten.
Es ist Ihnen nicht erlaubt, Anhänge hochzuladen.
Es ist Ihnen nicht erlaubt, Ihre Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks sind an
Pingbacks sind an
Refbacks sind an



Alle Zeitangaben in WEZ +1. Es ist jetzt 02:28 Uhr.

Domains, Webhosting & Vserver von Host Europe
Unterstützt das Flashforum!
Adobe User Group


Copyright ©1999 – 2012 Marc Thiele