Php 简明教程
PHP - AJAX XML Parser
使用 PHP 和 AJAX,我们可以解析本地目录和服务器上的 XML 文档。以下示例演示了如何用网络浏览器解析 XML。
Using PHP with AJAX, we can parse an XML document from local directory as well as on a server. The following example demonstrates how to parse XML with web browser.
客户端脚本呈现了一个 HTML 表单,并定义了一个 JavaScript 函数,用于通过 XMLHttpRequest 对象向服务器发送 HTTP 请求。
The client-end script renders a HTML form and defines a JavaScript function for sending a HTTP request to the server with XMLHttpRequest object.
在服务器上,一个 PHP 脚本从所需的 XML 文档加载 DOM 对象,从 $_REQUEST 变量中获取所选课程,并将已选课程的详细信息呈现为对客户端的响应。
On the server, a PHP script loads the DOM object from the required XML document, fetches the selected course from $_REQUEST variable, and renders the details of the course chosen as the response back to the client.
Step 1
以下 XML 文档存储在 XAMPP 服务器的文档根目录中。
The following XML document is stored on the document root of the XAMPP server.
<?xml version = "1.0" encoding = "utf-8"?>
<CATALOG>
<SUBJECT>
<COURSE>Android</COURSE>
<COUNTRY>India</COUNTRY>
<COMPANY>TutorialsPoint</COMPANY>
<PRICE>$10</PRICE>
<YEAR>2015</YEAR>
</SUBJECT>
<SUBJECT>
<COURSE>Html</COURSE>
<COUNTRY>India</COUNTRY>
<COMPANY>TutorialsPoint</COMPANY>
<PRICE>$15</PRICE>
<YEAR>2015</YEAR>
</SUBJECT>
<SUBJECT>
<COURSE>Java</COURSE>
<COUNTRY>India</COUNTRY>
<COMPANY>TutorialsPoint</COMPANY>
<PRICE>$20</PRICE>
<YEAR>2015</YEAR>
</SUBJECT>
<SUBJECT>
<COURSE>Microsoft</COURSE>
<COUNTRY>India</COUNTRY>
<COMPANY>TutorialsPoint</COMPANY>
<PRICE>$25</PRICE>
<YEAR>2015</YEAR>
</SUBJECT>
</CATALOG>
Step 2
下面的 AJAX 代码有一个 HTML 表单和一个 JavaScript 函数,通过 XMLHttpRequest 对象提出 HTTP 请求。
The AJAX code below has a HTML form and a JavaScript function to raise HTTP request through XMLHttpRequest object.
<html>
<head>
<script>
function showCD(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","hello.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
Select a Course:
<select name = "cds" onchange = "showCD(this.value)">
<option value = "">Select a course:</option>
<option value = "Android">Android </option>
<option value = "Html">HTML</option>
<option value = "Java">Java</option>
<option value = "Microsoft">MS technologies</option>
</select>
</form>
<div id = "txtHint"><b>Course info will be listed here...</b></div>
</body>
</html>
Step 3
搜索 XML 文档的服务器端 PHP 脚本如下 −
The server-side PHP script to search within the XML document is as follows −
<?php
$q = $_GET["q"];
$xmlDoc = new DOMDocument();
$xmlDoc->load("test.xml");
$x = $xmlDoc->getElementsByTagName('COURSE');
for ($i = 0; $i<=$x->length-1; $i++) {
if ($x->item($i)->nodeType == 1) {
if ($x->item($i)->childNodes->item(0)->nodeValue == $q) {
$y = ($x->item($i)->parentNode);
}
}
}
$cd = ($y->childNodes);
for ($i = 0;$i<$cd->length;$i++) {
if ($cd->item($i)->nodeType == 1) {
echo("<b>" . $cd->item($i)->nodeName . ":</b> ");
echo($cd->item($i)->childNodes->item(0)->nodeValue);
echo("<br>");
}
}
?>
访问“http://localhost/example.php”来让用户选择课程。在选择后,相关详细信息从服务器中获取并显示如下 −
Visit "http://localhost/example.php" to let the user select a course. Upon selection, the relevant details are fetched from the server and displayed as below −