实现动态修改xml文件;如下:
index.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/xml; charset=utf-8">
<title></title>
<script type="text/javascript">
var GetNodeValue = function(obj)
{
var str = "";
if(window.ActiveXObject) //IE
{
str = obj.text;
}
else //Mozilla
{
try
{
str = obj.childNodes[0].nodeValue;
}
catch(ex)
{
str = "";
}
}
return str;
}if(document.implementation && document.implementation.createDocument)
{
XMLDocument.prototype.loadXML = function(xmlString)
{
var childNodes = this.childNodes;
for (var i = childNodes.length - 1; i >= 0; i--)
this.removeChild(childNodes[i]);var dp = new DOMParser();
var newDOM = dp.parseFromString(xmlString, "text/xml");
var newElt = this.importNode(newDOM.documentElement, true);
this.appendChild(newElt);
};// check for XPath implementation
if( document.implementation.hasFeature("XPath", "3.0") )
{
// prototying the XMLDocument
XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
{
if( !xNode ) { xNode = this; }
var oNSResolver = this.createNSResolver(this.documentElement)
var aItems = this.evaluate(cXPathString, xNode, oNSResolver,
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
var aResult = [];
for( var i = 0; i < aItems.snapshotLength; i++)
{
aResult[i] = aItems.snapshotItem(i);
}
return aResult;
}// prototying the Element
Element.prototype.selectNodes = function(cXPathString)
{
if(this.ownerDocument.selectNodes)
{
return this.ownerDocument.selectNodes(cXPathString, this);
}
else{throw "For XML Elements Only";}
}
}// check for XPath implementation
if( document.implementation.hasFeature("XPath", "3.0") )
{
// prototying the XMLDocument
XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode)
{
if( !xNode ) { xNode = this; }
var xItems = this.selectNodes(cXPathString, xNode);
if( xItems.length > 0 )
{
return xItems[0];
}
else
{
return null;
}
}
// prototying the Element
Element.prototype.selectSingleNode = function(cXPathString)
{
if(this.ownerDocument.selectSingleNode)
{
return this.ownerDocument.selectSingleNode(cXPathString, this);
}
else{throw "For XML Elements Only";}
}
}
}
function loadXML()
{
var xmlHttp;
var name;
var xmlHttp2;
if(window.ActiveXObject)
{
xmlHttp2= new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlHttp2 = new XMLHttpRequest();
}
if(xmlHttp2){
xmlHttp2.open("GET", "ajaxtest.php?id=dsfwefwef", false);
xmlHttp2.onreadystatechange = function()
{
if(xmlHttp2.readyState == 4)
{
if (xmlHttp2.status == 200)
{
}
}
}
xmlHttp2.send(null);
}if(window.ActiveXObject)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
try
{
xmlHttp.onreadystatechange = function()
{
if(xmlHttp.readyState == 4)
{
if (xmlHttp.status == 200)
{
// 取得XML的DOM对象
var xmlDOM = xmlHttp.responseXML;
// 取得XML文档的根
var root = xmlDOM.documentElement;
try
{
var items = root.selectNodes("//urlset/url");
for(var i=0;i<items.length;i++)
{
//取得XML文件中内容:)
var strTitle = GetNodeValue(items[i].selectSingleNode("loc"));
var strLastMod=GetNodeValue(items[i].selectSingleNode("lastmod"));
var msg=document.getElementById("msg");
msg.innerHTML+=strTitle+" "+strLastMod+"<br/>";
}
}
catch(exception)
{
alert("error");
}
}
}
}
xmlHttp.open("GET", "test1.xml", true);
xmlHttp.send(null);
}
catch(exception)
{
alert("您要访问的资源不存在!");
}
}</script>
</head>
<body οnlοad="loadXML();">
<div id="msg" style="width:500px; height:auto;"></div>
</body>
</html>ajaxtest.php:
<?php
//Creates XML string and XML document using the DOM
$dom = new DomDocument('1.0');
//add root - <books>
$books = $dom->appendChild($dom->createElement('urlset'));
//add <book> element to <books>
$book = $books->appendChild($dom->createElement('url'));
// $book2 = $books->appendChild($dom->createElement('url'));
//add <title> element to <book>
$title1 = $book->appendChild($dom->createElement('loc'));
$title2 = $book->appendChild($dom->createElement('lastmod'));
$title3 = $book->appendChild($dom->createElement('changefreq'));
$title4 = $book->appendChild($dom->createElement('priority'));
//add <title> text node element to <title>
$title1->appendChild($dom->createTextNode('http://blog.const.net.cn/alexa/index.xml'));
$title2->appendChild($dom->createTextNode("id=$_GET[id] 2007-08-29"));
$title3->appendChild($dom->createTextNode('Great American Novel'));
$title4->appendChild($dom->createTextNode('Great American Novel'));
//generate xml
$dom->formatOutput = true; // set the formatOutput attribute of
//domDocument to true
// save XML as string or file
$test1 = $dom->saveXML(); // put string in test1
$dom -> save('test1.xml'); // save as file?>
php写xml,再把改写的xml读出来...
最新推荐文章于 2024-09-15 13:15:46 发布