ballhogjoni Posted October 7, 2007 Share Posted October 7, 2007 I have searched yahoo and google for about a week straight and cannot figure out how to parse specific elements in my xml file. I know how to start a parser, but when I parse my file it prints all the data of all the tags. Can someone show me how to print certain tag elements from a large xml file? EX. <cardlist> <card> <cid>111</cid> <category>Low Interest</category> </card> <card> <cid>112</cid> <category>0% APR</category> </card> </cardlist> From the example above I want to only print the <category> tag data for both <card> tags and not the <cid> tags. In my code below it prints everything to the browser. My code $file = "http://www.ccccccccccccccccc.com/partners/access/xml/xml.asp?uid=cccccccccccccc"; function contents($parser, $data){ echo $data; } function startTag($parser, $data){ echo "<b>"; } function endTag($parser, $data){ echo "</b><br />"; } $xml_parser = xml_parser_create(); xml_set_element_handler($xml_parser, "startTag", "endTag"); xml_set_character_data_handler($xml_parser, "contents"); 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); fclose($fp); Quote Link to comment https://forums.phpfreaks.com/topic/72190-how-to-parse-xml-elements-using-php/ Share on other sites More sharing options...
BlueSkyIS Posted October 7, 2007 Share Posted October 7, 2007 i have an easier time with xml if i put it into an array: function xml2ary(&$string) { $parser = xml_parser_create(); xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); xml_parse_into_struct($parser, $string, $vals, $index); xml_parser_free($parser); $mnary=array(); $ary=&$mnary; foreach ($vals as $r) { $t=$r['tag']; if ($r['type']=='open') { if (isset($ary[$t])) { if (isset($ary[$t][0])) $ary[$t][]=array(); else $ary[$t]=array($ary[$t], array()); $cv=&$ary[$t][count($ary[$t])-1]; } else $cv=&$ary[$t]; if (isset($r['attributes'])) {foreach ($r['attributes'] as $k=>$v) $cv['_a'][$k]=$v;} $cv['_c']=array(); $cv['_c']['_p']=&$ary; $ary=&$cv['_c']; } elseif ($r['type']=='complete') { if (isset($ary[$t])) { // same as open if (isset($ary[$t][0])) $ary[$t][]=array(); else $ary[$t]=array($ary[$t], array()); $cv=&$ary[$t][count($ary[$t])-1]; } else $cv=&$ary[$t]; if (isset($r['attributes'])) {foreach ($r['attributes'] as $k=>$v) $cv['_a'][$k]=$v;} $cv['_v']=(isset($r['value']) ? $r['value'] : ''); } elseif ($r['type']=='close') { $ary=&$ary['_p']; } } _del_p($mnary); return $mnary; } // _Internal: Remove recursion in result array function _del_p(&$ary) { foreach ($ary as $k=>$v) { if ($k==='_p') unset($ary[$k]); elseif (is_array($ary[$k])) _del_p($ary[$k]); } } function stripHeader($source) { list($header,$content) = explode("\r\n\r\n",$source,2); return $content; } use: $xmlarray = xml2ary($your_xml_here); Quote Link to comment https://forums.phpfreaks.com/topic/72190-how-to-parse-xml-elements-using-php/#findComment-364053 Share on other sites More sharing options...
ballhogjoni Posted October 7, 2007 Author Share Posted October 7, 2007 I am not so sure I know what you mean. I dont fully understand the concept of functions, so forgive me if I don't understand. What does this mean $xmlarray = xml2ary($your_xml_here); and where do I declare my xml file so I can parse it? Quote Link to comment https://forums.phpfreaks.com/topic/72190-how-to-parse-xml-elements-using-php/#findComment-364057 Share on other sites More sharing options...
BlueSkyIS Posted October 7, 2007 Share Posted October 7, 2007 use this, where $your_xml_here is the name of your file, for example: $xmlarray = xml2ary("somefile.xml"); then the xml will be parsed into the array $xmlarray. you can quickly view the contents using print_r($xmlarray); Quote Link to comment https://forums.phpfreaks.com/topic/72190-how-to-parse-xml-elements-using-php/#findComment-364059 Share on other sites More sharing options...
ballhogjoni Posted October 7, 2007 Author Share Posted October 7, 2007 cool, thx. I'll let you where im at. Quote Link to comment https://forums.phpfreaks.com/topic/72190-how-to-parse-xml-elements-using-php/#findComment-364061 Share on other sites More sharing options...
ballhogjoni Posted October 7, 2007 Author Share Posted October 7, 2007 when I use print_r($xmlarray); it just prints array() I used a foreach loop to try and extract the data but nothing happens when I do that. Quote Link to comment https://forums.phpfreaks.com/topic/72190-how-to-parse-xml-elements-using-php/#findComment-364066 Share on other sites More sharing options...
Barand Posted October 7, 2007 Share Posted October 7, 2007 if you have php5 <?php $str = '<cardlist> <card> <cid>111</cid> <category>Low Interest</category> </card> <card> <cid>112</cid> <category>0% APR</category> </card> </cardlist>'; $xml = simplexml_load_string($str); foreach ($xml->card as $c) { echo $c->category, '<br/>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/72190-how-to-parse-xml-elements-using-php/#findComment-364099 Share on other sites More sharing options...
ballhogjoni Posted October 7, 2007 Author Share Posted October 7, 2007 no I wish, I only have php 4 cuz of my host. Quote Link to comment https://forums.phpfreaks.com/topic/72190-how-to-parse-xml-elements-using-php/#findComment-364167 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.