Jump to content


Photo

Help with XML


  • Please log in to reply
4 replies to this topic

#1 Balmung-San

Balmung-San
  • Members
  • PipPipPip
  • Advanced Member
  • 327 posts

Posted 30 July 2006 - 12:47 PM

Okay, so I've looked through the documentation and a few tutorials, and I am completely lost on how PHP parses XML. About all I'm wishing to do with it right now is taking an XML file and printing the information from one tag. Any help(or code) would be appreciated.
"Paranoia is very useful in this work. ...If your cryptographic system can survive the paranoia model, it has at least a fighting chance of surviving in the real world." - Niels Ferguson & Bruce Schneier

They say there's more then one way to skin a cat.
In programming, there's no wrong way to skin a cat, just more efficent ways of doing so.

#2 micah1701

micah1701
  • Members
  • PipPipPip
  • Advanced Member
  • 613 posts
  • LocationEllington, CT USA

Posted 30 July 2006 - 04:43 PM

I believe that xml is client side (like HTML) so it is not parsed by PHP (server side).  it can be generated by PHP, but not parsed by it.
"Confidence in the face of risk."

#3 Drumminxx

Drumminxx
  • Members
  • PipPipPip
  • Advanced Member
  • 92 posts
  • LocationUSA

Posted 30 July 2006 - 04:50 PM

I just finished building an xml parser, give me about 10 minutes and I'll post you some code to look at so you can see how it works.
:: gucci.com ::
:: blockbuster.com ::

--------------------

#4 Drumminxx

Drumminxx
  • Members
  • PipPipPip
  • Advanced Member
  • 92 posts
  • LocationUSA

Posted 30 July 2006 - 05:01 PM

<?
global $c, $tag_name, $parent_name;
global $blurbs, $titles, $bodies, $dates;
global $images, $videos;
global $labels, $types, $urls;
$c=-1;
function startTag($parser, $name, $attrs) {
   	global $c, $tag_name, $parent_name;
	global $labels, $types, $urls;
	global $images, $videos;
   	global $dates, $Ms, $Ds, $Ys;
   $tag_name = $name;
   switch($name) {
		case "ITEM":
			$c++;
	 		$blurbs[$c]='';
	 		$bodies[$c]='';
	 		$titles[$c]='';
	 		$labels[$c]='';
	 		$types[$c]='';
	 		$urls[$c]='';
	 		$videos[$c]='';
	 		$images[$c]='';
			break;
		case "DATE":
			$date = $attrs["M"]."/".$attrs["D"]."/".$attrs["Y"];
			$Ms[$c] = $attrs["M"];
			$Ds[$c] = $attrs["D"];
			$Ys[$c] = $attrs["Y"];
			$dates[$c] = $date;
			break;
	 	case "LINK":
	 		$parent_name='link';
	 		break;
	 	case "MEDIA":
	 		$parent_name='media';
	 		break;
	 }
}

function tagData($parser, $tagData) {
  global $c, $tag_name, $parent_name;
  global $blurbs, $titles, $bodies;
  global $images, $videos;
	global $labels, $types, $urls;
  switch($tag_name) {
  	case "BLURB":
  		$blurbs[$c]=$tagData;
  		break;
  	case "TITLE":
			$titles[$c]=$tagData;
  		break;
  	case "BODY":
			$bodies[$c] .= $tagData;
  		break;
  	case "LABEL":
  		if($parent_name=='link')
				$labels[$c]=$tagData;
  		break;
  	case "TYPE":
  		if($parent_name=='link')
				$types[$c]=$tagData;
			break;
  	case "URL":
  		if($parent_name=='link')
				$urls[$c]=$tagData;
			break;
  	case "VIDEO":
  		if($parent_name=='media')
				$videos[$c]=$tagData;
  		break;
  	case "IMAGE":
			if($parent_name=='media')
				$images[$c]=$tagData;
  		break;
  }
  $tag_name = "";
}

function endTag($parser, $name) {
	$parent_name="";
	
}

$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser,XML_OPTION_SKIP_WHITE,false);
xml_set_element_handler($xml_parser, "startTag", "endTag");
xml_set_character_data_handler($xml_parser, "tagData");
$file = '../path_to_xml_file/home.xml';
$data = xml_parse($xml_parser,file_get_contents($file));
xml_parser_free($xml_parser);

if(!$data) {
   die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}

?>


:: gucci.com ::
:: blockbuster.com ::

--------------------

#5 Drumminxx

Drumminxx
  • Members
  • PipPipPip
  • Advanced Member
  • 92 posts
  • LocationUSA

Posted 30 July 2006 - 05:06 PM

looking at the code above, you have 3 functions

startTag - gets called when the parser reaches a start tag

tagData - will get all the data

endTag - gets called when the parser reaches an end tag

so pretty much it stores the xml into a global array that I can access in other pages

:: gucci.com ::
:: blockbuster.com ::

--------------------




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users