xml feed

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gonthechase
Forum Newbie
Posts: 3
Joined: Thu Sep 13, 2007 7:17 am

xml feed

Post by gonthechase »

i m extracting data from an xml file.i can get all contents between two tags but one information i want is inside a tag.and also the tag is self closing tag.
so first of all how to read such a tag which does not have a closing tag.
and secondly how to read data inside a tag.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Can you post your parsing code?
gonthechase
Forum Newbie
Posts: 3
Joined: Thu Sep 13, 2007 7:17 am

here's the code

Post by gonthechase »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Code: Select all

$file="http://feeds.feedburner.com/HotelsNews?format=xml";

$rss_channel = array();
$currently_writing = "";
$main = "";
$author_counter = 0;
$link_counter = 0;

function startElement($parser, $name, $attrs) {
   	global $rss_channel, $currently_writing, $main;
   	switch($name) {
   		case "RSS":
   		case "RDF:RDF":
   		case "LINK":
   			$main = "LINK";
   			break;   		 		
   		case "ENTRY":
   			$main = "ENTRY";
   			break;
				
   		default:
   			$currently_writing = $name;
   			break;
   	}
}

function endElement($parser, $name) {
   	global $rss_channel, $currently_writing, $author_counter,$link_counter;
   	if ($name == "ENTRY") {
   		$author_counter++;		
   	}	
	if ($name == "LINK") {
   		$link_counter++;		
   	}
}

function characterData($parser, $data) {
	global $rss_channel, $currently_writing, $main, $author_counter,$link_counter;
	if ($currently_writing != "") {
		switch($main) {
			case "LINK":				
			if (isset($rss_channel[$main][$link_counter][$currently_writing])) {
				print ("rss_channel[$main][$link_counter][$currently_writing] = $data<br>");
					$rss_channel[$main][$link_counter][$currently_writing] .= $data;
				} else {			
					print ("rss_channel[$main][$link_counter][$currently_writing] = $data<br>");
					$rss_channel[$main][$link_counter][$currently_writing] = $data;
				}
				break;			
			case "ENTRY":				
				if (isset($rss_channel[$main][$author_counter][$currently_writing])) {					
				} else {
					print ("rss_channel[$main][$author_counter][$currently_writing] = $data<br>");
					$rss_channel[$main][$author_counter][$currently_writing] = $data;
				}
				break;
		}
	}
}

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = @fopen($file, "r"))) {
	die("could not open XML input");
	
}

while ($data = fread($fp, 4096)) {
	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)));
	}
}
xml_parser_free($xml_parser);

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Post Reply