
April 22nd, 2008, 12:04 PM
|
|
Contributing User
|
|
Join Date: Mar 2007
Posts: 30
Time spent in forums: 12 h 57 m 43 sec
Reputation Power: 2
|
|
|
PHP XML Parsing problem
Hello, I'm doing some XML parsing with PHP's inbuilt XML parsing stuff, and I've run into a problem. I'm using a PHP class (xmlParser) to parse the feed, assigning values to variables within a subclass (blogEntry) of xmlParser. My feed looks like this (though much longer):
Code:
<entry>
<id>tag:blogger.com,1999:blog-4654928604139256637.post-2296813726404542278</id>
<published>2008-04-20T00:57:00.000-07:00</published>
<updated>2008-04-19T16:18:52.261-07:00</updated>
<category scheme='http://www.blogger.com/atom/ns#' term='churches'/>
<category scheme='http://www.blogger.com/atom/ns#' term='chilham'/>
<category scheme='http://www.blogger.com/atom/ns#' term='village'/>
<title type='text'>St Mary's Church in Chilham, Canterbury</title>
<content type='html'><![CDATA[<a href="http://bp1.blogger.com/_pGqCsz7Q_xI/SAp5PK-V8_I/AAAAAAAAApg/suVNk7LCDpQ/s1600-h/stmary%27s+chillam.jpg"
><img id="BLOGGER_PHOTO_ID_5191094822048756722" style="DISPLAY: block; MARGIN: 0px auto 10px; CURSOR: hand;
TEXT-ALIGN: center" alt="" src="http://bp1.blogger.com/_pGqCsz7Q_xI/SAp5PK-V8_I/AAAAAAAAApg/suVNk7LCDpQ/s320/stmary%27s+chillam.jpg"
border="0" /></a> <div>This is St Mary's Church has been here for a long time. It was mentioned
in the Doomsday book (1085) and building records go back to 1280. It is only a few yards from the house
I showed you yesterday.</div><div>If you look closely you can see a clock in the tower.
According to Tom Reed, in the Guide to Chilham booklet, the clock and the set of 8 church bells date
back from the 1720s.</div><div></div>]]></content>
<link rel='alternate' type='text/html'
href='http://testblog.blogspot.com/2008/04/st-marys-church-in-chilham-canterbury.html'
title='St Mary's Church in Chilham, Canterbury'/>
<link rel='replies' type='text/html'
href='http://www.blogger.com/comment.g?blogID=4654928604139256637&postID=2296813726404542278' title='8 Comments'/>
<link rel='replies' type='application/atom+xml'
href='http://testblog.blogspot.com/feeds/2296813726404542278/comments/default'
title='Post Comments'/>
<link rel='self' type='application/atom+xml'
href='http://testblog.blogspot.com/feeds/posts/default/2296813726404542278'/>
<link rel='edit' type='application/atom+xml'
href='http://www.blogger.com/feeds/4654928604139256637/posts/default/2296813726404542278'/>
<author>
<name>Bob</name>
</author>
</entry>
<entry>
<id>tag:blogger.com,1999:blog-4654928604139256637.post-6347572942790529689</id>
<published>2008-04-19T01:28:00.000-07:00</published>
<updated>2008-04-19T09:34:26.037-07:00</updated>...
The <entry> tags just basically repeat after this.
Here's my xmlParser class/functions:
Code:
class xmlParser
{
var $parser;
var $entries = array();
var $currentEntryIndex = 0;
var $currentElementAttributes;
var $currentElementName;
function xmlParser()
{
$this->parser = xml_parser_create();
xml_set_object($this->parser, $this);
xml_set_element_handler($this->parser, 'startTag', 'endTag');
xml_set_character_data_handler($this->parser, 'tagData');
}
function parse($data)
{
xml_parse($this->parser, $data);
//var_dump($this->entries);
print("Error code: " . xml_error_string(xml_get_error_code($this->parser)));
return serialize($this->entries);
}
function startTag($parser, $name, $attributes)
{
$this->currentElementName = strtolower($name);
if ($this->currentElementName == 'entry')
{
$this->entries[$this->currentEntryIndex] = new blogEntry();
}
else if ($this->currentElementName == 'category')
{
$this->entries[$this->currentEntryIndex]->categories[] = $attributes['TERM'];
}
else if ($this->currentElementName == 'link')
{
$this->currentElementName = $attributes['REL'] . 'Link';
$this->entries[$this->currentEntryIndex]->{$this->currentElementName} = $attributes['HREF'];
}
else
{
$this->currentElementAttributes = $attributes;
}
}
function tagData($parser, $data)
{
if ($this->currentElementName != 'author' && $this->currentElementName != 'link' && $this->currentElementName != 'category' && $this->currentElementName != 'entry')
{
$this->entries[$this->currentEntryIndex]->{$this->currentElementName} = $data;
}
}
function endTag($parser, $name)
{
if ($name == 'ENTRY')
{
$this->currentEntryIndex++;
}
}
function closeParser()
{
xml_parser_free($this->parser);
}
The issue that I'm having is that the xmlParser class only parses one set of <entry></entry> tags; I've tried printing the index for each iteration but it seems to just stop when it hits the next <entry> tag. Anyone know why it could be doing this?
|