简体   繁体   English

ActionScript 3从网站解析XML

[英]ActionScript 3 Parsing XML From Website

I have tried may example I found here and on the new (Google). 我试过可能的例子,我发现在这里和新的(谷歌)。 Nothing seems to work. 似乎没什么用。

I have a XML file which I get after a call to a website: 我有一个XML文件,我在调用网站后获得:

<?xml version="1.0" encoding="iso8859-1" ?>
<Database version="1.24" xmlns="http://1234.com">
  <Session>
    <Key>1234</Key>
    <Count>2424</Count>
    <SubExp>Sun Dec  1 00:00:00 2013</SubExp>
    <GMTime>Thu Feb  7 19:38:03 2013</GMTime>
    <Remark>cpu: 0.058s</Remark>
  </Session>
</Database>

OK I loaded into a XML object like so: 好吧我加载到这样的XML对象:

var xml:XML = new XML(event.target.data);

Good so far the object contains the XML data: 到目前为止,对象包含XML数据:

<Database version="1.24" xmlns="http://1234.com">
      <Session>
        <Key>1234</Key>
        <Count>2424</Count>
        <SubExp>Sun Dec  1 00:00:00 2013</SubExp>
        <GMTime>Thu Feb  7 19:38:03 2013</GMTime>
        <Remark>cpu: 0.058s</Remark>
      </Session>
    </Database>

now I need to read the key value under session so I have tried : 现在我需要读取会话下的键值,所以我尝试过:

xml.Session.Key
xml.Session[0].Key
xml[0].Session.Key

some of them returns a empty string some just errors either way no data? 他们中的一些人返回一个空字符串,有些只是错误,无论是哪种方

so in the Expression window I typed xml[0][0] just to see what happens. 所以在Expression窗口中我输入xml [0] [0]只是为了看看会发生什么。

and it returns the key value, however when I place it in my code 并且它返回键值,但是当我将它放在我的代码中时

var key:String = xml[0][0];
trace(key):

the trace returns the whole XML file? 跟踪返回整个XML文件? so I'm not sure what I might be missing? 所以我不确定我可能会缺少什么?

The problem is with the namespace. 问题在于命名空间。

Try this: 尝试这个:

var xml:XML = new XML(event.target.data);
var ns:Namespace = xml.namespace();
trace(xml.ns::Session.ns::Key);

Also, this can be a bad idea, but when I just want some data and don't care about the namespaces (or they aren't relevant to what I'm doing) I have this handy function to strip them out and return an XML object without them: 此外,这可能是一个坏主意,但当我只是想要一些数据并且不关心命名空间(或者它们与我正在做的事情无关)时,我有这个方便的功能来剥离它们并返回一个没有它们的XML对象:

public function stripNamespaces(xml:XML):XML {

    const DECLARATION_REG_EXP:RegExp = new RegExp("xmlns[^\"]*\"[^\"]*\"", "gi");

    var namespaceDeclarations:Array = xml.namespaceDeclarations();

    for (var i:int = 0; i < namespaceDeclarations.length; i++) {
        xml.removeNamespace(namespaceDeclarations[i]);
    }

    return new XML(xml.toString().replace(DECLARATION_REG_EXP, ""));        
}

Since your xml has a namespace, you need to tell Flash/e4x to use it: 由于您的xml具有命名空间,因此您需要告诉Flash / e4x使用它:

private var xml:XML =
    <Database version="1.24" xmlns="http://1234.com">
      <Session>
        <Key>1234</Key>
        <Count>2424</Count>
        <SubExp>Sun Dec  1 00:00:00 2013</SubExp>
        <GMTime>Thu Feb  7 19:38:03 2013</GMTime>
        <Remark>cpu: 0.058s</Remark>
      </Session>
    </Database>;

private function onCreationComplete():void
{
    namespace myNameSpace = "http://1234.com";
    use namespace myNameSpace;
    var value:String = xml.Session.Key;
    trace("value:", value); // outputs: value: 1234
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM