2015-05-04 31 views
0

自定義XML我有一個XML,我並沒有找到一種方法如何解析它(讀取裏面的數據)解析與AS3

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

我使用AS3語言

回答

1

你的榜樣在你的問題去(假定你的data是一個字符串),你可以做一些沿着這些路線:

//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]); 
} 

有很多方法可以讓你從想要的數據在AS3中的XML,一篇好文章is this one from senocular

1

The XML class is the way.

[它]包含使用XML對象的方法和屬性。 XML類(以及XMLList,Namespace和QName類)實現了ECMAScript for XML(E4X)規範(ECMA-357第2版)中定義的強大的XML處理標準。