Two demo code below (for IE only):
<script type= "Text/javascript" >
//demo1
var xmldoc = new ActiveXObject ("msxml2.domdocument");
var root;
var CDATASection;
xmldoc.async = false;
Xmldoc.loadxml ("<a/>");
root = xmldoc.documentelement;
cdatasection = xmldoc.createcdatasection ("Hello World!");
Root.appendchild (cdatasection);
b=xmldoc.createelement ("Test")
b.text= "Hahahahaha"
Root.appendchild (b);
Alert (root.xml);
for (i =0;i< root.childnodes.length;i++)
{
if (root.childnodes[i].nodetype==4)
Alert ( Root.childnodes[i].nodevalue)
}
</script>
<script type= "Text/javascript" >
Demo2
var xmldoc = new ActiveXObject ("Msxml2.domdocument");
Xmldoc.async = false;
Xmldoc.loadxml ("<a><![ Cdata[aaaaaaaaaaaaaaaaaaaaa]]></a> ");
root = xmldoc.documentelement;
for (i =0;i<root.childnodes.length;i++)
{
if (root.childnodes[i].nodetype==4)
Alert (Root.childnodes[i].nodevalue)
}
</script>
Demo3 (for Firefox)
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
< HTML >
< head >
< title > selectSingleNode SelectNodes method to enable Firefox to process XML (IE) </title >
< META NAME = "Author" CONTENT = "EMU" >
< META NAME = "Keywords" CONTENT = "Firefox IE selectsinglenode selectnodes" >
< META NAME = "Description" CONTENT = "Enable Firefox to support selectSingleNode SelectNodes method" >
< SCRIPT LANGUAGE = "JavaScript" >
<!--
var Isie =!! document.all;
function Parsexml (ST) {
if (Isie) {
var result = new ActiveXObject ("Microsoft". XMLDOM ");
Result.loadxml (ST);
} else {
var parser = new Domparser ();
var result = parser.parsefromstring (St, "Text/xml");
}
return result;
}
if (! Isie) {
var ex;
XMLDOCUMENT.PROTOTYPE.__PROTO__.__DEFINEGETTER__ ("XML", function () {
try {
Return to New XMLSerializer (). serializetostring (this);
catch (ex) {
var d = document.createelement ("div");
D.appendchild (this. CloneNode (true));
return d.innerhtml;
}
});
ELEMENT.PROTOTYPE.__PROTO__.__DEFINEGETTER__ ("XML", function () {
try {
Return to New XMLSerializer (). serializetostring (this);
catch (ex) {
var d = document.createelement ("div");
D.appendchild (this. CloneNode (true));
return d.innerhtml;
}
});
xmldocument.prototype.__proto__.__definegetter__ ("Text", function () {
return this. firstchild.textcontent
});
element.prototype.__proto__.__definegetter__ ("Text", function () {
return this. textcontent
});
XMLDocument.prototype.selectSingleNode = Element.prototype.selectSingleNode = function (XPath) {
var x = this. selectnodes (XPath)
if (! x | | X.length < 1) return null;
return x[0];
}
XMLDocument.prototype.selectNodes = Element.prototype.selectNodes = function (XPath) {
var xpe = new Xpathevaluator ();
var nsresolver = Xpe.creatensresolver (this. ownerdocument = null?
this. Documentelement:this. ownerdocument.documentelement);
var result = Xpe.evaluate (XPath, this, nsresolver, 0, NULL);
var found = [];
var res;
while (res = Result.iteratenext ())
Found.push (RES);
return found;
}
}
var x = parsexml ("<people> <person first-name=/" eric/" middle-initial=/"h/" last-name=/"jung/" > <address street=/"321 sou th st/" city=/" denver/" state=/" co/" country=/" usa/"/> <add" ress street=/"123 main st/" city=/"arlington/" state=/"ma/" country=/"usa/" /> </person> <person first-name=/"jed/" last-name=/"brown/" > <address street=/"321 north st/" city=/atlanta/" state=/" ga/" country=/" usa/"/> <address street=/" 123 west st/" c" ity=/"seattle/" state=/"wa/" country=/"usa/"/> <address street=/" 321 south avenue/" city=/"denver/" state=/" co/" country=/" usa/"/> </person></people>");
Alert ("Search for everyone's last name (Last-name)")
var results = x.selectnodes ("//person/@last-name");
for (var i = 0; I < Results.length;i + +)
Alert ("Person #" + i + "has"-"Last Name" + Results[i].nodevalue);
Alert ("Search for a second person");
IE is based on the subscript 0, not 1
<