PDA

View Full Version : Processing XML data


Ariff
11-02-2007, 02:02 AM
Hello,

im trying to setup a news section which publishes a header/body into an swf.

I created an XML file with the following layout :


<news>
<subject active = "true">
<header>This is the active article</header>
<body>Hi there, i am the active article of today</body>
</subject>

<subject active = "false">
<header>This is another article</header>
<body>This is another article</body>
</subject>

<subject active = "false">
<header>Article 3</header>
<body>this is article number 3</body>
</subject>
</news>


i use the following code in flash to "parse" the xml data :

function xmlLoaded(event:Event):void

{

xml = XML(event.target.data);
xmlList = xml.children();

//trace(xml.children().length());
for (var i=0; i <= xml.length(); i++){
//trace(i);
if (xml.child(i).@active == "true"){
trace("The active article is :" +xml.child(i).parent);
}
}

What im trying to accomplish is the following :

- im trying to browse through the xml file, with a for loop, to obtain the "active" article, once found, i need to obtain the header and body for that active article and push those to some textfields on stage. I have been trying to get the data i need out of it, but the XML and xmlList object really have me confused. Anyone who could give me a hand, or point me to some in-depth explanations of the xml/xmlList objects. I have been reading up on the livedocs from adobe, but that didn't get me much smarter on this matter.

Kind regards,
Juan

FrodoBaggins
11-02-2007, 08:39 AM
What does your trace result in?

saravanan
11-02-2007, 10:26 AM
hi,

var news:XML=<news>
<subject active = "true">
<header>This is the active article</header>
<body>Hi there, i am the active article of today</body>
</subject>

<subject active = "false">
<header>This is another article</header>
<body>This is another article</body>
</subject>

<subject active = "false">
<header>Article 3</header>
<body>this is article number 3</body>
</subject>
</news>;
var subject:XMLList=news.subject;
for (var i:int=0; i<subject.length(); i++) {
//trace(subject[i].attribute("active"));
if (subject[i].attribute("active")=="true") {
trace(subject[i].header.toString());
}
}


XML will have single root node and if xml without single root node is called XMLLIST


example for XML
var xml:XML=<class><student><id>1</id></student><student><id>2</id></student></class>

example for XMLLIST
var xml:XML=<student><id>1</id></student><student><id>2</id></student>

Jim Freer
11-02-2007, 12:29 PM
for each( var lvXml:XML in xml.subject.( @active == "true" ) )
{
trace( "Header: " + lvXml.header.toString() );
trace( "Body: " + lvXml.body.toString() + "\n");
} // for each


This creates an XMLList object of all active subject children:


var lvXmlList:XMLList = xml.subject.( @active == "true" )


The objects in the XMLList are XML objects of all active subject(s).

Jim
http://freerpad.blogspot.com/