AjxXmlDoc.js
Summary
No overview generated for 'AjxXmlDoc.js'
function AjxXmlDoc() {
if (!AjxXmlDoc._inited)
AjxXmlDoc._init();
}
AjxXmlDoc.prototype.toString =
function() {
return "AjxXmlDoc";
}
AjxXmlDoc._inited = false;
AjxXmlDoc._msxmlVers = null;
AjxXmlDoc.create =
function() {
var xmlDoc = new AjxXmlDoc();
var newDoc = null;
if (AjxEnv.isIE) {
newDoc = new ActiveXObject(AjxXmlDoc._msxmlVers);
newDoc.async = true;
if (AjxXmlDoc._msxmlVers == "MSXML2.DOMDocument.4.0") {
newDoc.setProperty("SelectionLanguage", "XPath");
newDoc.setProperty("SelectionNamespaces", "xmlns:zimbra='urn:zimbra' xmlns:mail='urn:zimbraMail' xmlns:account='urn:zimbraAccount'");
}
} else if (document.implementation && document.implementation.createDocument) {
newDoc = document.implementation.createDocument("", "", null);
} else {
throw new AjxException("Unable to create new Doc", AjxException.INTERNAL_ERROR, "AjxXmlDoc.create");
}
xmlDoc._doc = newDoc;
return xmlDoc;
}
AjxXmlDoc.createFromDom =
function(doc) {
var xmlDoc = new AjxXmlDoc();
xmlDoc._doc = doc;
return xmlDoc;
}
AjxXmlDoc.createFromXml =
function(xml) {
var xmlDoc = AjxXmlDoc.create();
xmlDoc.loadFromString(xml);
return xmlDoc;
}
AjxXmlDoc.getXml =
function(node) {
var ser = new XMLSerializer();
return ser.serializeToString(node);
}
AjxXmlDoc.prototype.getDoc =
function() {
return this._doc;
}
AjxXmlDoc.prototype.loadFromString =
function(str) {
var doc = this._doc;
doc.loadXML(str);
if (AjxEnv.isIE) {
if (doc.parseError.errorCode != 0)
throw new AjxException(doc.parseError.reason, AjxException.INVALID_PARAM, "AjxXmlDoc.loadFromString");
}
}
AjxXmlDoc.prototype.loadFromUrl =
function(url) {
this._doc.load(url);
}
AjxXmlDoc.prototype.toJSObject =
function(dropns, lowercase, withAttrs) {
function _node() { this.__msh_content = ''; };
_node.prototype.toString = function() { return this.__msh_content; };
function rec(i, o) {
var tags = {}, t, n;
for (i = i.firstChild; i; i = i.nextSibling) {
if (i.nodeType == 1) {
t = i.tagName;
if (dropns) t = t.replace(/^.*?:/, "");
if (lowercase) t = t.toLowerCase();
n = new _node();
if (tags[t]) {
if (tags[t] == 1) {
o[t] = [ o[t] ];
tags[t] = 2;
}
o[t].push(n);
} else {
o[t] = n;
tags[t] = 1;
}
if(withAttrs) {
if(i.attributes && i.attributes.length) {
for(var ix = 0;ix<i.attributes.length;ix++) {
attr = i.attributes[ix];
n[attr.name] = AjxUtil.isNumeric(attr.value) ? attr.value : String(attr.value);
}
}
}
rec(i, n);
} else if (i.nodeType == 3)
o.__msh_content += i.nodeValue;
}
};
var o = new _node();
rec(this._doc.documentElement, o);
return o;
};
AjxXmlDoc.prototype.getElementsByTagNameNS =
function(ns, tag) {
var doc = this.getDoc();
return AjxEnv.isIE
? doc.getElementsByTagName(ns + ":" + tag)
: doc.getElementsByTagNameNS(ns, tag);
};
AjxXmlDoc.prototype.getFirstElementByTagNameNS =
function(ns, tag) {
return this.getElementsByTagNameNS(ns, tag)[0];
};
AjxXmlDoc._init =
function() {
if (AjxEnv.isIE) {
var msxmlVers = ["MSXML4.DOMDocument", "MSXML3.DOMDocument", "MSXML2.DOMDocument.4.0",
"MSXML2.DOMDocument.3.0", "MSXML2.DOMDocument", "MSXML.DOMDocument",
"Microsoft.XmlDom"];
for (var i = 0; i < msxmlVers.length; i++) {
try {
new ActiveXObject(msxmlVers[i]);
AjxXmlDoc._msxmlVers = msxmlVers[i];
break;
} catch (ex) {
}
}
if (AjxXmlDoc._msxmlVers == null)
throw new AjxException("MSXML not installed", AjxException.INTERNAL_ERROR, "AjxXmlDoc._init");
} else if (AjxEnv.isNav) {
Document.prototype.loadXML = function(str) {
var domParser = new DOMParser();
var domObj = domParser.parseFromString(str, "text/xml");
while (this.hasChildNodes())
this.removeChild(this.lastChild);
for (var i = 0; i < domObj.childNodes.length; i++) {
var importedNode = this.importNode(domObj.childNodes[i], true);
this.appendChild(importedNode);
}
}
_NodeGetXml = function() {
var ser = new XMLSerializer();
return ser.serializeToString(this);
}
Node.prototype.__defineGetter__("xml", _NodeGetXml);
} else if (AjxEnv.isSafari) {
document.__proto__.loadXML = function(str) {
var domParser = new DOMParser();
var domObj = domParser.parseFromString(str, "text/xml");
while (this.hasChildNodes())
this.removeChild(this.lastChild);
for (var i = 0; i < domObj.childNodes.length; i++) {
var importedNode = this.importNode(domObj.childNodes[i], true);
this.appendChild(importedNode);
}
}
}
AjxXmlDoc._inited = true;
};
Documentation generated by
JSDoc on Thu Apr 27 13:58:44 2006