JavaScript to generate/parse Dom's CDATA type fields

Source: Internet
Author: User
Tags cdata xpath

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
<

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.