最近沒事,寫了個在項目經常要取城市或省份名的方法,所以改成了一個類.方便以後調用
//********************************************************************************
//* *
// 功能描述:從XML中獲取數據
// 版本編號:owendu V1.1
// 初始創建: 2006.08.27
// 最後修改: 2006.10.08
//* *
//********************************************************************************
//根據cityid,獲取返回cityname ---------------XMLurl 指的是整個站點的路征
//頁面調用方式如下:<script language=Javascript>GetCityName("<%=cityid%>","<%=System.Configuration.ConfigurationSettings.APPSettings["LocalSiteURL"]%>")</script>
function GetCityName(cityid,XMLurl)
{
var XmlDoc_city = new ActiveXObject("MSXML2.DOMDocument.3.0");
XMLDoc_city.async=false;
XMLDoc_city.validateOnParse=false;
var Url=xmlurl+"XmlData/city.XML";
XMLDoc_city.load(Url);
//
if (XmlDoc_city.readyState == 4 && XmlDoc_city.XML != "")
{
var city;
var citIEs = XMLDoc_city.documentElement.selectNodes("city");
for (var i=0; i < citIEs.length; i++)
{
city = citIEs[i];
if (city.getAttribute("cityid") == cityid)
{
document.write(city.getAttribute("cityname"));
break;
}
//obj2.options[obj2.length] = new Option(city.getAttribute("cityname"),city.getAttribute("cityid"));
}
}
// else
// {
//alert('您的浏覽器不支持XML文件讀取,於是本頁面禁止您的操作,推薦使用IE5.0以上可以解決此問題!');
// }
}
//根據省份id,獲取返回省名 ---------------XMLurl 指的是整個站點的路征
function GetProviceName(pid,XMLurl)
{
var XmlDoc_city = new ActiveXObject("MSXML2.DOMDocument.3.0");
XMLDoc_city.async=false;
XMLDoc_city.validateOnParse=false;
var Url=xmlurl+"XmlData/province.XML";
XMLDoc_city.load(Url);
if(XmlDoc_city.readyState == 4 && XmlDoc_city.XML != "")
{
var city;
var citIEs = XMLDoc_city.documentElement.selectNodes("province");
for (var i=0; i < citIEs.length; i++)
{
city = citIEs[i];
if (city.getAttribute("provinceid") == pid)
{
document.write(city.getAttribute("provincename"));
break;
}
}
}
}
//根據地區districtid,獲取返回地區名 ---------------XMLurl 指的是整個站點的路征
function GetProviceName(districtid,XMLurl)
{
var XmlDoc_city = new ActiveXObject("MSXML2.DOMDocument.3.0");
XMLDoc_city.async=false;
XMLDoc_city.validateOnParse=false;
var Url=xmlurl+"XmlData/district.XML";
XMLDoc_city.load(Url);
if(XmlDoc_city.readyState == 4 && XmlDoc_city.XML != "")
{
var city;
var citIEs = XMLDoc_city.documentElement.selectNodes("district");
for (var i=0; i < citIEs.length; i++)
{
city = citIEs[i];
if (city.getAttribute("districtid") == pid)
{
document.write(city.getAttribute("districtname"));
break;
}
}
}
}
//根據省份id,城市id,地區(縣)id,獲取返回省城市名地區名 ---------------XMLurl 指的是整個站點的路征
function GetName(pid,cityid,districtid ,XMLurl)
{
var cityname="";
var provicename="";
var districtname="";
//
var XmlDoc_p = new ActiveXObject("MSXML2.DOMDocument.3.0");
XMLDoc_p.async=false;
XMLDoc_p.validateOnParse=false;
var UrlProvice=xmlurl+"XmlData/province.XML";
XMLDoc_p.load(UrlProvice);
//
var XmlDoc_city = new ActiveXObject("MSXML2.DOMDocument.3.0");
XMLDoc_city.async=false;
XMLDoc_city.validateOnParse=false;
var Urlcity=xmlurl+"XmlData/city.XML";
XMLDoc_city.load(Urlcity);
//
var XmlDoc_district = new ActiveXObject("MSXML2.DOMDocument.3.0");
XMLDoc_district.async=false;
XMLDoc_district.validateOnParse=false;
var Urldis=xmlurl+"XmlData/district.XML";
XMLDoc_district.load(Urldis);
//
if(XmlDoc_p.readyState == 4 && XmlDoc_p.XML != "")
{
var provice;
var provices = XMLDoc_city.documentElement.selectNodes("province");
for (var i=0; i < provices.length; i++)
{
provice = provices[i];
if (provice.getAttribute("provinceid") == pid)
{
provicename=provice.getAttribute("provincename");
break;
}
}
}
if(XmlDoc_city.readyState == 4 && XmlDoc_city.XML != "")
{
var city;
var citys = XMLDoc_city.documentElement.selectNodes("city");
for (var i=0; i < citys.length; i++)
{
city = citys[i];
if (city.getAttribute("cityid") == cityid)
{
cityname=city.getAttribute("cityname");
break;
}
}
}
if(XmlDoc_district.readyState == 4 && XmlDoc_district.XML != "")
{
var district;
var districts = XMLDoc_district.documentElement.selectNodes("district");
for (var i=0; i < districts.length; i++)
{
district = districts[i];
if (district.getAttribute("districtid") == districtid)
{
districtname=district.getAttribute("districtname");
break;
}
}
}
var name=provicename+" "+cityname+" "+ districtname;
document.write(name);
}
http://www.cnblogs.com/owendu/archive/2006/10/16/530580.Html