two demo code as follows (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;
}
}
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
if (! document.all)
Results = X.selectsinglenode ("/people/person[2]");
Else
Results = X.selectsinglenode ("/people/person[1]");
Alert (Results.xml)
Alert ("person who obtains an address in Donver Street");
Results = X.selectnodes ("//person[address/@city = ' Denver ']");
for (var i = 0; I < Results.length;i + +) alert (results[i].xml)
if (! document.all) {
Get the address of all street names with South.
Results = X.selectnodes ("//address[contains (@street, ' South ')");
Alert (results[0].xml);
} else {
Alert ("IE does not support//address[contains (@street, ' South ')] this way of querying")
}
-->
</SCRIPT >
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.