The following is example of reading XML with ActionScript 3
以下是使用ActionScript 3读取XML的示例
var xml:XML =
<content>
<a>Hello A</a>
<b>Hello B</b>
<c>
<c1>Child C1</c1>
<c2>Child C2</c2>
</c>
</content>;
trace(xml.a); // OP: Hello A
trace(xml.c.c1); // OP: Child C1
trace(xml.d); // OP: (nothing)
trace(xml.b);; // OP: Hello B
I don't see the xml.d
outputting an empty string as expected behavior? Is this normal? What is the reasoning for this?
我没有看到xml.d输出一个空字符串作为预期的行为?这是正常的吗?这是什么原因?
To me, I 'feel' like I should be doing this:
对我来说,我觉得'我应该这样做:
if(xml.d) trace(xml.d);
Is it ok to rely on the empty string behavior? IE do I need to check for a node's existence??
可以依赖空字符串行为吗? IE我需要检查节点的存在吗?
3 个解决方案
#1
4
xml.d
is XMLList. Since node doesn't exist, this list will be empty.
You can test node existence with xml.d.length()
(gives count of nodes d) or xml.d[0]
(gives first node d, will be null in this case).
xml.d是XMLList。由于节点不存在,此列表将为空。您可以使用xml.d.length()(给出节点数d)或xml.d [0]来测试节点是否存在(给出第一个节点d,在这种情况下将为null)。
#2
0
the only Time I have ever had issues with empty child nodes is if i was trying to assess children of the empty node. So yeah in that case you need to test for empty string.
我唯一遇到空子节点问题的时间是我是否正在尝试评估空节点的子节点。所以,在那种情况下你需要测试空字符串。
if(xml.d != "" ) trace(xml.d);
You might want to also read up on e4x
您可能还想阅读e4x
#3
0
If you try to access properties or methods of a non existent node, the AVM will throw an error. You should wrap any code that might fail for a known reason in a try{} catch(){}
.
如果您尝试访问不存在的节点的属性或方法,AVM将抛出错误。您应该在try {} catch(){}中包装因已知原因可能失败的任何代码。
here's a good explanation: http://www.kirupa.com/forum/showthread.php?p=1957523
这是一个很好的解释:http://www.kirupa.com/forum/showthread.php?p = 1957523
#1
4
xml.d
is XMLList. Since node doesn't exist, this list will be empty.
You can test node existence with xml.d.length()
(gives count of nodes d) or xml.d[0]
(gives first node d, will be null in this case).
xml.d是XMLList。由于节点不存在,此列表将为空。您可以使用xml.d.length()(给出节点数d)或xml.d [0]来测试节点是否存在(给出第一个节点d,在这种情况下将为null)。
#2
0
the only Time I have ever had issues with empty child nodes is if i was trying to assess children of the empty node. So yeah in that case you need to test for empty string.
我唯一遇到空子节点问题的时间是我是否正在尝试评估空节点的子节点。所以,在那种情况下你需要测试空字符串。
if(xml.d != "" ) trace(xml.d);
You might want to also read up on e4x
您可能还想阅读e4x
#3
0
If you try to access properties or methods of a non existent node, the AVM will throw an error. You should wrap any code that might fail for a known reason in a try{} catch(){}
.
如果您尝试访问不存在的节点的属性或方法,AVM将抛出错误。您应该在try {} catch(){}中包装因已知原因可能失败的任何代码。
here's a good explanation: http://www.kirupa.com/forum/showthread.php?p=1957523
这是一个很好的解释:http://www.kirupa.com/forum/showthread.php?p = 1957523