1. 程式人生 > >js 獲取 網頁原始碼

js 獲取 網頁原始碼

JS程式碼:
複製程式碼 程式碼如下:
<script language="javascript" type="text/javascript">
//需要讀取的xml檔案
var uRl = "jsReadXml.xml";
var xmlDoc;
//初始化,給上述定義變數賦值
// function showcurcity(){
if(window.ActiveXObject)
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false"
xmlDoc.load(uRl);
getvalue();
}
else if(document.implementation && document.implementation.createDocument)
{
xmlDoc=document.implementation.createDocument("", "root", null);
xmlDoc.load(uRl);
xmlDoc.onload=getvalueff;
}
// else {
// return null;
// }
// }
function getvalue()
{
var nodes=xmlDoc.documentElement.childNodes;
var now=new Date();
for(i=0;i<nodes.length;i++){
var value=nodes.item(i).childNodes.item(0).text ;
alert(nodes.item(i).childNodes.item(1).text);
alert(nodes.item(i).childNodes.item(2).text);
}
}
function getvalueff(){
var cCode=xmlDoc.getElementsByTagName("date");
var now = new Date();
var nowvalue=now.getFullYear()+'年'+(now.getMonth()+1)+'月'+now.getDate()+'日';
for(i=0;i<cCode.length;i++)
{
alert(xmlDoc.getElementsByTagName("imgsrc")[i].childNodes[0].nodeValue);
alert(xmlDoc.getElementsByTagName("title")[i].childNodes[0].nodeValue);
alert(xmlDoc.getElementsByTagName("url")[i].childNodes[0].nodeValue);
}
}
</script>

xml程式碼:
複製程式碼 程式碼如下:
<?xml version="1.0" encoding="UTF-8" ?>
<root>
<imags>
<imgsrc>images/ad-01.jpg</imgsrc>
<title>胡志明市</title>
<url>http://torchrelay.beijing2008.cn/cn/journey/hochiminhcity/</url>
</imags>
<imags>
<imgsrc>images/ad-02.jpg</imgsrc>
<title>香港2</title>
<url>http://torchrelay.beijing2008.cn/cn/journey/hongkong/</url>
</imags>
<imags>
<imgsrc>images/ad-03.jpg</imgsrc>
<title>香港3</title>
<url>http://torchrelay.beijing2008.cn/cn/journey/hongkong/</url>
</imags>
<imags>
<imgsrc>images/ad-04.jpg</imgsrc>
<title>香港4</title>
<url>http://torchrelay.beijing2008.cn/cn/journey/hongkong/</url>
</imags>
<imags>
<imgsrc>images/ad-05.jpg</imgsrc>
<title>香港5</title>
<url>http://torchrelay.beijing2008.cn/cn/journey/hongkong/</url>
</imags>
</root>

下面的文章也可以一同參考下
我看IE與FF------Js讀取xml檔案
我幾天為了實現一個小小的功能(奧運聖火傳遞路線).Js讀取了下xml檔案,為了適應IE與FF還是費了一點時間的.
複製程式碼 程式碼如下:
<script language="javascript" type="text/javascript">
var xmlDoc;
function showcurcity()
{
if(window.ActiveXObject)
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false"
xmlDoc.load("OlyCity.xml");
getvalue();
}
else if(document.implementation && document.implementation.createDocument)
{
xmlDoc=document.implementation.createDocument("", "root", null);
xmlDoc.load('OlyCity.xml');
xmlDoc.onload=getvalueff;
}
else
{
return null;
}
}
function getvalue()
{
var nodes=xmlDoc.documentElement.childNodes;
var now=new Date();
for(i=0;i<nodes.length;i++){
var value=nodes.item(i).childNodes.item(0).text ;
if(value==now.toLocaleDateString())
{
window.document.getElementById('curcity').innerText=nodes.item(i).childNodes.item(1).text;
window.document.getElementById('curcity').href =nodes.item(i).childNodes.item(2).text;
}
}
}
function getvalueff(){
var cCode=xmlDoc.getElementsByTagName("date");
var now = new Date();
var nowvalue=now.getFullYear()+'年'+(now.getMonth()+1)+'月'+now.getDate()+'日';
for(i=0;i<cCode.length;i++)
{
var value=cCode[i].childNodes[0].nodeValue;
if(value==nowvalue)
{
window.document.getElementById('curcity').href =xmlDoc.getElementsByTagName("url")[i].childNodes[0].nodeValue;;
window.document.getElementById('curcity').childNodes[0].nodeValue=xmlDoc.getElementsByTagName("city")[i].childNodes[0].nodeValue;
}
}
}
setInterval(showcurcity,1000);
</script>

以下是xml的片斷.
複製程式碼 程式碼如下:
<?xml version="1.0" encoding="gb2312" ?>
<root>
<citymessage>
<date>2008年4月29日</date>
<city>胡志明市</city>
<url>http://torchrelay.beijing2008.cn/cn/journey/hochiminhcity/</url>
</citymessage>
<citymessage>
<date>2008年4月30日</date>
<city>香港</city>
<url>http://torchrelay.beijing2008.cn/cn/journey/hongkong/</url>
</citymessage>
</root>

也可以非同步讀取檔案

<!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></title>
    <script language="javascript" type="text/javascript">
        //需要讀取的xml檔案
      ///// var uRl = "http://localhost:2301/GetOffSet.ashx";
        var uRl = "http://192.168.1.172/WcfMapServer/GetOffSet.ashx?maptype=3&lnglats=113.2,22.3";
       
        var xmlDoc;
        //初始化,給上述定義變數賦值
        // function showcurcity(){
        if (window.ActiveXObject) {
            xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async = "true"
            xmlDoc.load(uRl);
            xmlDoc.onreadystatechange = function writeSource() {
                if (xmlDoc.readyState == 4) {
                    if (xmlDoc.parseError.errorCode != 0) {
                       var txt = "err";
                    }
                    else {


                        var nodes = xmlDoc.documentElement.childNodes;
                        var now = new Date();
                        for (i = 0; i < nodes.length; i++) {
                            var value = nodes.item(i).childNodes.item(0).text;
                            alert(nodes.item(i).childNodes.item(0).text);
                            alert(nodes.item(i).childNodes.item(1).text);
                        }
                    }
                }
            };
          //  getvalue();
        }
        else if (document.implementation && document.implementation.createDocument) {
            xmlDoc = document.implementation.createDocument("", "root", null);
            xmlDoc.load(uRl);
            xmlDoc.onload = getvalueff;
        }
 
        function getvalue() {
         
            var nodes = xmlDoc.documentElement.childNodes;
            var now = new Date();
            for (i = 0; i < nodes.length; i++) {
                var value = nodes.item(i).childNodes.item(0).text;
                alert(nodes.item(i).childNodes.item(0).text);
                alert(nodes.item(i).childNodes.item(1).text);
            }
        }
        function getvalueff() {
            var cCode = xmlDoc.getElementsByTagName("date");
            var now = new Date();
            var nowvalue = now.getFullYear() + '年' + (now.getMonth() + 1) + '月' + now.getDate() + '日';
            for (i = 0; i < cCode.length; i++) {
                alert(xmlDoc.getElementsByTagName("imgsrc")[i].childNodes[0].nodeValue);
                alert(xmlDoc.getElementsByTagName("title")[i].childNodes[0].nodeValue);
                alert(xmlDoc.getElementsByTagName("url")[i].childNodes[0].nodeValue);
            }
        }
    </script>
</head>
<body>
</body>
</html>