复制代码 代码如下:
function createXmlTree(node, indent) {
if (node == null)
return "";
var str = "";
switch (node.nodeType) {
case 1: // Element
str += "<div class='element'><<span class='elementname'>" + node.nodeName + "</span>";
var attrs = node.attributes;
for (var i = 0; i < attrs.length; i++)
str += createXmlAttribute(attrs[i]);
if (!node.hasChildNodes())
return str + "/></div>";
str += "><br />";
var cs = node.childNodes;
for (var i = 0; i < cs.length; i++)
str += createXmlTree(cs[i], indent + 3);
str += "</<span class='elementname'>" + node.nodeName + "</span>></div>";
break;
case 9: // Document
var cs = node.childNodes;
for (var i = 0; i < cs.length; i++)
str += createXmlTree(cs[i], indent);
break;
case 3: // Text
if (!/^\s*$/.test(node.nodeValue))
str += "<span class='text'>" + node.nodeValue + "</span><br />";
break;
case 7: // ProcessInstruction
str += "<?" + node.nodeName;
var attrs = node.attributes;
for (var i = 0; i < attrs.length; i++)
str += createXmlAttribute(attrs[i]);
str+= "?><br />"
break;
case 4: // CDATA
str = "<div class='cdata'><![CDATA[<span class='cdata-content'>" +
node.nodeValue +
"</span>]" + "]></div>";
break;
case 8: // Comment
str = "<div class='comment'><!--<span class='comment-content'>" +
node.nodeValue +
"</span>--></div>";
break;
case 10:
str = "<div class='doctype'><!DOCTYPE " + node.name;
if (node.publicId) {
str += " PUBLIC \"" + node.publicId + "\"";
if (node.systemId)
str += " \"" + node.systemId + "\"";
}
else if (node.systemId) {
str += " SYSTEM \"" + node.systemId + "\"";
}
str += "></div>";
// TODO: Handle custom DOCTYPE declarations (ELEMENT, ATTRIBUTE, ENTITY)
break;
default:
//alert(node.nodeType + "\n" + node.nodeValue);
inspect(node);
}
return str;
}
function inspect(obj) {
var str = "";
for (var k in obj)
str += "obj." + k + " = " + obj[k] + "\n";
window.alert(str);
}
function createXmlAttribute(a) {
return " <span class='attribname'>" + a.nodeName + "</span><span class='attribvalue'>=\"" + a.nodeValue + "\"</span>";
}
复制代码 代码如下: