search
  • Sign In
  • Sign Up
Password reset successful

Follow the proiects vou are interested in andi aet the latestnews about them taster

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('您的浏览器不支持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);
  
}

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 (m.sbmmt.com)!


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

Undress AI Tool

Undress AI Tool

Undress images for free

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

ArtGPT

ArtGPT

AI image generator for creative art from text prompts.

Stock Market GPT

Stock Market GPT

AI powered investment research for smarter decisions

Popular tool

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)

JSON vs. XML: Why RSS Chose XML JSON vs. XML: Why RSS Chose XML May 05, 2025 am 12:01 AM

RSS chose XML instead of JSON because: 1) XML's structure and verification capabilities are better than JSON, which is suitable for the needs of RSS complex data structures; 2) XML was supported extensively at that time; 3) Early versions of RSS were based on XML and have become a standard.

RSS in XML: Decoding Tags, Attributes, and Structure RSS in XML: Decoding Tags, Attributes, and Structure Apr 24, 2025 am 12:09 AM

RSS is an XML-based format used to publish and subscribe to content. The XML structure of an RSS file includes a root element, an element, and multiple elements, each representing a content entry. Read and parse RSS files through XML parser, and users can subscribe and get the latest content.

Understanding RSS Documents: A Comprehensive Guide Understanding RSS Documents: A Comprehensive Guide May 09, 2025 am 12:15 AM

RSS documents are a simple subscription mechanism to publish content updates through XML files. 1. The RSS document structure consists of and elements and contains multiple elements. 2. Use RSS readers to subscribe to the channel and extract information by parsing XML. 3. Advanced usage includes filtering and sorting using the feedparser library. 4. Common errors include XML parsing and encoding issues. XML format and encoding need to be verified during debugging. 5. Performance optimization suggestions include cache RSS documents and asynchronous parsing.

Building XML Applications with C  : Practical Examples Building XML Applications with C : Practical Examples May 03, 2025 am 12:16 AM

You can use the TinyXML, Pugixml, or libxml2 libraries to process XML data in C. 1) Parse XML files: Use DOM or SAX methods, DOM is suitable for small files, and SAX is suitable for large files. 2) Generate XML file: convert the data structure into XML format and write to the file. Through these steps, XML data can be effectively managed and manipulated.

RSS, XML and the Modern Web: A Content Syndication Deep Dive RSS, XML and the Modern Web: A Content Syndication Deep Dive May 08, 2025 am 12:14 AM

RSS and XML are still important in the modern web. 1.RSS is used to publish and distribute content, and users can subscribe and get updates through the RSS reader. 2. XML is a markup language and supports data storage and exchange, and RSS files are based on XML.

XML in C  : Handling Complex Data Structures XML in C : Handling Complex Data Structures May 02, 2025 am 12:04 AM

Working with XML data structures in C can use the TinyXML or pugixml library. 1) Use the pugixml library to parse and generate XML files. 2) Handle complex nested XML elements, such as book information. 3) Optimize XML processing code, and it is recommended to use efficient libraries and streaming parsing. Through these steps, XML data can be processed efficiently.

Beyond Basics: Advanced RSS Features Enabled by XML Beyond Basics: Advanced RSS Features Enabled by XML May 07, 2025 am 12:12 AM

RSS enables multimedia content embedding, conditional subscription, and performance and security optimization. 1) Embed multimedia content such as audio and video through tags. 2) Use XML namespace to implement conditional subscriptions, allowing subscribers to filter content based on specific conditions. 3) Optimize the performance and security of RSSFeed through CDATA section and XMLSchema to ensure stability and compliance with standards.

Understanding RSS: An XML Perspective Understanding RSS: An XML Perspective Apr 25, 2025 am 12:14 AM

RSS is an XML-based format used to publish frequently updated content. 1. RSSfeed organizes information through XML structure, including title, link, description, etc. 2. Creating RSSfeed requires writing in XML structure, adding metadata such as language and release date. 3. Advanced usage can include multimedia files and classified information. 4. Use XML verification tools during debugging to ensure that the required elements exist and are encoded correctly. 5. Optimizing RSSfeed can be achieved by paging, caching and keeping the structure simple. By understanding and applying this knowledge, content can be effectively managed and distributed.

Related articles