javascript生成解析dom的CDATA类型的字段的代码

2018-09-23 20:31

阅读:607

  两段demo代码如下(forieonly):
<scripttype=text/javascript>
//demo1
varxmlDoc=newActiveXObject(Msxml2.DOMDocument);
varroot;
varCDATASection;
xmlDoc.async=false;
xmlDoc.loadXML(<a/>);
root=xmlDoc.documentElement;
CDATASection=xmlDoc.createCDATASection(HelloWorld!);
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>

<scripttype=text/javascript>
//demo2
varxmlDoc=newActiveXObject(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(forfirefox)
<!DOCTYPEHTMLPUBLIC-//W3C//DTDHTML4.0Transitional//EN>
<HTML>
<HEAD>
<TITLE>使firefox对xml的处理兼容IE的selectSingleNodeselectNodes方法</TITLE>
<METANAME=AuthorCONTENT=emu>
<METANAME=KeywordsCONTENT=firefoxIEselectSingleNodeselectNodes>
<METANAME=DescriptionCONTENT=使firefox可以支持selectSingleNodeselectNodes方法>
<SCRIPTLANGUAGE=JavaScript>
<!--
varisIE=!!document.all;

functionparseXML(st){
if(isIE){
varresult=newActiveXObject(microsoft.XMLDOM);
result.loadXML(st);
}else{
varparser=newDOMParser();
varresult=parser.parseFromString(st,text/xml);
}
returnresult;
}

if(!isIE){
varex;
XMLDocument.prototype.__proto__.__defineGetter__(xml,function(){
try{
returnnewXMLSerializer().serializeToString(this);
}catch(ex){
vard=document.createElement(div);
d.appendChild(this.cloneNode(true));
}
});
Element.prototype.__proto__.__defineGetter__(xml,function(){
try{
returnnewXMLSerializer().serializeToString(this);
}catch(ex){
vard=document.createElement(div);
d.appendChild(this.cloneNode(true));
returnd.innerHTML;
}
});
XMLDocument.prototype.__proto__.__defineGetter__(text,function(){
returnthis.firstChild.textContent
});
Element.prototype.__proto__.__defineGetter__(text,function(){
returnthis.textContent
});




XMLDocument.prototype.selectSingleNode=Element.prototype.selectSingleNode=function(xpath){
varx=this.selectNodes(xpath)
if(!xx.length<1)returnnull;
returnx[0];
}
XMLDocument.prototype.selectNodes=Element.prototype.selectNodes=function(xpath){
varxpe=newXPathEvaluator();
varnsResolver=xpe.createNSResolver(this.ownerDocument==null?
this.documentElement:this.ownerDocument.documentElement);
varresult=xpe.evaluate(xpath,this,nsResolver,0,null);
varfound=[];
varres;
while(res=result.iterateNext())
found.push(res);
returnfound;
}
}

varx=parseXML(<people><personfirst-name=\eric\middle-initial=\H\last-name=\jung\><addressstreet=\321southst\city=\denver\state=\co\country=\usa\/><addressstreet=\123mainst\city=\arlington\state=\ma\country=\usa\/></person><personfirst-name=\jed\last-name=\brown\><addressstreet=\321northst\city=\atlanta\state=\ga\country=\usa\/><addressstreet=\123westst\city=\seattle\state=\wa\country=\usa\/><addressstreet=\321southavenue\city=\denver\state=\co\country=\usa\/></person></people>);

alert(搜索所有人的姓氏(last-name))
varresults=x.selectNodes(//person/@last-name);
for(vari=0;i<results.length;i++)
alert(Person#+i+hasthelastname+results[i].nodeValue);

alert(搜索第二个人);
//IE是以0为下标基数的,而不是1
if(!document.all)
results=x.selectSingleNode(/people/person[2]);
else
results=x.selectSingleNode(/people/person[1]);
alert(results.xml)

alert(获得住址在donver街上的人);
results=x.selectNodes(//person[address/@city=denver]);
for(vari=0;i<results.length;i++)alert(results[i].xml)

if(!document.all){
//获得所有街名中带south的地址
results=x.selectNodes(//address[contains(@street,south)]);
alert(results[0].xml);
}else{
alert(IE不支持//address[contains(@street,south)]这种查询方式)
}
//-->
</SCRIPT>
</HEAD>

<BODY>

</BODY>
</HTML>

//demo4(crossbrower)


评论


亲,登录后才可以留言!