Home Backend Development XML/RSS Tutorial Get city, province name from xml

Get city, province name from xml

Feb 28, 2017 pm 04:34 PM

I’ve been fine recently. I wrote a method that often requires the name of a city or province in a project, so I changed it to a class. It is convenient to call later

//********************************************************************************
//*                                                                              *
//    功能描述:从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(&#39;您的浏览器不支持xml文件读取,于是本页面禁止您的操作,推荐使用IE5.0以上可以解决此问题!&#39;);
    // }
}
//根据省份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);
  
}
Copy after login

The above is the content of getting the city and province name from xml. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
Can I open an XML file using PowerPoint? Can I open an XML file using PowerPoint? Feb 19, 2024 pm 09:06 PM

Can XML files be opened with PPT? XML, Extensible Markup Language (Extensible Markup Language), is a universal markup language that is widely used in data exchange and data storage. Compared with HTML, XML is more flexible and can define its own tags and data structures, making the storage and exchange of data more convenient and unified. PPT, or PowerPoint, is a software developed by Microsoft for creating presentations. It provides a comprehensive way of

Convert XML data to CSV format in Python Convert XML data to CSV format in Python Aug 11, 2023 pm 07:41 PM

Convert XML data in Python to CSV format XML (ExtensibleMarkupLanguage) is an extensible markup language commonly used for data storage and transmission. CSV (CommaSeparatedValues) is a comma-delimited text file format commonly used for data import and export. When processing data, sometimes it is necessary to convert XML data to CSV format for easy analysis and processing. Python is a powerful

Handling errors and exceptions in XML using Python Handling errors and exceptions in XML using Python Aug 08, 2023 pm 12:25 PM

Handling Errors and Exceptions in XML Using Python XML is a commonly used data format used to store and represent structured data. When we use Python to process XML, sometimes we may encounter some errors and exceptions. In this article, I will introduce how to use Python to handle errors and exceptions in XML, and provide some sample code for reference. Use try-except statement to catch XML parsing errors When we use Python to parse XML, sometimes we may encounter some

Collect all treasure chests in Mingchao Jinzhou City Collect all treasure chests in Mingchao Jinzhou City Mar 16, 2024 pm 02:28 PM

There are many treasure chests in Mingchao Jinzhou City. Many players want to know where the treasure chests are. The editor has marked the locations of all the treasure chests. Players can just follow the marked numbers to find them one by one. Let’s take a look at this Mingchao article for details. The latest collection of all treasure chests in Jinzhou City. Mingchao Strategy Collection Mingchao Jinzhou City Treasure Box Collection Location 1: Jinzhou City (1-25) Twenty-five treasure boxes, five voice boxes Location 2: Jinzhou City (26-41) Fifteen treasure boxes, three A voice box, two flying hunters

How to handle XML and JSON data formats in C# development How to handle XML and JSON data formats in C# development Oct 09, 2023 pm 06:15 PM

How to handle XML and JSON data formats in C# development requires specific code examples. In modern software development, XML and JSON are two widely used data formats. XML (Extensible Markup Language) is a markup language used to store and transmit data, while JSON (JavaScript Object Notation) is a lightweight data exchange format. In C# development, we often need to process and operate XML and JSON data. This article will focus on how to use C# to process these two data formats, and attach

Python parsing special characters and escape sequences in XML Python parsing special characters and escape sequences in XML Aug 08, 2023 pm 12:46 PM

Python parses special characters and escape sequences in XML XML (eXtensibleMarkupLanguage) is a commonly used data exchange format used to transfer and store data between different systems. When processing XML files, you often encounter situations that contain special characters and escape sequences, which may cause parsing errors or misinterpretation of the data. Therefore, when parsing XML files using Python, we need to understand how to handle these special characters and escape sequences. 1. Special characters and

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

How to use PHP functions to process XML data? How to use PHP functions to process XML data? May 05, 2024 am 09:15 AM

Use PHPXML functions to process XML data: Parse XML data: simplexml_load_file() and simplexml_load_string() load XML files or strings. Access XML data: Use the properties and methods of the SimpleXML object to obtain element names, attribute values, and subelements. Modify XML data: add new elements and attributes using the addChild() and addAttribute() methods. Serialized XML data: The asXML() method converts a SimpleXML object into an XML string. Practical example: parse product feed XML, extract product information, transform and store it into a database.

See all articles