surion Posted December 18, 2007 Share Posted December 18, 2007 hi at the moment I'm trying to write a script that generates an XSL file to transform an xml file to another xml file. to generate the xsl file I first need to analyse the xml file I get, to do this analising I use the next script I found on php.net: function readxmlfile($file) { $xml_parser = xml_parser_create(); xml_set_element_handler($xml_parser, "startElement", "endElement"); 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); } function startElement($parser, $name, $attrs) { global $depth; global $elements; echo "$name--$depth<br />\n"; $elements[] = array('name'=>$name,'depth'=>$depth); $depth++; } function endElement($parser, $name) { global $depth; $depth--; } it generates an array containing the names of the elements and their depths in the xml file, wich is the data I need for an analysis of the xml file. works perfect, TOUGH, I got one little problem, the element names I get are ALWAYS uppercase values. wich is not good since xsl is case sensitive. For example if an element in my input xml is called "CaTaLoG" i still get "CATALOG", any idea how to solve this problem? Offcourse I could scan trough the xml file before analysing it and perform some string replaces to make sure all tags are uppercase, but that would take the performance down on large xml files AND my output format shouldn't be only uppercase, so thats a bad solution any other suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/82204-analysing-xml-files-using-php/ Share on other sites More sharing options...
surion Posted December 19, 2007 Author Share Posted December 19, 2007 *bump* anybody? Quote Link to comment https://forums.phpfreaks.com/topic/82204-analysing-xml-files-using-php/#findComment-418360 Share on other sites More sharing options...
surion Posted December 20, 2007 Author Share Posted December 20, 2007 still having same problem,... by the way, is there anybody who knows an alternative for : xml_set_element_handler but than for attributes (meaning, next to the elements i also want to have the attributes (not their values, their names) in the array Quote Link to comment https://forums.phpfreaks.com/topic/82204-analysing-xml-files-using-php/#findComment-419360 Share on other sites More sharing options...
littledragon Posted December 20, 2007 Share Posted December 20, 2007 Sorry if this isn't what you were getting at, but have you tried simple_xml on php5? I think it leaves the element names intact OK Quote Link to comment https://forums.phpfreaks.com/topic/82204-analysing-xml-files-using-php/#findComment-419364 Share on other sites More sharing options...
surion Posted December 20, 2007 Author Share Posted December 20, 2007 aha, very intresting suggestion mate, i ve tested a little with it: data.xml: <?xml version="1.0" encoding="utf-8"?> <Properties> <Property> <Name>some data</Name> <Beds>some data</Beds> </Property> <Property> <Name>some more data</Name> <Beds>some more data</Beds> </Property> <Property> <Name>again some more data</Name> <Beds>again some more data</Beds> </Property> </Properties> my phpscript: $dom = new domDocument; $dom->load('data.xml'); $sx = simplexml_import_dom($dom); showdata($sx); function showdata($sx) { foreach((array) $sx as $tagname => $val) { if (is_string($val)) { echo $tagname."<br />"; } elseif (is_array($val)) { echo $tagname."<br />"; showdata($val); } elseif (is_object($val)) { echo $tagname."<br />"; showdata($val); } } } the output: Property 0 Name Beds 1 Name Beds 2 Name Beds Looks sweet, upper & lower cases are perfect now, BUT -what happened to my Root element Properties? -why does Property get only listed once? -where do those numbers come from? anywayz, i love the fact that i can use recursion now, one step closer again Quote Link to comment https://forums.phpfreaks.com/topic/82204-analysing-xml-files-using-php/#findComment-419386 Share on other sites More sharing options...
littledragon Posted December 20, 2007 Share Posted December 20, 2007 -what happened to my Root element Properties? Good question, never wanted to retrieve the root element before -why does Property get only listed once? Cos property is now an array key -where do those numbers come from? They're array heys too. imagine this: $Properties = array($property => array([0] => array('name' => 'somedata'... Quote Link to comment https://forums.phpfreaks.com/topic/82204-analysing-xml-files-using-php/#findComment-419395 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.