<?xml version="1.0" encoding="utf-8"?>
<novels>
<novel>
<name>五星饭店</name>
<author>海岩</author>
<price>28.00 </price>
<introduce>
这是一本关于一群年轻人奋斗的的过程,在追求属于自己的真实人生
</introduce>
<publish>现代出版社</publish>
<publishDate>2006-08-01 </publishDate>
</novel>
</novels>
<html>
<head>
<meta xmlns-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest()
{
//在IE下创建XMLHttpRequest对象
try
{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(oc)
{
xmlHttp = null;
}
}
//在Mozilla和Safari等非IE浏览器下创建XMLHTTPRequest对象
if(!xmlHttp && typeof XMLHttpRequest != "undefined")
{
xmlHttp = new XMLHttpRequest();
}
return xmlHttp;
}
//发送请求
function showBook()
{
createXMLHttpRequest();
var url="novel.php";
xmlHttp.onreadystatechange=StateChange;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
//处理请求
function StateChange()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
document.getElementById("message").innerHTML= xmlHttp.responseText;
}
}
}
</script>
</head>
<body>
<input type="button" name="btn" id="button1" value="单击按钮显示详细信息" οnclick="showBook()">
<div id="message"></div>
</body>
</html>
<?php
header('Content-type:text/xml;charset=utf-8');
$doc = new DOMDocument();
$doc->load('novel.xml' );
$novels = $doc->getElementsByTagName( "novel" );
foreach( $novels as $book )
{
$names = $book->getElementsByTagName("name");
$name = $names->item(0)->nodeValue;
$authors = $book->getElementsByTagName("author");
$author = $authors->item(0)->nodeValue;
$prices = $book->getElementsByTagName("price");
$price = $prices->item(0)->nodeValue;
$introduces = $book->getElementsByTagName("introduce");
$introduce = $introduces->item(0)->nodeValue;
$publishs = $book->getElementsByTagName("publish");
$publish = $publishs->item(0)->nodeValue;
$publishDates = $book->getElementsByTagName("publishDate");
$publishDate = $publishDates->item(0)->nodeValue;
echo ("书名:".$name."<br/>");
echo ("作者:".$author."<br/>");
echo ("价格:".$price."<br/>");
echo ("简介:".$introduce."<br/>");
echo ("出版社:".$publish."<br/>");
echo ("出版日期:".$publishDate."<br/>");
echo ("<hr/>");
}
?>
使用Ajax+PHP处理XML文件,在PHP页面中遍历了XML文件中的所有信息,然后将得到的信息输入的HTML页面中。