【发布时间】:2015-07-21 07:01:25
【问题描述】:
是否可以在 Actionscript 3 中的 XML 节点查询中使用变量?
鉴于以下 rss 提要结构:
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
<channel>
<item>
<media:content url="http://www.example.com/test.jpg" type="image/jpeg" height="683" width="1024" />
<media:content url="http://www.example.com/test2.jpg" type="image/jpeg" height="683" width="1024" />
</item>
</channel>
</rss>
以下代码按预期工作,显示媒体命名空间中第一个内容节点的 url 属性值:
var nsAttribute:String = 'media';
var nodeName:String = 'content';
var holder:String = 'item';
var imagePathAttribute = 'url';
// Pull the namespace attribute value from the xml declaration
var ns:String = rssXML.namespace(nsAttribute);
// Make a namespace instance based on the xpl namespace's URI
var imagePathNameSpace:Namespace = new Namespace( ns );
trace ( rssXML.channel.child(holder).imagePathNameSpace::content[0].@url );
返回预期结果:
http://www.example.com/test.jpg
但是,当我使用路径节点中的变量时,出现错误。
trace ( rssXML.channel.child(holder).imagePathNameSpace::nodeName[0].@imagePathAttribute );
我收到的错误是:
TypeError: Error #1010: A term is undefined and has no properties.
可能是因为它找不到任何要索引的值,因为路径返回一个空字符串。如果我从路径中删除索引部分“[0]”,它会返回一个空字符串。
那么如何在xml查询中使用变量呢?我尝试这样做的原因是使我正在处理的类具有足够的可扩展性,我们需要做的就是设置类的属性,因此它可以与不同的 rss 提要类型一起使用,而不必费力-每个特定提要的代码。
感谢您提供的任何帮助。
【问题讨论】:
标签: xml actionscript-3