varoon

How to read a specific node in xml in Flash AS ?

Recommended Posts

Hi

 

 

 

how to load a specific node from a xml and pass it to chart....

 

 

 

Need to do in FLASH Action Script....

 

 

 

any one tried ??? is it possible ?

 

 

 

xml is given in attachment....

 

 

 

2rpfyar.png

sample.xml

post-2173-128441568983_thumb.png

Share this post


Link to post
Share on other sites

Hi Varoon,

It can be easily done using Action Script.

I have written a fucntion (using AS2) that can do it. Please find the attached fla file with the function.

The fla file alos contains 2 TextArea (xml & chart)

In xml textArea you would need to place the full XML which has <chart> node(s) somewhere in the XMLtree. There is button which when clicked would extract all the <chart> nodes from the XML textarea and place them as string in chart textarea.

For this i have written a function -getNodesByName(node, node_name)

Pass an XML or a node or XML as sting in the first parameter - node and

Pass the name of the node (as string) to be searched for in the paremeter - node_name.

[so we pass "chart" here]

This function would return an Array containing all the matched nodes.

You would need to iterate through the array and take the appropriate array element (i.e. chart node.)

AS2 function:

/**

*

* @function getNodesByName

* @author InfoSoft Global (P) Ltd. www.InfoSoftGlobal.com

* @version 0.9.1

*

* Extracts all Nodes with a specific name

* from an XML/XMLNode and returns an array of Nodes

*

* @param node  XMLNode/XML that is searched.

* @param node_name Name of the name to extract.

* @return An Array of Nodes.

*

*/

function getNodesByName(node, node_name:String){

 //if the node is of stirng type conver to XML object

   if(typeof node=="string"){

node=new XML(node);

  }

  // create a blank array to store all matched nodes

  var arr_matched_nodes_collection=[];

 

// when a matched node is found add to array

  if(node.nodeName.toLowerCase()==node_name){

arr_matched_nodes_collection.push(node);

  }else{

// otherwise search child nodes for match (in recursion)

if(node.hasChildNodes()){

   //itetate through all child nodes

   for(var index=0; index<node.childNodes.length; index++){

  // call itself to find searched node in the child nodes 

// until no child node is found

var arr_matched_nodes=getNodesByName(node.childNodes[index],node_name);

// if the array returned has some element, it contained 

// a matched node

if(arr_matched_nodes.length!=0){

   //add the matched node to the array collection

   arr_matched_nodes_collection.push(arr_matched_nodes); 

}

   }

}

  }

  //return the array containing all matched nodes

  return arr_matched_nodes_collection;

}

getChartXML.zip

Share this post


Link to post
Share on other sites

I dont know how could you implement this in Fusions chart but maybe this sample can give you clue:

 

 

 

//declare what node you are searching for. in this case it's attribute in Node

 

_global.searched="second";

 

 

 

function loadXML(loaded) {

 

if (loaded) {

 

xmlNode = this.firstChild;

 

 

 

total = xmlNode.childNodes.length;

 

for (i=0; i

 

if(xmlNode.childNodes.attributes.id == _global.searched){

 

 

 

heading = xmlNode.childNodes.childNodes[0].firstChild.nodeValue;

 

description = xmlNode.childNodes.childNodes[1].firstChild.nodeValue;

 

}

 

}

 

showMatchedNode();

 

} else {

 

content = "file not loaded!";

 

}

 

}

 

xmlData = new XML();

 

xmlData.ignoreWhite = true;

 

xmlData.onLoad = loadXML;

 

 

 

xmlData.load("data.xml");

 

 

 

function showMatchedNode(){

 

 

 

your_dynamic_textfield_heading.text=heading;

 

your_dynamic_textfield_description.text=description;

 

}

 

 

 

I have used this to display node values in dynamic text fields. Maybe it's not perfect but it's working.:)

 

 

 

Hope it helps.

 

 

 

Sasha

 

Solution studio

data.xml

Edited by Guest

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now