PDA

View Full Version : xml parsing using as3



nipunkx
September 25th, 2009, 07:23 AM
please help me with the script to parse the following xml using AS3. I am having an XML which details different Sizes and the respective pressure and title of pipes. I need to retrieve data in a group where each SIZE is listed along with its available THICKNESS and the TITTLE available under that corresponding THICKENSS.
I shall paste my XML over here:

<pipes>

<size v="4mm">
<pressure v="Drainage">
<title>Universal</title>
<title>Megha</title>
<title>Kairali</title>
</pressure>
<pressure v="2.5kg">
<title>Universal</title>
<title>Megha</title>
<title>Kairali</title>
<title>Omega</title>
</pressure>
<pressure v="4kg">
<title>Universal</title>
<title>Megha</title>
<title>Kairali</title>
<title>Syntex</title>
</pressure>
</size>

<size v="3mm">
<pressure v="Drainage">
<title>Universal</title>
<title>Megha</title>
<title>Kairali</title>
<title>Syntex</title>
</pressure>
<pressure v="4kg">
<title>Universal</title>
<title>Megha</title>
<title>Kairali</title>
<title>Syntex</title>
</pressure>
</size>


<size v="2mm">
<pressure v="Drainage">
<title>Universal</title>
<title>Megha</title>
<title>Kairali</title>
<title>Syntex</title>
</pressure>
<pressure v="4kg">
<title>Universal</title>
<title>Megha</title>
<title>Kairali</title>
<title>Syntex</title>
</pressure>
<pressure v="8kg">
<title>Universal</title>
<title>Megha</title>
<title>Kairali</title>
<title>Syntex</title>
</pressure>
</size>


<size v="1mm">
<pressure v="Threaded">
<title>Universal</title>
<title>Megha</title>
<title>Kairali</title>
<title>Syntex</title>
</pressure>
<pressure v="Pasted">
<title>Universal</title>
<title>Megha</title>
<title>Kairali</title>
<title>Syntex</title>
</pressure>
</size>

</pipes>


Please help me out with the AS3 script to parse this xml.
http://www.actionscript.org/forums/images/statusicon/user_online.gif http://www.actionscript.org/forums/images/buttons/report.gif (http://www.actionscript.org/forums/report.php3?p=926620) http://www.actionscript.org/forums/images/misc/progress.gif http://www.actionscript.org/forums/images/buttons/edit.gif (http://www.actionscript.org/forums/editpost.php3?do=editpost&p=926620)

GrndMasterFlash
September 25th, 2009, 10:34 AM
im going to assume that THICKNESS is the attribute of size and you would like to retrieve all titles

import this (http://www.donovanh.com/main.php?page=blog/as3_dxml.txt) to your file

then you can do some looping to grab your data


var x_file:dXML = new dXML("file_location.xml");
//
var size_arr:Array = new Array();
//
function getPipeSizeSet(looking_for:String)
{
var size:String;
var pressure:String;
var p_arr:Array = new Array();
var title:String;
var t_arr:Array = new Array();
var a:int;
var b:int;
var c:int;
while((size!=looking_for)&&(getListAt(a)!=null))
{
var next_node:Object = x_file.getListAt(a);
var nxt_size:Object = x_file.getAttribute(next_node, "v");
nxt_size==looking_for?size = String(nxt_size):a++;
}
while(getListAt(a,b)!=null)
{
var pres_node:Object = x_file.getListAt(a,b);
var next_pres:Object = x_file.getAttribute(pres_node, "v");
pressure = String(next_pres);
p_arr.push(pressure);
while(getListAt(a,b,c)!=null)
{
var next_ttl:String = x_file.getBodyAt(a,b,c);
t_arr.push(next_ttl);
c++;
}
p_arr.push(t_arr);
}
/*so now we have the p_arr with odd #'s as the pressure and evens as the array of titles the throw it all togeather*/
size_arr.push(size);
size_arr.push(p_arr);
/*now we have added the pressures as the 2nd value of the main size array*/
}
/*ok so we have shoved a bunch of info into an array let's get it back!*/
var new_size:String = size_arr[0];
var new_pres_arr:Array = size_arr[1][0];
var new_pres:String = new_pres_arr[0];
var new_ttl_arr:Array = new_pres_arr[1];
var new_ttl:String = new_ttl_arr[0];
/*ideally you would loop through to get your results back, but as you can see size_arr[0] will always be the size. size_arr[1][x] will point to a p_arr. new_pres_arr[x is odd]will point to a pressure value and new_pres_arr[x is even] will point to an array of titles. there for new_ttl_arr[x] will pull out title values*/


i know this may seem complicated but it should do the trick for you. you might have questions so feel free to ask.