AS3 解析 XML
您好,当我尝试使用 AS3 解析 XML 时,我不断遇到错误并在输出面板中输出显示“未定义”。我想获得score1,score2的值...... 谁能告诉我我做错了什么?
感谢
科学
////AS3//////////
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
var myXML:XML;
var xmlLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
xmlLoader.load(new URLRequest("scores.xml"));
function xmlLoaded(e:Event):void
{
myXML = new XML(e.target.data);
trace(myXML.scores1);
}
//////xml file////////////
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<root>
<scores>
<id>1</id>
<score1>105</score1>
<score2>60</score2>
<score3>56</score3>
<score4>48</score4>
<score5>30</score5>
</scores>
</root>
</feed>
Hello I keep running into errors and outputs that say "undefined" in my output panel when I try and parse XML using AS3. I would like to get the values of score1, score2....
Can anyone tell me what I am doing wrong?
thanks
Scientific
////AS3//////////
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
var myXML:XML;
var xmlLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
xmlLoader.load(new URLRequest("scores.xml"));
function xmlLoaded(e:Event):void
{
myXML = new XML(e.target.data);
trace(myXML.scores1);
}
//////xml file////////////
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<root>
<scores>
<id>1</id>
<score1>105</score1>
<score2>60</score2>
<score3>56</score3>
<score4>48</score4>
<score5>30</score5>
</scores>
</root>
</feed>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我有一篇更深入的文章这里,但你会本质上是获取所有分数标签,然后对它们求和。您还必须考虑 feed 节点上定义的 xmlns。
以下代码适用于 Score1 到 ScoreX:
您可以通过引用命名空间变量来简化正则表达式。
祝你好运!
[[编辑]]
修改为引用命名空间:
I have a rather more indepth post here, but you would essentially grab all score tags, then sum them. You must also take into account the xmlns defined on the feed node.
The following code would work for score1 to scoreX:
You could probably simplify the regex by referencing the namespace variable.
Best of luck!
[[Edit]]
Modified to reference the namespace:
试试这个代码:
}
Try this code:
}
更好的是...像这样
在分割的地方使用scores,id,score1 xml标签
Its better na....like this
use scores,id,score1 xml tag in split places
您可能会发现后代访问器..很有用。示例:
编辑:
使用通用命名空间会导致一些问题。添加别名可以修复此问题,例如:
否则 Flash 预计您将使用完全限定名称来指定您尝试访问的元素:
You might find the descendant accessor .. useful. Example:
Edit:
There is something about using the generic namespace that is causing some problems. Adding an alias fixes it, eg:
Otherwise Flash is expecting that you are going to use fully qualified names to specify the element you are trying to access:
您未定义,因为您的 xml 中没有“scores1”标签。
试试这个:
You get undefined because you don't have "scores1" tag in your xml.
Try this: