处理XML格式参数的请求

novel.xml文件

<?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>

 

 
novel.html

<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;

  }

 }

  //MozillaSafari等非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>

 

 

 novel.php

<?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页面中。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值