Balmung-San Posted July 30, 2006 Share Posted July 30, 2006 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. Quote Link to comment Share on other sites More sharing options...
micah1701 Posted July 30, 2006 Share Posted July 30, 2006 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. Quote Link to comment Share on other sites More sharing options...
Drumminxx Posted July 30, 2006 Share Posted July 30, 2006 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. Quote Link to comment Share on other sites More sharing options...
Drumminxx Posted July 30, 2006 Share Posted July 30, 2006 [code]<?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)));}?>[/code] Quote Link to comment Share on other sites More sharing options...
Drumminxx Posted July 30, 2006 Share Posted July 30, 2006 looking at the code above, you have 3 functionsstartTag - gets called when the parser reaches a start tagtagData - will get all the dataendTag - gets called when the parser reaches an end tagso pretty much it stores the xml into a global array that I can access in other pages Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.