Parsing RDF files help needed?
| From: | Rus Foster | Date: | Thu, 26 Sep 2002 15:13:28 +0000 |
| Subject: | Parsing RDF files help needed? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-117737@lists.php.net to get a copy of this message | ||
Hi All,
I've been playing with parsing RDF/XML files using PHP (code snippet
below). However I'm having trouble trying to work out how to actually
format the results in an order. My source is in the form
<item>
<title>Politicizing Science</title>
<link>http://slashdot.org/article.pl?sid=02/09/17/0349219</link>
</item>
Now I could easily cope with it if it was link, title as I would just
print <a href=$link>$title</a>. However I just can't work out a way of
doing this, this way. My attempt so far is below (mostly debugging code)
function startElement($parser, $name, $attrs) {
global $depth,$type,$title,$url;
for ($i = 0; $i < $depth[$parser]; $i++) {
print " ";
}
$type = $name;
$depth[$parser]++;
}
function stopElement($parser, $name) {
global $depth,$type,$title,$url;
$depth[$parser]--;
print "depth parse = $depth[$parser] and $title !! $url <br> ";
}
function characterData($parser, $data) {
global $type,$title,$url;
if ($data)
{
switch ($type) {
case "ITEM":
case 'LINK':
$url=$data;
case "TITLE":
$title=$data;
}
}
}
$file="slashdot.rdf";
$fp= fopen($file,"r");
$parser=xml_parser_create();
xml_set_element_handler($parser,"startElement","stopElement");
xml_set_character_data_handler($parser,"characterData");
while ($data = fread($fp,4096))
xml_parse($parser, $data, feof($fp))
or die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($parser)),xml_get_current_line_number($parser)));
fclose($fp);
As you can see I'm trying to use global variables but I'm just really
unhappy with the quality of the code so any help would be appreciated.
Rgds
Rus foster
--
http://www.fsck.me.uk - Rant wibble wave
http://shells.fsck.me.uk - Hosting and stuff