【问题标题】:E4X: Parse a document with namespaced nodes and a variable to be evaluated?E4X:解析具有命名空间节点和要评估的变量的文档?
【发布时间】:2009-10-10 09:10:44
【问题描述】:

我正在尝试解析具有如下所示节点的文档:

<doc>
<results>
        <result xmlns="http://www.inktomi.com/">
            <title>Senate Panel to Review Election Unit Sale</title>
        </result>
</result>
</doc>

但是结果的命名空间和节点名可能不同。如果不是这样,这将起作用:

results..*::title //>Senate Panel to ...

但这样做不会:

var myvar = "title"
results..*::[myvar] 

有什么线索吗?

【问题讨论】:

    标签: apache-flex actionscript-3 e4x


    【解决方案1】:

    所以正确的解决方案显然是:

    var myvar = "title"
    var ans = results..*.(localName()==myvar);
    

    感谢 Twitter 上的 @xtyler 找到答案

    【讨论】:

      【解决方案2】:

      显然,访问子级的方括号方式和使用 * 选择任何命名空间不能一起使用

      var doc:XML = 
      <doc> 
          <results> 
              <result xmlns="http://www.inktomi.com/">  
                  <title>Senate Panel to Review Election Unit Sale</title> 
              </result>
          </results>
      </doc>;
      var ns:Namespace = new Namespace("http://www.inktomi.com/");
      trace(doc..*::title.toXMLString()); //These three 
      trace(doc.results.*::result);       //lines compile
      trace(doc.results.ns::["result"]);  //and run as expected
      //This commented out line compiles but throws 2 verify errors in the run time
      //trace(doc.results.*::["result"]); 
      

      VerifyError:错误 #1080:命名空间的值非法。
      ReferenceError:错误 #1065:未定义变量测试。

      VerifyError 类表示遇到格式错误或损坏的 SWF 文件时发生的错误。

      【讨论】:

        【解决方案3】:

        不是 E4X 解决方案,但您可以遍历 xml.namespaceDeclarations() 返回的所有可用命名空间,然后获取第一个子节点或使用方括号访问它。

        您也可以只预先解析 xml 并使所有命名空间与快速修复相同。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-10-29
          • 1970-01-01
          • 2011-12-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多