|
load the file for all format
// System.useCodepage = true; var my_xml:XML = new XML(); my_xml.ignoreWhite = true; var debug_:String; var XMLED = false; my_xml.onLoad = function(success:Boolean) { if (success) { if (my_xml.status == 0) { xl = my_xml.firstChild.childNodes; //trace([ev, ev.length]); //trace("XML was loaded and parsed successfully"); debug_ = "XML was loaded and parsed successfully"; if (my_xml.loaded) { //trace("successfully: "+this.loaded); XMLED = true; } } else { //trace("XML was loaded successfully, but was unable to be parsed."); debug_ = "XML was loaded successfully, but was unable to be parsed."; } var errorMessage:String; switch (my_xml.status) { case 0 : errorMessage = "No error; parse was completed successfully."; break; case -2 : errorMessage = "A CDATA section was not properly terminated."; break; case -3 : errorMessage = "The XML declaration was not properly terminated."; break; case -4 : errorMessage = "The DOCTYPE declaration was not properly terminated."; break; case -5 : errorMessage = "A comment was not properly terminated."; break; case -6 : errorMessage = "An XML element was malformed."; break; case -7 : errorMessage = "Out of memory."; break; case -8 : errorMessage = "An attribute value was not properly terminated."; break; case -9 : errorMessage = "A start-tag was not matched with an end-tag."; break; case -10 : errorMessage = "An end-tag was encountered without a matching start-tag."; break; default : errorMessage = "An unknown error has occurred."; break; } //trace("status: "+my_xml.status+" ("+errorMessage+")"); debug_ = "status: "+my_xml.status+" ("+errorMessage+")"; delete my_xml; } else { //trace("Unable to load/parse XML. (status: "+my_xml.status+")"); debug_ = "Unable to load/parse XML. (status: "+my_xml.status+")"; } }; // Posted by: ooxford | website http:// |
String.prototype.searchReplace=function(find,replace) {
return this.split( find ).join( replace);
}
String.prototype.replaceExtended = function(){
/*
Only vocals ar threated now, I will extend it
It works only if you use è instead
of 'è' into your xml source (for example)
*/
temp = this.searchReplace( "à", "à" );
temp = temp.searchReplace( "á", "á" );
temp = temp.searchReplace( "À", "À" );
temp = temp.searchReplace( "Á", "Á" );
temp = temp.searchReplace( "è", "è" );
temp = temp.searchReplace( "é", "é" );
temp = temp.searchReplace( "È", "È" );
temp = temp.searchReplace( "É", "É" );
temp = temp.searchReplace( "ì", "ì" );
temp = temp.searchReplace( "í", "ì" );
temp = temp.searchReplace( "Ì", "Ì" );
temp = temp.searchReplace( "Í", "Í" );
temp = temp.searchReplace( "ò", "ò" );
temp = temp.searchReplace( "ó", "ó" );
temp = temp.searchReplace( "Ò", "Ò" );
temp = temp.searchReplace( "Ó", "Ó" );
temp = temp.searchReplace( "ù", "ù" );
temp = temp.searchReplace( "ú", "ú" );
temp = temp.searchReplace( "Ù", "Ù" );
temp = temp.searchReplace( "Ú", "Ú" );
return temp;
}
TextField.prototype.loadAndFormatXML = function(xmlUrl, cssUrl, wrap){
var txtFld = this;
//Prepare the TextField object
this.html = true;
this.multiline = true;
this.wordWrap = wrap;
//Create the CSS object (MX2004 is needed!!!)
s = new TextField.StyleSheet();
s.load( cssUrl );
s.onLoad = function( ok ){
if( ok )
txtFld.styleSheet = s;
}
//Create the XML object
x = new XML();
x.load( xmlUrl );
x.onLoad = function( ok ){
if( ok ){
_txt = x.toString();
_txt = _txt.replaceExtended();
txtFld.text = _txt;
}
}
}
Posted by: Federico Maggi | website http://www.someurl.org |
If you are accessing XML tags using childNodes, try using this code. It creates dynamic properties that allow you to access the XML tags in a more human readable form such as myXML.catalog.page.item[3].price
Here's an example including the XML and flash source code:
XML Example:
<?xml version="1.0" encoding="iso-8859-1" ?> <PRODUCE> <FRUIT> <APPLE> <KIND>Delicious</KIND> <COLOR>Red</COLOR> <PRICE> <AMOUNT>.79</AMOUNT> <PER>pound</PER> </PRICE> </APPLE> <PEAR> <KIND>Golden</KIND> <COLOR>yellow</COLOR> <PRICE> <AMOUNT>.99</AMOUNT> <PER>pound</PER> </PRICE> </PEAR> </FRUIT> </PRODUCE> // DESCRIPTION: The following routines reparse the XML structure and add routines that allow
// access to nodes to be much more human readable.
//
// EXAMPLE: Instead of using: myXML.firstChild.childNodes[5].childNodes[3].description.firstChild.nodeValue;
// Now you can use: myXML.catalog.page[5].item[3].description.firstChild.nodeValue;
//
// NOTES: Since the XML structure is parse a second time, there may be a performance hit. On a
// - 1G machine, I did not notice any with roughly 100 elements.
// - The structure created by XML.parseXML is preserved, these routines merely enhance it.
// - XML.parseXML MUST be called before extendXML.
// - This routine does NOT cleanup after itself (beware of reusing the XML object).
// - Also, it has only been tested for XML source (not HTML etc.).
// - The following can NOT be used for generated XML tags:
// attributes, childNodes, contentType, docTypeDecl, firstChild, ignoreWhite,
// lastChild, loaded, nextSibling, nodeName, nodeType, nodeValue, onLoad,
// parentNode, previousSibling, status, xmlDecl, nodeFuncs (there may be others).
// - Read below about how DUPLICATE TAGS are handled.
// - It has been reported to me that attributes work similar to the following:
// myXML.catalog.page[5].item.attributes.description;
//
// AUTHOR: Mark Britt ( CreateN )
//
// CHANGE LOG: If you make any improvements/bug fixes, I would appreciate a copy, thanks : )
//
XML.prototype.traverseNodes = function ( node ) {
var i = 0;
var children = node.childNodes; //required for performance issues (interpreter recalling childNodes)
if ( node.nodeFuncs == undefined ) { node.nodeFuncs = new Object; } // add array for addProperty functions
while ( children[i] ) {
// DUPLICATE TAGS: We need to handle childNodes that contain duplicate nodeName entries
// The following works only if all nodeNames are equal for the entire childNodes list
// this is NOT a final solution, modify as needed
var flag = true;
if ( children.length > 1 && children[0].nodeName == children[1].nodeName ) {
// for now, we assume all children are equal
if ( i == 0 ) {
// Only addProperty for the first node
node.nodeFuncs[0] = function () { return node.childNodes; };
} else {
flag = false;
}
} else {
node.nodeFuncs[i] = function () { return this.childNodes[arguments.callee.index]; };
// saving 'index' allows the interpreter to find the node when calling the 'nodeFunc'
node.nodeFuncs[i].index = i; // save the node index with the associated function
}
if ( flag == true ) {
// addProperty for each node except duplicate nodes
if ( children[i].nodeName.length && node.addProperty( children[i].nodeName, node.nodeFuncs[i], null ) ) {
//trace ("Property:"+child.nodeName+" added");
}
}
if ( children[i].childNodes.length > 0 ) {
this.traverseNodes( children[i] );
}
i++;
}
//trace("traverseNodes");
}
XML.prototype.extendXML = function () {
if ( this.childnodes.length > 0 ) {
this.traverseNodes( this );
//trace("extendXML");
}
}
XML.prototype.onData = function ( src ) {
if ( src == undefined ) {
this.onLoad( false );
} else {
this.parseXML( src );
this.extendXML();
// OR this.traverseNodes( this ); and eliminate extendXML
this.loaded = true;
this.onLoad( true );
}
}
function myOnLoad(success) {
if (success) {
trace ("Load succeeded!");
// EXAMPLE Access to the new XML properties
var result1 = this.produce.fruit.apple.price.amount.firstChild.nodeValue;
// result1= .79
var result2 = this.produce.fruit.apple.price.per.firstChild.nodeValue;
// result2= pound
} else {
trace ("Load failed!");
}
}
var myXML = new XML();
myXML.onLoad = myOnLoad;
myXML.ignoreWhite = true;
myXML.load( "file.xml" );
Posted by: Mark Britt | website http://www.brittmd.com |
XML.prototype.prettyPrint = function(spacer) {
spacer = (spacer == undefined) ? "\t" : spacer;
var xml_str = this.toString();
var open, closed, tabs, output, node_str, eol;
output = "";
open = xml_str.indexOf("<", 0);
var tab_next = false;
while (open != undefined and open != -1) {
if (open != -1) {
closed = xml_str.indexOf(">", open+1);
node_str = xml_str.substring(open, closed+1);
if (tab_next) {
tabs++;
tab_next = false;
}
if (node_str.indexOf("/") == 1) {
tabs--;
} else if (node_str.lastIndexOf("/") != node_str.length-2) {
tab_next = true;
}
ts = "";
for (var j = 0; j<tabs; j++) {
ts += spacer;
}
if (open == last_closed+1) {
eol = "\n"+ts;
} else if (open>0) {
eol = xml_str.substring(last_closed+1, open);
}
output = output+eol+node_str;
}
open = xml_str.indexOf("<", open+1);
last_closed = closed;
}
return output;
};
//This is a fast "pretty print" function for the XML object.
//It basically just prints out the XML object in a structured fashion with tabbed indents.
//usage: trace(myXMLObj.prettyPrint()); - for tabbed indents
//or: trace(myXMLObj.prettyPrint(" ")); - for tabbed custom-specified indents
Posted by: Ken Goulding | website http://www.eyeriss.com |
/** *@author: By Eric Feminella *@published: 06.16.05 *@class: RSS1Parser Version 1.0 *@copy: http://www.ericfeminella.com */ class dataProviders.rss.RSS1Parser extends XML { private var src:String; private var parent:MovieClip; private var tf:TextField; private var articles_tf:TextField; public function RSS1Parser(src:String, parent:MovieClip) { this.src = src; this.parent = parent; this.tf = parent.tf; this.articles_tf = parent.articles_tf; this.ignoreWhite = true; this.loadStyles(); } public function requestFeed():Void { this.tf.htmlText = "Requesting feed @url:\n" + this.src + " please wait..."; this.load(this.src); } private function onLoad(success:Boolean):Void { this.tf.htmlText = ""; if (success) { parseRSS(this); } else { this.tf.htmlText = "An load error occured in the request. Please try again"; } } private function parseRSS(node:XMLNode):Void { var linkNode:Array = node.firstChild.firstChild.childNodes; for (var i:Number = 0; i <= linkNode.length; i++) { if (linkNode[i].firstChild.nodeName == "title") { var obj:Object = {}; for (var j:Number = 0; j < linkNode[i].childNodes.length; j++) { var itemNode:XMLNode = linkNode[i].childNodes[j]; switch(itemNode.nodeName) { case "title": obj.title = itemNode.firstChild.nodeValue; break; case "description": obj.description = itemNode.firstChild.nodeValue; break; case "link": obj.url = itemNode.firstChild.nodeValue; break; } } this.createItem(obj); } this.articles_tf.text = "Total articles Recieved: " + i; } } private function createItem(obj:Object):Void { this.tf.htmlText += "<a href='" + obj.url + "' target='_blank'><b>" + obj.title + "</b></a><br/>"; this.tf.htmlText += "<span class='desc'>" + obj.description + "</span><br/><br/>"; } public function set setSource(src:String):Void { this.src = src; } public function get getSource():String { return this.src; } private function loadStyles():Void { var ref = this; var css:TextField.StyleSheet = new TextField.StyleSheet(); css.onLoad = function(true:Boolean):Void { if (true){ ref.tf.styleSheet = this; } } css.load("styles.css"); } } Posted by: Eric Feminella | website http://www.ericfeminella.com |

