javascript中支持firefox的xml读取操作实例

来源:互联网 发布:手机淘宝上怎么看直播 编辑:程序博客网 时间:2024/06/05 04:24

javascript中支持Firefox的xml读取操作实例

<html>
<head>
<title>javascript中支持Firefox的xml读取操作实例</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;

    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+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+strLastMod+"<br/>";
                       }
                    }
                    catch(exception)
                    {
                        alert("error");
                    }
                }
            }
        }
        xmlHttp.open("GET", "sitemap.xml", true);
        xmlHttp.send(null);
    }
    catch(exception)
    {
      alert("您要访问的资源不存在!");
    }
}

</script>
</head>
<body onload="loadXML();">
<div id="msg" style="width:500px; height:auto;"></div>
</body>
</html>

sitemap.xml

<?xml version="1.0" encoding="utf-8" ?>
<urlset>

<url>
    <loc>http://blog.const.net.cn/alexa/index.htm</loc>
    <lastmod>2007-08-29</lastmod>
    <changefreq>daily</changefreq>
    <priority>1.0</priority>
</url>

<url>
    <loc>http://blog.const.net.cn/index/More.yhtml</loc>
    <lastmod>2007-09-03</lastmod>
    <changefreq>daily</changefreq>
    <priority>1.0</priority>
</url>

</urlset>

原创粉丝点击