javascript Xml 첨삭 검사(IE 아래)동작 구현 코드

3770 단어 자바 scriptXml
html 파일:
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>js Xml (IE )</title>
<script type="text/javascript"><!--
/* :
1.xpath , root person name .
*/
var xmlDoc;
var rootNode; //
// Xml
function loadXml(){
try{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;//
xmlDoc.load("XmlFile.xml");//load ,loadXML .
rootNode = xmlDoc.documentElement;
}catch(e) {alert(e.message)}
}
// Xml
function outXml(){
var divXml=document.getElementById("divXml");
divXml.innerHTML=xmlDoc.xml;// xml , xml .?
alert(xmlDoc.xml);
}
//
function addXml(){
// , text
var newName = xmlDoc.createElement("name");
newName.text = "crane";
var newGender = xmlDoc.createElement("gender");
newGender.text = "female";
// , appendChild(childNode);
var newPerson = xmlDoc.createElement("person");
// id
newPerson.setAttribute("id","2");
newPerson.appendChild(newName);
newPerson.appendChild(newGender);
//
rootNode.appendChild(newPerson);
alert(xmlDoc.xml);
}
//
function deleteXml(){
//
var singleNode = xmlDoc.selectSingleNode("/root/person[name='tree']");
//
singleNode.parentNode.removeChild(singleNode);
alert(xmlDoc.xml);
}
//
function updateXml(){
var singleNode = xmlDoc.selectSingleNode("/root/person[name='crane']");
singleNode.childNodes[0].text = "updated";
alert(xmlDoc.xml);
}
//
function queryXml(){
//alert(rootNode.nodeName);//
//alert(rootNode.text);//
//xPath
//var nodes = xmlDoc.selectNodes("/root/person");
//alert(nodes[0].text);
//
/*
1."/root/person[name='tree']" "/root[person/name='tree']" person
*/
var singleNode = xmlDoc.selectSingleNode("/root/person[gender='female']");//
alert(singleNode.text);
alert(singleNode.getAttribute("id"));
// xpath
var sglNode = xmlDoc.selectSingleNode("/root[person/gender='male']");// . .
alert(sglNode.text);
// xml
//alert(xmlDoc.xml);
}
// --></script>
</head>
<body>
<div id="divXml"></div>
<input type="button" value="load" onclick="loadXml();" />
<input type="button" value="show" onclick="outXml();" />
<input type="button" value="add" onclick="addXml();" />
<input type="button" value="delete" onclick="deleteXml();" />
<input type="button" value="update" onclick="updateXml();" />
<input type="button" value="query" onclick="queryXml();" />
</body>
</html>
Xml 파일:
 
<?xml version="1.0" encoding="utf-8" ?>
<root>
<person id="1">
<name>tree</name>
<gender>male</gender>
</person>
</root>

좋은 웹페이지 즐겨찾기