hmdb
hmdb

Reputation: 323

Reading XML in AS3

I have the following xml from a large xml file.

<Text>
   <cp IX='0'/>
   <pp IX='0'/>
   <tp IX='0'/>Wo<cp IX='1'/>rl<cp IX='2'/>d<cp IX='3'/>
   <cp IX='4'/>!!<cp IX='5'/>
</Text>

is there any way to access the text in between "cp" and "tp" nodes separately (ex: Wo, rl, d, !!)? I can get them as a single string but, I need them separately like in an array. Thanks..

Upvotes: 1

Views: 124

Answers (3)

ZuzEL
ZuzEL

Reputation: 13645

You should get your <Text> tag content as string and use split with regex:

var array:Array = textTagContent.split(/<\S*[^>]*>/);
trace(array) // ,,,Wo,rl,d,,!!,

Trim it!

a = trimArray(a);

Use it!

trace(a) // Wo,rl,d,!!

Trim function:

function trimArray(array:Array):Array {
    var a:Array = [];
    for each (var item in array) 
    {
        if (item) {
            a.push(item);
        }
    }
    return a;
}

Upvotes: 0

Stefan van de Vooren
Stefan van de Vooren

Reputation: 2707

You can use the text() method on your XML property:

var myXML : XML = <Text>
        <cp IX='0'/>
        <pp IX='0'/>
        <tp IX='0'/>Wo<cp IX='1'/>rl<cp IX='2'/>d<cp IX='3'/>
        <cp IX='4'/>!!<cp IX='5'/>
    </Text>

var text : String = myXML.text().toString();
trace (text); // output World!!

Upvotes: 1

Kolyunya
Kolyunya

Reputation: 6230

Basically Wo , rl , d and !! are children of Text node. Their type is TEXT_NODE.

So I would suggest to iterate over all Text children, and store those which type is TEXT_NODE as elements of an array.

Upvotes: 0

Related Questions