xml访问元素和属性

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jQuery.get()</title>
<style type="text/css">
ul{border:1px solid black;list-style:none;
margin:0pt;padding:0pt;float:left;
font-family:Verdana,Arial,Helvetica,sans-serif;
font-size:12px;width:300px;}
li{padding:10px 5px;border-bottom:1px solid black;}
</style>
</head>
<body>

<p>
<select id="bookList">
<option value="">select a book</option>
<?php
$objxml=simplexml_load_file('../common.xml');
foreach($objxml->book as $book){
echo '<option value="'.$book['index'].'">'.$book->name.'</option>';
}
?>
</select>
<input  type="button" id="year"  value="Get year of publication" />
<input type="button" id="stories"  value="get story list" />
</p>
<p id="result"></p>
<script type="text/javascript" src="../jquery.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
     $('input:button').click(function()
               {
                    if($('#bookList').val()!='')
                         {
                              $.get(
                                   'process.php',
                                   {id:$('#bookList').val(),action:$(this).attr('id')},
                                   function(data)
                                   {
                                        $('#result').html(data);
                                   }
                                   );
    
                         };
               });    
});

</script>
</body>
</html>
 
 
process.php
 
 
<?php
$bookId=$_GET['id'];
$action=$_GET['action'];
$strResponse;
$objXML=simplexml_load_file('../common.xml');
foreach ($objXML->book as $book){
     if($book['index']==$bookId){
if($action=='year'){
          $strResponse='This book was published in year:'.$book->name['year'];
     }else if($action=='stories'){
          $stories=$book->story;
          $strResponse='<ul>';
          foreach($stories as $story){
               $strResponse.='<li>'.$story->title.'</li>';
          }
          $strResponse.='</ul>';
     }else{
          $strResponse='Nothing to do';
     }
     break;
         
     }
     }
     echo $strResponse;
posted @ 2014-04-04 17:25  wint  Views(146)  Comments(0)    收藏  举报