Actionscript 2.0 XML get child by name
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" no开发者_运维百科de 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...
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.
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;
}
精彩评论