XML Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreXML Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old November 12th, 2003, 12:43 PM
madeinbritain madeinbritain is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 5 madeinbritain User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #2  
Old November 13th, 2003, 07:21 AM
madeinbritain madeinbritain is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 5 madeinbritain User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
bump

Reply With Quote
  #3  
Old November 13th, 2003, 12:49 PM
jharnois's Avatar
jharnois jharnois is offline
mod_dev_shed
Dev Shed God 19th Plane (14000 - 14499 posts)
 
Join Date: Sep 2002
Location: Atlanta, GA
Posts: 14,299 jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 Days 1 h 54 m 17 sec
Reputation Power: 760
Search in your characterData function against the $data variable. Probably simplist method, depending on how exact you'd like this, would be to use strstr() or stristr() to determine if the $data contained a particular word or words:
PHP Code:
case 'TITLE':
  if((bool)
stristr($data,$_POST['search']))
    {
    echo 
"<i>$data</i>";
    }
  break;
  } 
However, I'm not sure how this would work in the grand scheme of things b/c it's possible that the price of the book's title would still be displayed, even if the book wasn't.

FYI, you can shorten your switch() statements:
PHP Code:
switch($name)
  {
  case 
"BOOK":
    echo 
"<tr>";
    break;

  case 
"TITLE":
  case 
"AUTHOR":
  case 
"PRICE":
  case 
"RATING":
    echo 
"<td>";
    break;
  } 
__________________
# Jeremy

Explain your problem instead of asking how to do what you decided was the solution.

Reply With Quote
  #4  
Old November 13th, 2003, 02:11 PM
madeinbritain madeinbritain is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 5 madeinbritain User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
should I be using php or perl ? to do this and maybe parsing the above xml into a mysql database ?

I really am lost to what I should be looking to do

Any help would be great Jeremy

Last edited by madeinbritain : January 10th, 2004 at 07:02 AM.

Reply With Quote
  #5  
Old November 13th, 2003, 02:34 PM
jharnois's Avatar
jharnois jharnois is offline
mod_dev_shed
Dev Shed God 19th Plane (14000 - 14499 posts)
 
Join Date: Sep 2002
Location: Atlanta, GA
Posts: 14,299 jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 Days 1 h 54 m 17 sec
Reputation Power: 760
The Perl scripts looks like it needs configured. And unless you have some Perl experience, that may be rather difficult. Not so difficult as frustrating if you make a mistake.

It is going to be difficult for anybody to help you here b/c of the number of different factors and languages involved, particuarly how they all co-exist to provide the desired results. That is, unless somebody has experience with this exact thing, which I do not.

I recommend looking at PEAR (http://pear.php.net) for XML related PHP classes that may help you with this. They are normally well documented and easy to use.

Do you have an experience with XSL?

Reply With Quote
  #6  
Old November 13th, 2003, 03:32 PM
madeinbritain madeinbritain is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 5 madeinbritain User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Not much Jeremy, so it's pretty hard going really. I think I will stick with the php rather than perl.

Reply With Quote
  #7  
Old November 13th, 2003, 03:43 PM
jharnois's Avatar
jharnois jharnois is offline
mod_dev_shed
Dev Shed God 19th Plane (14000 - 14499 posts)
 
Join Date: Sep 2002
Location: Atlanta, GA
Posts: 14,299 jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 Days 1 h 54 m 17 sec
Reputation Power: 760
Try http://www.w3schools.com . They have a good XML and XSL tutorials. If all you need to do is get this displayed in a browser, XSL is the way to go IMO. If you're looking to get it into a database, you should be able to accomplish that with the PHP XML Functions similar to what you showed in your first post.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreXML Programming > Parsing XML with Php, help needed please


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway