
November 12th, 2003, 12:43 PM
|
|
Junior Member
|
|
Join Date: Nov 2003
Posts: 5
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Parsing XML with Php, help needed please
I have created a test xml file and have created a php file to parse this xml. So great all is working fine so far. But I want to do more than just parse and display the xml data. What I want to do is not only parse the xml file but also to search the xml for certain keywords such as 'book title' and display this on a html page.
The code for my xml file and php parser is below. Could anyone please help me beable to search and fetch data from the xml file as well.
xml file
Quote:
<?xml version="1.0"?>
<library>
<book>
<title>Hannibal</title>
<author>Thomas Harris</author>
<genre>Suspense</genre>
<pages>564</pages>
<price>8.99</price>
<rating>4</rating>
</book>
<book>
<title>Run</title>
<author>Douglas E. Winter</author>
<genre>Thriller</genre>
<pages>390</pages>
<price>7.49</price>
<rating>5</rating>
</book>
<book>
<title>The Lord Of The Rings</title>
<author>J. R. R. Tolkien</author>
<genre>Fantasy</genre>
<pages>3489</pages>
<price>10.99</price>
<rating>5</rating>
</book>
</library> |
and my php file
Quote:
<html>
<head>
<title>The Library</title>
<style type="text/css">
TD {font-family: Arial; font-size: smaller}
H2 {font-family: Arial}
</style>
</head>
<body bgcolor="white">
<h2>The Library</h2>
<table border="1" cellspacing="1" cellpadding="5">
<tr>
<td align=center>Title</td>
<td align=center>Author</td>
<td align=center>Price</td>
<td align=center>User Rating</td>
</tr>
<?
// data file
$file = "xml/library.xml";
// use this to keep track of which tag the parser is currently processing
$currentTag = "";
function startElement($parser, $name, $attrs) {
global $currentTag;
$currentTag = $name;
// output opening HTML tags
switch ($name) {
case "BOOK":
echo "<tr>";
break;
case "TITLE":
echo "<td>";
break;
case "AUTHOR":
echo "<td>";
break;
case "PRICE":
echo "<td>";
break;
case "RATING":
echo "<td>";
break;
default:
break;
}
}
function endElement($parser, $name) {
global $currentTag;
// output closing HTML tags
switch ($name) {
case "BOOK":
echo "</tr>";
break;
case "TITLE":
echo "</td>";
break;
case "AUTHOR":
echo "</td>";
break;
case "PRICE":
echo "</td>";
break;
case "RATING":
echo "</td>";
break;
default:
break;
}
// clear current tag variable
$currentTag = "";
}
// process data between tags
function characterData($parser, $data) {
global $currentTag;
// text ratings
$ratings = array("Words fail me!", "Terrible", "Bad", "Indifferent",
"Good", "Excellent");
// format the data
switch ($currentTag) {
case "TITLE":
// italics for title
echo "<i>$data</i>";
break;
case "AUTHOR":
echo $data;
break;
case "PRICE":
// add currency symbol for price
echo "$" . $data;
break;
case "RATING":
// get text rating
echo $ratings[$data];
break;
default:
break;
}
}
// initialize parser
$xml_parser = xml_parser_create();
// set callback functions
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
// open XML file
if (!($fp = fopen($file, "r")))
{
die("Cannot locate XML data file: $file");
}
// read and parse data
while ($data = fread($fp, 4096))
{
// error handler
if (!xml_parse($xml_parser, $data, feof($fp)))
{
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
// clean up
xml_parser_free($xml_parser);
?>
</table>
</body>
</html> |
Last edited by madeinbritain : January 10th, 2004 at 06:56 AM.
|