我如何知道何时遇到带有命名空间的节点?当我这样做时,是否可以提取该命名空间的名称和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
我可以检查&#34; ::&#34;在字符串中,但似乎很麻烦。肯定有更好的办法。
答案 0 :(得分: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获得的信息。