views:

36

answers:

1

I'm getting XML input that looks like this

<?xml version="1.0" encoding="utf-8"?>
<data1>this is data 1</data1>
<data2>this is data 2</data2>
<data3>
      <3a>this is data 3a</3a>
      <3b>this is data 3b</3b>
      <3c>
            <TextFlow xmlns="http://ns.adobe.com/textLayout/2008"&gt;
                    <p direction="ltr" >
                           <span>some text</span>
                           <span>some additional text</span>
                    </p>
                    <p direction="ltr">
                           <span>some text</span>
                           <span>some additional text</span>
                    </p>
             </TextFlow>
       </3c>
</data3>

I can read <data1> with event.result.data1 which outputs a string this is data1

But when I do the same thing to event.result.data3.3c, it prints object [object] so I guess it's trying to dig deeper into the tree. But I need the actual string text (not xml tree) starting from and including <TextFlow></TextFlow> to be stored and printed as a string. Any idea what's the syntax for this?

The string I'm looking for would look like this:

        <TextFlow xmlns="http://ns.adobe.com/textLayout/2008"&gt;
                <p direction="ltr" >
                       <span>some text</span>
                       <span>some additional text</span>
                </p>
                <p direction="ltr">
                       <span>some text</span>
                       <span>some additional text</span>
                </p>
         </TextFlow>
+1  A: 

First, I see a couple of problems with your XML. It's invalid and it's kind of surprising that you don't get an error.

1) There's no root node. A simple fix would be putting what you already have in a tag or something more meaninful. But you need to have a root node.

2) Node names that begin with numbers are a bad idea. Not sure if it's valid according to the XML spec, but even if it is, it won't be valid actionscript. In that case, you will have to avoid using dots (instead of data1.3c, something like data1["3c"]. As a general rule, name your nodes just like you name your variables and you'll be fine.

If the data within <TextFlow> is meant to be a string, and you are not interested in parsing it, perhaps a better idea is wrapping it in a CDATA section:

<c3><![CDATA[<TextFlow xmlns="http://ns.adobe.com/textLayout/2008"&gt;
    <p direction="ltr" >
           <span>some text</span>
           <span>some additional text</span>
    </p>
    <p direction="ltr">
           <span>some text</span>
           <span>some additional text</span>
    </p>
</TextFlow>]]></c3>

Otherwise, you should use xml namespaces to work with it (notice the <TextFlow> node has a xmlns declaration; xmlns stands for XML namespace.

You could try something like this to grab it:

var layout_ns:Namespace = new Namespace("http://ns.adobe.com/textLayout/2008");
trace(your_xml.data3.c3.layout_ns::TextFlow);

Notice TextFlow is prefixed by the proper namespace.

An alternative to the above code is setting a default namespace:

var layout_ns:Namespace = new Namespace("http://ns.adobe.com/textLayout/2008");
default xml namespace = layout_ns;
trace(your_xml.data3.c3.TextFlow);

This kind of defeats the purpose of having namespaces in the first place, though.

PS

If you go with the second option (i.e. no CDATA), once you get to the node you want, you could use the toXMLString method to get the contents of the node as a string.

Juan Pablo Califano
Juan, Thank you so much. Best answer ever.
duder