如何检查Flex / AS3中是否存在XML节点?

时间:2022-10-24 01:53:31

Why does the trace in the loop below return false for every iteration, even though there ARE nodes named with 6 of the 8 possible values??? This only happens when I have a namespace. Is there some other way to check for the node values???

为什么下面循环中的跟踪在每次迭代时都返回false,即使有8个可能值为6的ARE节点?这只有在我有一个命名空间时才会发生。有没有其他方法来检查节点值???

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
    <mx:Script>
        <![CDATA[
            private namespace ltrs = "letters";

            use namespace ltrs;

            private var myArray:Array = ["a","b","c","d","e","f","g","h"];

            private function checkXML():void
            {
                for each (var p:String in myArray)
                {
                    trace(myXML.hasOwnProperty(p).toString()); // returns false;
                }
            }
        ]]>
    </mx:Script>

    <mx:XML id="myXML">
        <root xmlns="letters">
            <a>true</a>
            <b>true</b>
            <c>true</c>
            <e>true</e>
            <f>true</f>
            <g>true</g>
        </root>
    </mx:XML>

    <mx:Button click="checkXML();" />
</mx:Application>

3 个解决方案

#1


The method hasOwnProperty just doesn't work with XML nodes. I believe this is to the E4X specification. However, you can always ask for a node with E4X even if it's not there and just see what the length of the XMLList is that you get back. Like so:

方法hasOwnProperty不适用于XML节点。我相信这是E4X规范。但是,您可以随时询问具有E4X的节点,即使它不在那里,只看到您回来的XMLList的长度。像这样:

trace(myXML[p].length());

EDIT: As noted below, I was wrong about the hasOwnProperty bit. It does work with XML, and it is a namespace issue that's causing your problem. You can make sure your XML is using the correct namespace by using this handy snippet:

编辑:如下所述,我对hasOwnProperty位错了。它确实与XML一起使用,它是一个导致问题的命名空间问题。您可以使用这个方便的代码段确保您的XML使用正确的命名空间:

if (myXML.namespace("") != undefined) {
    default xml namespace = myXML.namespace("");
}

#2


I know this is an old thread, but in case others have this same issue, here's another way of doing it. Rather than using .length(), you can check for the existence of a namespaced node using QName like so:

我知道这是一个旧线程,但如果其他人有同样的问题,这是另一种方法。您可以使用QName检查是否存在命名空间节点,而不是使用.length(),如下所示:


var xml:XML = 
<ns:outernode xmlns:ns="urn:ns:1.0">
    <ns:innernode>Here is some text content</ns:innernode>
</ns:outernode>;
var ns:Namespace = new Namespace("urn:ns:1.0");
var q:QName = new QName(ns, "innernode");
var b:Boolean = xml.hasOwnProperty(q);
trace("Has innernode: " + b);

This will return true. The QName q represents the fully qualified nodename "ns:innernode".

这将返回true。 QName q表示完全限定的节点名称“ns:innernode”。

#3


I think the problem resides in the namespace. Try it without, or try xmlns:letters = "letters"

我认为问题在于命名空间。尝试不用,或尝试xmlns:letters =“letters”

#1


The method hasOwnProperty just doesn't work with XML nodes. I believe this is to the E4X specification. However, you can always ask for a node with E4X even if it's not there and just see what the length of the XMLList is that you get back. Like so:

方法hasOwnProperty不适用于XML节点。我相信这是E4X规范。但是,您可以随时询问具有E4X的节点,即使它不在那里,只看到您回来的XMLList的长度。像这样:

trace(myXML[p].length());

EDIT: As noted below, I was wrong about the hasOwnProperty bit. It does work with XML, and it is a namespace issue that's causing your problem. You can make sure your XML is using the correct namespace by using this handy snippet:

编辑:如下所述,我对hasOwnProperty位错了。它确实与XML一起使用,它是一个导致问题的命名空间问题。您可以使用这个方便的代码段确保您的XML使用正确的命名空间:

if (myXML.namespace("") != undefined) {
    default xml namespace = myXML.namespace("");
}

#2


I know this is an old thread, but in case others have this same issue, here's another way of doing it. Rather than using .length(), you can check for the existence of a namespaced node using QName like so:

我知道这是一个旧线程,但如果其他人有同样的问题,这是另一种方法。您可以使用QName检查是否存在命名空间节点,而不是使用.length(),如下所示:


var xml:XML = 
<ns:outernode xmlns:ns="urn:ns:1.0">
    <ns:innernode>Here is some text content</ns:innernode>
</ns:outernode>;
var ns:Namespace = new Namespace("urn:ns:1.0");
var q:QName = new QName(ns, "innernode");
var b:Boolean = xml.hasOwnProperty(q);
trace("Has innernode: " + b);

This will return true. The QName q represents the fully qualified nodename "ns:innernode".

这将返回true。 QName q表示完全限定的节点名称“ns:innernode”。

#3


I think the problem resides in the namespace. Try it without, or try xmlns:letters = "letters"

我认为问题在于命名空间。尝试不用,或尝试xmlns:letters =“letters”