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:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old April 22nd, 2008, 12:04 PM
smesser smesser is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Posts: 30 smesser User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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&apos;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?

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreXML Programming > PHP XML Parsing problem


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

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

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





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