AS3 解析 XML

发布于 2024-12-16 13:23:39 字数 999 浏览 0 评论 0原文

您好,当我尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

岁吢 2024-12-23 13:23:40

我有一篇更深入的文章这里,但你会本质上是获取所有分数标签,然后对它们求和。您还必须考虑 feed 节点上定义的 xmlns。

以下代码适用于 Score1 到 ScoreX:

var namespace:Namespace = new Namespace("http://www.w3.org/2005/Atom");

var score:XML;
var scores:XMLList = xml..namespace::scores.*.(/^http:\/\/www.w3.org\/2005\/Atom::score/.test(name()));

if(!scores)
{
    return;
}

var final:Number = 0;
for each(score in scores)
{
    final += Number(score.text());
}

trace(final); // 299

您可以通过引用命名空间变量来简化正则表达式。

祝你好运!

[[编辑]]

修改为引用命名空间:

var regex:RegExp = new RegExp(namespace.toString().replace(/\//g, "\\/") + "::score");
var scores:XMLList = xml..namespace::scores.*.(regex.test(name()));

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:

var namespace:Namespace = new Namespace("http://www.w3.org/2005/Atom");

var score:XML;
var scores:XMLList = xml..namespace::scores.*.(/^http:\/\/www.w3.org\/2005\/Atom::score/.test(name()));

if(!scores)
{
    return;
}

var final:Number = 0;
for each(score in scores)
{
    final += Number(score.text());
}

trace(final); // 299

You could probably simplify the regex by referencing the namespace variable.

Best of luck!

[[Edit]]

Modified to reference the namespace:

var regex:RegExp = new RegExp(namespace.toString().replace(/\//g, "\\/") + "::score");
var scores:XMLList = xml..namespace::scores.*.(regex.test(name()));
凶凌 2024-12-23 13:23:40

试试这个代码:

import gs.dataTransfer.XMLParser;

XMLParser.load("myDocument.xml", onFinish);

function onFinish($success:Boolean, $results:Object, $xml:XML):Void



{ //This function gets called when the XML gets parsed.

    if ($success) {

        trace("The first book is: "+$results.Book[0].name);
    }

}

Try this code:

import gs.dataTransfer.XMLParser;

XMLParser.load("myDocument.xml", onFinish);

function onFinish($success:Boolean, $results:Object, $xml:XML):Void



{ //This function gets called when the XML gets parsed.

    if ($success) {

        trace("The first book is: "+$results.Book[0].name);
    }

}

南城旧梦 2024-12-23 13:23:40

更好的是...像这样

在分割的地方使用scores,id,score1 xml标签

var xml=Xml as String;
trace(xml);
var xmlfull=xml.split("scores").length - 1;
trace(xmlfull);
for (var i = 1; i <= xmlfull; i++) 
{
  trace(xml.split("id")[i].split("id")[0]);
  trace(xml.split("score1")[i].split("score1")[0]);
  trace(xml.split("score2")[i].split("score2")[0]);
}

Its better na....like this

use scores,id,score1 xml tag in split places

var xml=Xml as String;
trace(xml);
var xmlfull=xml.split("scores").length - 1;
trace(xmlfull);
for (var i = 1; i <= xmlfull; i++) 
{
  trace(xml.split("id")[i].split("id")[0]);
  trace(xml.split("score1")[i].split("score1")[0]);
  trace(xml.split("score2")[i].split("score2")[0]);
}
丢了幸福的猪 2024-12-23 13:23:39

您可能会发现后代访问器..很有用。示例:

myXML..score1[0].toString()

编辑:

使用通用命名空间会导致一些问题。添加别名可以修复此问题,例如:

<feed xmlns:atom="http://www.w3.org/2005/Atom">

否则 Flash 预计您将使用完全限定名称来指定您尝试访问的元素:

var qName:QName = new QName("http://www.w3.org/2005/Atom","score1");
trace(myXML.descendants(qName)[0].toString()); // 105

You might find the descendant accessor .. useful. Example:

myXML..score1[0].toString()

Edit:

There is something about using the generic namespace that is causing some problems. Adding an alias fixes it, eg:

<feed xmlns:atom="http://www.w3.org/2005/Atom">

Otherwise Flash is expecting that you are going to use fully qualified names to specify the element you are trying to access:

var qName:QName = new QName("http://www.w3.org/2005/Atom","score1");
trace(myXML.descendants(qName)[0].toString()); // 105
留蓝 2024-12-23 13:23:39

您未定义,因为您的 xml 中没有“scores1”标签。
试试这个:

trace(myXML.scores.id.toString());
trace(myXML.scores.score1.toString());
trace(myXML.scores.score2.toString());
trace(myXML.scores.score3.toString());
trace(myXML.scores.score4.toString());
trace(myXML.scores.score5.toString());

You get undefined because you don't have "scores1" tag in your xml.
Try this:

trace(myXML.scores.id.toString());
trace(myXML.scores.score1.toString());
trace(myXML.scores.score2.toString());
trace(myXML.scores.score3.toString());
trace(myXML.scores.score4.toString());
trace(myXML.scores.score5.toString());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文