2015-10-13 74 views
0

如何知道何時遇到具有名稱空間的節點,以及何時可以提取該名稱空間的名稱和URL?如何知道屬性或節點是否有不同的名稱空間?

XML:

<s:Image xmlns:fx="http://ns.adobe.com/mxml/2009" 
     xmlns:s="library://ns.adobe.com/flex/spark" 
     xmlns:test="library://ns.test.com/flex/" 

     visible="false" 
     test:locked="true" /> 

XML解析代碼:

public static function getAttributeNames(node:XML):Array { 
    var result:Array = []; 
    var attributeName:String; 

    for each (var attribute:XML in node.attributes()) { 
     attributeName = attribute.name().toString(); 

     result.push(attributeName); 
    } 
    return result; 
} 

trace (result); 

[0] visible 
[1] library://ns.test.com/flex/::locked 

我能爲做一次檢查。 「::」 的字符串,但似乎累贅。一定會有更好的辦法。

回答

0

使用來自不同但相關帖子的信息我能夠檢查attribute.namespace()對象。通常沒什麼!但是當它有一個名稱空間前綴時,它將返回一個對象。

public static function getAttributeNames(node:XML):Array { 
    var result:Array = []; 
    var attributeName:String; 

    for each (var attribute:XML in node.attributes()) { 
     attributeName = attribute.name().toString(); 

     var attNamespace:Object = attribute.namespace(); 
     var a:Object = attribute.namespace().prefix  //returns prefix i.e. rdf 
     var b:Object = attribute.namespace().uri  //returns uri of prefix i.e. http://www.w3.org/1999/02/22-rdf-syntax-ns# 

     var c:Object = attribute.inScopeNamespaces() //returns all inscope namespace as an associative array like above 

     //returns all nodes in an xml doc that use the namespace 
     var nsElement:Namespace = new Namespace(attribute.namespace().prefix, attribute.namespace().uri); 

     var usageCount:XMLList = attribute..nsElement::*; 
     result.push(attributeName); 
    } 

    return result; 
} 

獲得的信息從here

相關問題