Actionscript 2.0 XML 按名称获取子项

发布于 2024-12-03 12:32:17 字数 208 浏览 1 评论 0原文

现在我正在将一些 AS3.0 应用程序移植到 AS2.0(不要问我为什么:)) 在 AS3.0 中,我以简单的方式获取 xml 中名为“x”的子节点:

xml.child(“x”)。

如何在 AS2.0 中获取“x”节点? 当然,我可以编写一个循环来枚举 xml 的子级并将它们的名称与“x”进行比较。 但我希望有一个更简单的方法......

Now I'm porting some AS3.0 app to AS2.0 (don't ask me why :))
In AS3.0 I get child named "x" of my xml in a simple manner :

xml.child("x").

How can I get "x" node in AS2.0?
Of course, I can write a loop that enumerates children of xml and compares their names to "x".
But I hope there exists a simpler way...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不必你懂 2024-12-10 12:32:17

不是 100% 确定,但使用 XPath API 应该更容易(PDF链接)在as2中而不是默认的节点循环。应该还有一些被遗忘的资源 在网络上。

Not 100% sure, but it should be easier to use the XPath API(PDF link) in as2 instead of the default node looping. There should still be some forgotten resources out there on the web.

昨迟人 2024-12-10 12:32:17

解析另存为 XML 2003 的 Excel 文件
(另存为xml后,用记事本打开并删除前两行)

// XML
var main_xml:XML = new XML();
main_xml.ignoreWhite = true;
main_xml.load(_global.path + "main.xml"+_global.randomprm)
main_xml.onLoad = function(success) {
    var s:String = xmlValueOnPageByColTitleAndRow("your_worksheet_title", "cell_value_in_any_row_in_interested_column_OR_column_title_in_table", 2); // 2 - row number in table
    trace(s);
};

function xmlValueOnPageByColTitleAndRow(PageName:String, ColTitle:String, Row:Number) {
    var worksheet:XMLNode = xmlNodeByNameWithValue(main_xml.firstChild, "Worksheet", PageName);
    var column = xmlColumnByValue(worksheet, ColTitle);
    var rowNode:XMLNode = xmlNodeByNameWithOrder(worksheet, "Row", Row);
    var cell:XMLNode = xmlNodeByNameWithOrder(rowNode, "Cell", column);
    return cell.firstChild.firstChild.nodeValue;
}

function xmlColumnByValue(node:XMLNode, Value:String) {
    if (node.firstChild.nodeName == "Cell") {
        var n = 0;
        for (var childnode:XMLNode = node.firstChild; childnode != null; childnode = childnode.nextSibling, n++) {
            if (childnode.firstChild.firstChild.nodeValue == Value) return n;
        }
    } else 
    for (var childnode:XMLNode = node.firstChild; childnode != null; childnode = childnode.nextSibling) {
        res = xmlColumnByValue(childnode, Value); 
        if (res > -1) return res;
    }
    return -1;
}

function xmlNodeByNameWithOrder(node:XMLNode, Name:String, Order:Number) {
    var n = 0;
    for (var childnode:XMLNode = node.firstChild; childnode != null; childnode = childnode.nextSibling)
        if (childnode.nodeName == Name) {
            n++;
            if (n == Order) return childnode;
        }
    for (var childnode:XMLNode = node.firstChild; childnode != null; childnode = childnode.nextSibling) {
        finded = xmlNodeByNameWithOrder(childnode, Name, Order); 
        if (finded != NULL) return finded;
    }
    return NULL;
}

function xmlHasValue(node:XMLNode, Value:String) {
    for (attr in node.attributes) {
        if (node.attributes[attr] == Value) return true;
    }
    return false;
}

function xmlNodeByNameWithValue(node:XMLNode, Name:String, Value:String) {
    if (node.nodeName == Name) {
        if (xmlHasValue(node, Value)) 
            return node;
        else 
            return null;
    } else {
        for (var childnode:XMLNode = node.firstChild; childnode != null; childnode = childnode.nextSibling) {
            finded = xmlNodeByNameWithValue(childnode, Name, Value); 
            if (finded != NULL) return finded;
        }
    }
    return NULL;
}

var depth = 0;
var str = "";
function traceAllChilds(node:XMLNode) {
    depth++;
    str = ""; for (var i=0; i<depth; i++) str = str + " ";
    if (node.nodeValue) trace(str + node.nodeName + " = " + node.nodeValue);
        else trace(str + node.nodeName);
    for (attr in node.attributes)
        trace (str + " [" + attr + " = " + node.attributes[attr]+"]");
    for (var childnode:XMLNode = node.firstChild; childnode != null; childnode = childnode.nextSibling)
        traceAllChilds(childnode); 
    str = ""; for (var i=0; i<depth; i++) str = str + " ";
    trace(str+""+ node.nodeName);
    depth--;
    return true;
}

Parsing Excel file saved as XML 2003
(after saving as xml, open in notepad and delete first two rows)

// XML
var main_xml:XML = new XML();
main_xml.ignoreWhite = true;
main_xml.load(_global.path + "main.xml"+_global.randomprm)
main_xml.onLoad = function(success) {
    var s:String = xmlValueOnPageByColTitleAndRow("your_worksheet_title", "cell_value_in_any_row_in_interested_column_OR_column_title_in_table", 2); // 2 - row number in table
    trace(s);
};

function xmlValueOnPageByColTitleAndRow(PageName:String, ColTitle:String, Row:Number) {
    var worksheet:XMLNode = xmlNodeByNameWithValue(main_xml.firstChild, "Worksheet", PageName);
    var column = xmlColumnByValue(worksheet, ColTitle);
    var rowNode:XMLNode = xmlNodeByNameWithOrder(worksheet, "Row", Row);
    var cell:XMLNode = xmlNodeByNameWithOrder(rowNode, "Cell", column);
    return cell.firstChild.firstChild.nodeValue;
}

function xmlColumnByValue(node:XMLNode, Value:String) {
    if (node.firstChild.nodeName == "Cell") {
        var n = 0;
        for (var childnode:XMLNode = node.firstChild; childnode != null; childnode = childnode.nextSibling, n++) {
            if (childnode.firstChild.firstChild.nodeValue == Value) return n;
        }
    } else 
    for (var childnode:XMLNode = node.firstChild; childnode != null; childnode = childnode.nextSibling) {
        res = xmlColumnByValue(childnode, Value); 
        if (res > -1) return res;
    }
    return -1;
}

function xmlNodeByNameWithOrder(node:XMLNode, Name:String, Order:Number) {
    var n = 0;
    for (var childnode:XMLNode = node.firstChild; childnode != null; childnode = childnode.nextSibling)
        if (childnode.nodeName == Name) {
            n++;
            if (n == Order) return childnode;
        }
    for (var childnode:XMLNode = node.firstChild; childnode != null; childnode = childnode.nextSibling) {
        finded = xmlNodeByNameWithOrder(childnode, Name, Order); 
        if (finded != NULL) return finded;
    }
    return NULL;
}

function xmlHasValue(node:XMLNode, Value:String) {
    for (attr in node.attributes) {
        if (node.attributes[attr] == Value) return true;
    }
    return false;
}

function xmlNodeByNameWithValue(node:XMLNode, Name:String, Value:String) {
    if (node.nodeName == Name) {
        if (xmlHasValue(node, Value)) 
            return node;
        else 
            return null;
    } else {
        for (var childnode:XMLNode = node.firstChild; childnode != null; childnode = childnode.nextSibling) {
            finded = xmlNodeByNameWithValue(childnode, Name, Value); 
            if (finded != NULL) return finded;
        }
    }
    return NULL;
}

var depth = 0;
var str = "";
function traceAllChilds(node:XMLNode) {
    depth++;
    str = ""; for (var i=0; i<depth; i++) str = str + " ";
    if (node.nodeValue) trace(str + node.nodeName + " = " + node.nodeValue);
        else trace(str + node.nodeName);
    for (attr in node.attributes)
        trace (str + " [" + attr + " = " + node.attributes[attr]+"]");
    for (var childnode:XMLNode = node.firstChild; childnode != null; childnode = childnode.nextSibling)
        traceAllChilds(childnode); 
    str = ""; for (var i=0; i<depth; i++) str = str + " ";
    trace(str+""+ node.nodeName);
    depth--;
    return true;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文