使用as3解析自定义XML

时间:2022-11-03 00:05:01

i have a XML and i didn't found a way how to parse it (read the data inside)

我有一个XML,我没有找到解决方法(阅读里面的数据)

<Saa:Header>
        <Saa:Message>
            <Saa:SenderReference> data
             </Saa:SenderReference>
       </Saa:Message>
        <Saa:Message>
            <Saa:SenderReference> data
             </Saa:SenderReference>
       </Saa:Message>
  </Saa:Header>

i use the as3 language

我使用as3语言

2 个解决方案

#1


Going with your example in your question (which assumes your data is a string), you could do something along these lines:

在你的问题(假设你的数据是一个字符串)中使用你的例子,你可以沿着这些方向做一些事情:

//your xml needs a namespace identifier to be valid
//I've added one on the header node. 
//Likely if you're consuming this xml from some third party, the namespace declaration will be on root node.
var myXML = <Saa:Header xmlns:Saa="urn:swift:saa:xsd:saa.2.0" >
        <Saa:Message>
            <Saa:SenderReference> data
             </Saa:SenderReference>
       </Saa:Message>
        <Saa:Message>
            <Saa:SenderReference> data
             </Saa:SenderReference>
       </Saa:Message>
  </Saa:Header>;

//create a reference to the `Saa` namespace that prefixes all your xml nodes
var saaNamespace:Namespace = new Namespace("urn:swift:saa:xsd:saa.2.0");

//tell AS3 to use that namespace by default
default xml namespace = saaNamespace;

//basically an array of all the SenderReference nodes in the entire xml
var xmlList:XMLList = myXML..SenderReference; 

//This line will give the same results as the line above, but if there were SenderReference nodes somewhere else in the document that weren't under Message nodes, they would not be included (unlike above)
xmlList = myXML.Message.SenderReference; 

//iterate through all the SenderReference nodes
for(var i:int=0;i<xmlList.length();i++){
    trace("Node #" + (i+1) + ":",xmlList[i]);
}

There are lots of ways to get the data you want from XML in AS3, a good article is this one from senocular.

有很多方法可以从AS3中获取XML所需的数据,这篇文章来自于senocular。

#2


The XML class is the way.

XML类是这样的。

[it] contains methods and properties for working with XML objects. The XML class (along with the XMLList, Namespace, and QName classes) implements the powerful XML-handling standards defined in ECMAScript for XML (E4X) specification (ECMA-357 edition 2).

[it]包含使用XML对象的方法和属性。 XML类(以及XMLList,Namespace和QName类)实现了ECMAScript for XML(E4X)规范(ECMA-357第2版)中定义的强大的XML处理标准。

#1


Going with your example in your question (which assumes your data is a string), you could do something along these lines:

在你的问题(假设你的数据是一个字符串)中使用你的例子,你可以沿着这些方向做一些事情:

//your xml needs a namespace identifier to be valid
//I've added one on the header node. 
//Likely if you're consuming this xml from some third party, the namespace declaration will be on root node.
var myXML = <Saa:Header xmlns:Saa="urn:swift:saa:xsd:saa.2.0" >
        <Saa:Message>
            <Saa:SenderReference> data
             </Saa:SenderReference>
       </Saa:Message>
        <Saa:Message>
            <Saa:SenderReference> data
             </Saa:SenderReference>
       </Saa:Message>
  </Saa:Header>;

//create a reference to the `Saa` namespace that prefixes all your xml nodes
var saaNamespace:Namespace = new Namespace("urn:swift:saa:xsd:saa.2.0");

//tell AS3 to use that namespace by default
default xml namespace = saaNamespace;

//basically an array of all the SenderReference nodes in the entire xml
var xmlList:XMLList = myXML..SenderReference; 

//This line will give the same results as the line above, but if there were SenderReference nodes somewhere else in the document that weren't under Message nodes, they would not be included (unlike above)
xmlList = myXML.Message.SenderReference; 

//iterate through all the SenderReference nodes
for(var i:int=0;i<xmlList.length();i++){
    trace("Node #" + (i+1) + ":",xmlList[i]);
}

There are lots of ways to get the data you want from XML in AS3, a good article is this one from senocular.

有很多方法可以从AS3中获取XML所需的数据,这篇文章来自于senocular。

#2


The XML class is the way.

XML类是这样的。

[it] contains methods and properties for working with XML objects. The XML class (along with the XMLList, Namespace, and QName classes) implements the powerful XML-handling standards defined in ECMAScript for XML (E4X) specification (ECMA-357 edition 2).

[it]包含使用XML对象的方法和属性。 XML类(以及XMLList,Namespace和QName类)实现了ECMAScript for XML(E4X)规范(ECMA-357第2版)中定义的强大的XML处理标准。