第一个ws.js,摘要代码如下:
var XML = {
createDocumentQName : function(qname) {
return XML.createDocument(qname.namespace,qname.value_of());
},
createDocument : function(namespace,nodename) {
return Try.these(
function() {
var doc = new ActiveXObject('Msxml2.XMLDOM');
var root = XML.createElementNS(doc, nodename, namespace);
doc.documentElement = root;
return doc;
},
function() {
var doc = new ActiveXObject('Microsoft.XMLDOM')
var root = XML.createElementNS(doc, nodename, namespace);
doc.documentElement = root;
return doc;
},
function() {
return document.implementation.createDocument(
namespace,
nodename,
null)
}
) || false;
},
createElementNS : function(document,nodename,namespace) {
var el = Try.these(
function() {
var el = null;
if (namespace) {
el = document.createNode(1,nodename,namespace);
} else {
el = document.createNode(1,nodename,"");
}
return el;
},
function() {
var el = null;
if (namespace) {
el = document.createElementNS(namespace,nodename);
} else {
el = document.createElement(nodename);
}
return el;
}
) || false;
return el;
},
createElementQName : function(document,qname) {
return XML.createElementNS(document,qname.value_of(),qname.namespace);
},
createAttributeNS : function(document,nodename,namespace,value) {
var attr = Try.these(
function() { return document.createNode(2,nodename,namespace)},
function() { return document.createAttributeNS(namespace,nodename)}
) || false;
attr.nodeValue = value;
return attr;
},
createAttributeQName : function(document,qname,value) {
return XML.createAttributeNS(document,qname.value_of(),qname.namespace,value);
},
createAttribute : function(document,nodename,value) {
var attr = Try.these(
function() { return document.createNode(2, nodename)},
function() { return document.createAttribute(nodename)}
) || false;
attr.nodeValue = value;
return attr;
},
createText : function(document,value) {
var node = Try.these(
function() { return document.createTextNode(value) }
) || false;
return node;
},
createCDATA : function(document,value) {
var node = Try.these(
function() { return document.createCDATASection(value) }
) || false;
return node;
},
getElementsByQName : function(element, qname) {
var nl = null;
if(!element.getElementsByTagNameNS) {
nl = new Array();
var nodes = element.getElementsByTagName(qname.value_of());
for (var n = 0; n < nodes.length; n++) {
if (nodes[n].namespaceURI == qname.namespace) {
nl.push(nodes[n]);
}
}
} else {
nl = element.getElementsByTagNameNS(qname.namespace,qname.localpart);
}
return nl;
}
}
回复 更多评论