Skip to main content
Inspiring
May 9, 2013
Question

How to access different xml tags?

  • May 9, 2013
  • 1 reply
  • 586 views

This is my xml file:

<?xml version="1.0" encoding="utf-8"?>

<personagequotes>

<personage>

<quote>Hallo daar</quote>

<quote>Hier ben ik</quote>

</personage>

<personage>

<quote>Tjonge jonge</quote>

<quote>Nog een uitspraak</quote>

</personage>

</personagequotes>

I'm using this AS code to read it's content and placing clips on the stage containing the quotes of the first 'personage' tag:

var personagequotes_XML:XML;

var xmlLoader:URLLoader = new URLLoader();

xmlLoader.load(new URLRequest("quotes.xml"));

xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);

xmlLoader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);

function errorHandler(event:IOErrorEvent):void

{

          trace("XML loading error: " + event);

}

function xmlLoaded(event:Event):void

{

          trace("loaded");

          personagequotes_XML = new XML(xmlLoader.data);

          var quoteNumbers:Number = personagequotes_XML.personage[0].quote.length();

          for (var i = 0; i < quoteNumbers; i++)

          {

                    var MyInstance:quoteclip = new quoteclip();

                    var quote:String = personagequotes_XML.personage[0].quote;

                    MyInstance.quotetext.text = quote;

                    addChild(MyInstance);

                    MyInstance.x=20;

                    MyInstance.y=20+(i*(MyInstance.height+4))

          }

}

Which works ok, but I want to use it on this XML code:

<?xml version="1.0" encoding="utf-8"?>

<personagequotes>

<personage1>

<quote>Hallo daar</quote>

<quote>Hier ben ik</quote>

</personage1>

<personage2>

<quote>Tjonge jonge</quote>

<quote>Nog een uitspraak</quote>

</personage2>

</personagequotes>

Each 'personage' is numbered so it is more clear which 'personage' is saying the quotes. How do I alter lines like this:

var quoteNumbers:Number = personagequotes_XML.personage[0].quote.length();

so that it understangs which 'personage' tag (personage1 or personage2) to access?

This topic has been closed for replies.

1 reply

Ned Murphy
Legend
May 9, 2013

Your second version of the xml is not really made to differentiate the way you want to... it does not have a personage tag.   The first version is what you need if you want to be able to pick oout personage[0].  Each different "personage#" tag is unique.

jiggy1965Author
Inspiring
May 9, 2013

Yes, I know, but in the XML for clarity I would like to use tags that show which 'personage' is saying the quotes. So here 'personage1' and 'personage2'.

In actionscript I then want to use those tags as if they were strings. Something like this. I know it don't work, but it shows what I'm trying to do:

var personagequotes_XML:XML;

var xmlLoader:URLLoader = new URLLoader();

xmlLoader.load(new URLRequest("quotes.xml"));

xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);

xmlLoader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);

function errorHandler(event:IOErrorEvent):void

{

          trace("XML loading error: " + event);

}

function xmlLoaded(event:Event):void

{

          trace("loaded");

          personagequotes_XML = new XML(xmlLoader.data);

          var whichPersonage:String = "personage" + 1;

          var quoteNumbers:Number = personagequotes_XML.whichPersonage[0].quote.length();

          for (var i = 0; i < quoteNumbers; i++)

          {

                    var MyInstance:quoteclip = new quoteclip();

                    var quote:String = personagequotes_XML.whichPersonage[0].quote;

                    MyInstance.quotetext.text = quote;

                    addChild(MyInstance);

                    MyInstance.x=20;

                    MyInstance.y=20+(i*(MyInstance.height+4))

          }

}

So here I want to read the personage1 tag. So I'm using:

 

       var whichPersonage:String = "personage" + 1;

          var quoteNumbers:Number = personagequotes_XML.whichPersonage[0].quote.length();

which should result this so Flash can read it:

      

   var quoteNumbers:Number = personagequotes_XML.personage1[0].quote.length();

But of course this doesn't work, I know. It needs something to turn this whichPersonage string into the xml object name so it can be targetted in the quoteNumbers line.

Is this possible?