将XML加载到Flex AS3中

时间:2012-05-12 17:16:55

标签: xml actionscript-3 flex loader

我正在尝试从XML文档加载所有XML数据,并且即使通过研究也一直都在失败

以下是我要加载的XML片段:

<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <id>urn:uuid:5762c9e8-4e65-3b0c-83b3-7874683f3dbe</id>
  <link href="/v1/espi_third_party_batch_feed" rel="self">
  </link>
  <title type="text">Opower ESPI Third Party Batch Feed v1</title>
  <updated>2012-04-03T22:55:36.364Z</updated>

  <entry>
    <id>urn:uuid:baa6a50a-1306-3680-80a4-51d228db8f25</id>
    <link href="/v1/User/3560606/UsagePoint/6403112/MeterReading/1/IntervalBlock/1" rel="self">
    </link>
    <content type="xml">
      <IntervalBlock xmlns="http://naesb.org/espi">
        <interval>
          <duration>3600</duration>
          <start>1299128400</start>
        </interval>
        <IntervalReading>
          <timePeriod>
            <duration>3600</duration>
            <start>1299128400</start>
          </timePeriod>
          <value>436</value>
        </IntervalReading>
      </IntervalBlock>
    </content>
  </entry>
</feed>

以下是我在XmlLoader中困惑的AS3代码片段:

    public function returnArray():Array
    {           
        var ATOM:Namespace = new Namespace("http://www.w3.org/2005/Atom");
        var NAESB:Namespace = new Namespace("http://naesb.org/espi");
        trace("entered returnArray");
        var tempArray:Array = new Array();
        var x:Number = 0;
        var y:Number = 0;
        var entries = this.docXML.ATOM::entry;
        trace(entries.length());                //returns 4 and supposed to
        trace(entries[3].content.length());     //returns 0 supposed to return 1
        trace(this.docXML.ATOM::entry[3].content.NAESB::interval.length()); //returns 0`enter code here`

2 个答案:

答案 0 :(得分:0)

首先,你应该看看the documentation for e4x。其次:你究竟想做什么?

您可以更改为this.docXML.ATOM::entry.content..NAESB::interval.length()的最后一行,但我不确定您是否真正试图摆脱xml文档。获取节点的长度对于从xml文件中获取实际数据并不是非常有用。

答案 1 :(得分:0)

感谢frankhermes和www.Flextras.com帮助我学习的答案是为了继续遍历,需要在xml中添加另一个ATOM命名空间以保持遍历

var entries = this.docXML.ATOM::entry;
trace(entries[3].content.length()); 

应该是

var entries = this.docXML.ATOM::entry;
trace(entries[3].ATOM::content.length());