简体   繁体   English

在Flex / AS3中以字符串形式读取xml中的xml

[英]Reading xml within xml as String in flex/AS3

I'm getting XML input that looks like this 我收到的XML输入看起来像这样

<?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">
                    <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 我可以用event.result.data1读取<data1> ,它输出一个字符串, 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. 但是,当我对event.result.data3.3c做同样的事情时,它会打印object [object]因此我想它正在尝试更深入地研究树。 But I need the actual string text (not xml tree) starting from and including <TextFlow></TextFlow> to be stored and printed as a string. 但是我需要存储从<TextFlow></TextFlow>开始并包括在内的实际字符串文本(不是xml树),并将其打印为字符串。 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">
                <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>

First, I see a couple of problems with your XML. 首先,我看到您的XML有两个问题。 It's invalid and it's kind of surprising that you don't get an error. 这是无效的,并且您没有收到错误,这令人感到惊讶。

1) There's no root node. 1)没有根节点。 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. 2)以数字开头的节点名称不是一个好主意。 Not sure if it's valid according to the XML spec, but even if it is, it won't be valid actionscript. 不确定根据XML规范是否有效,但是即使有效,它也不是有效的动作脚本。 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. 在这种情况下,您将必须避免使用点(而不是data1.3c ,类似data1["3c"] 。通常,像命名变量一样命名节点,这样就可以了。

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: 如果<TextFlow>的数据是字符串,并且您对解析它不感兴趣,则可能更好的主意是将其包装在CDATA节中:

<c3><![CDATA[<TextFlow xmlns="http://ns.adobe.com/textLayout/2008">
    <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. 否则,您应该使用xml名称空间来使用它(请注意<TextFlow>节点具有xmlns声明; xmlns代表XML名称空间。

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. 注意TextFlow由适当的名称空间作为前缀。

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 PS

If you go with the second option (ie 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. 如果使用第二个选项(即不使用CDATA),那么一旦到达所需的节点,就可以使用toXMLString方法以字符串形式获取节点的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM