js ajax经典实例之解析xml
test.php文件代码如下:
<code> <title>js ajax经典实例之解析xml</title> <script type="text/javascript"> //使用工厂的方法来创建xmlHttpRequest对象 function createXmlHttpRequest(){ var xmlHttpRequest=null; if(window.ActiveXObject){ //以下代码是IE中创建xmlHttpRequest对象的方法 xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP"); }else{ //以下代码是其他浏览器中创建xmlHttpRequest对象的方法 xmlHttpRequest = new XMLHttpRequest(); } return xmlHttpRequest; } function getData(){ //如果xmlHttpRequest对象创建成功,就开始请求服务器 var xmlHttpRequest=createXmlHttpRequest(); if(xmlHttpRequest){ //打开请求,第一个参数表示请求的类型(get或post),第二个参数表示请求的服务器地址,第三个参数表示是否异步(异步指的可以同时执行,同步则表示执行完步骤一,然后才能执行步骤二) xmlHttpRequest.open("get","xml.php",true); //设置xmlHttpRequest对象的onreadystatechange事件,onreadystatechange存储函数(或函数名),每当xmlHttpRequest对象的readyState 属性改变时,就会调用该函数。readyState存有 xmlHttpRequest的状态。从 0 到 4 发生变化,0表示请求未初始化,1表示服务器连接已建立,2表示请求已接收 3表示请求处理中 4表示请求已完成,且响应已就绪, xmlHttpRequest.onreadystatechange=function(){ if (xmlHttpRequest.readyState==4 && xmlHttpRequest.status==200){//当请求完成且状态为ok的时候,下面会解析ajax返回的xml. var xml=xmlHttpRequest.responseXML;//接受服务器返回的xml对象,并存储在xml变量中 //下面解析xml; var persons=xml.getElementsByTagName("person"); var str=""; for(var i=0;i<persons.length;i++){ var fc= persons[i]; var name=fc.getElementsByTagName("name")[0].innerHTML; var age=fc.getElementsByTagName("age")[0].innerHTML; var weight=fc.getElementsByTagName("weight")[0].innerHTML; var height=fc.getElementsByTagName("height")[0].innerHTML; str+="name:"+name+" age:"+age+" weight:"+weight+" height:"+height+"<br/>" } document.getElementById("data").innerHTML=str; } } xmlHttpRequest.send(); } } </script> <input type="button" value="获取数据" onclick="getData();"> <div id="data"></div> </code>
xml.php文件代码如下:
<code><?php header("Content-type:text/xml"); $arr=array( array("name"=>"张三","age"=>"16","weight"=>"50","height"=>"160"), array("name"=>"李四","age"=>"17","weight"=>"51","height"=>"165"), array("name"=>"王二","age"=>"18","weight"=>"55","height"=>"170"), array("name"=>"javior","age"=>"20","weight"=>"68","height"=>"179") ); $xml="<?xml version='1.0' encoding='gb2312'?>"; $xml.="<persons>"; foreach($arr as $k=>$v){ $xml.="<person>"; $xml.="<name>".$v["name"]."</name>"; $xml.="<age>".$v["age"]."</age>"; $xml.="<weight>".$v["weight"]."</weight>"; $xml.="<height>".$v["height"]."</height>"; $xml.="</person>"; } $xml.="</persons>"; echo $xml; ?> </code>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Detailed explanation of Oracle error 3114: How to solve it quickly, specific code examples are needed. During the development and management of Oracle database, we often encounter various errors, among which error 3114 is a relatively common problem. Error 3114 usually indicates a problem with the database connection, which may be caused by network failure, database service stop, or incorrect connection string settings. This article will explain in detail the cause of error 3114 and how to quickly solve this problem, and attach the specific code

Build an autocomplete suggestion engine using PHP and Ajax: Server-side script: handles Ajax requests and returns suggestions (autocomplete.php). Client script: Send Ajax request and display suggestions (autocomplete.js). Practical case: Include script in HTML page and specify search-input element identifier.

Wormhole is a leader in blockchain interoperability, focused on creating resilient, future-proof decentralized systems that prioritize ownership, control, and permissionless innovation. The foundation of this vision is a commitment to technical expertise, ethical principles, and community alignment to redefine the interoperability landscape with simplicity, clarity, and a broad suite of multi-chain solutions. With the rise of zero-knowledge proofs, scaling solutions, and feature-rich token standards, blockchains are becoming more powerful and interoperability is becoming increasingly important. In this innovative application environment, novel governance systems and practical capabilities bring unprecedented opportunities to assets across the network. Protocol builders are now grappling with how to operate in this emerging multi-chain

[Analysis of the meaning and usage of midpoint in PHP] In PHP, midpoint (.) is a commonly used operator used to connect two strings or properties or methods of objects. In this article, we’ll take a deep dive into the meaning and usage of midpoints in PHP, illustrating them with concrete code examples. 1. Connect string midpoint operator. The most common usage in PHP is to connect two strings. By placing . between two strings, you can splice them together to form a new string. $string1=&qu

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:

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.

Analysis of new features of Win11: How to skip logging in to a Microsoft account. With the release of Windows 11, many users have found that it brings more convenience and new features. However, some users may not like having their system tied to a Microsoft account and wish to skip this step. This article will introduce some methods to help users skip logging in to a Microsoft account in Windows 11 and achieve a more private and autonomous experience. First, let’s understand why some users are reluctant to log in to their Microsoft account. On the one hand, some users worry that they

Due to space limitations, the following is a brief article: Apache2 is a commonly used web server software, and PHP is a widely used server-side scripting language. In the process of building a website, sometimes you encounter the problem that Apache2 cannot correctly parse the PHP file, causing the PHP code to fail to execute. This problem is usually caused by Apache2 not configuring the PHP module correctly, or the PHP module being incompatible with the version of Apache2. There are generally two ways to solve this problem, one is
