note 82795 deleted from ref.xml by danbrown

From: Date: Wed, 03 Mar 2010 18:56:55 +0000
Subject: note 82795 deleted from ref.xml by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-166631@lists.php.net to get a copy of this message
Note Submitter: galen dot senogles at gmail dot com ---- If anyone else is having issues figuring out how to utilize the xml class that people have created and modified, don't worry as you are not alone. It took me a bit to come up with a solution that I liked, but I feel this does the job quite nice. I read through the entire structure of the xml file and create an associative array based on the tag names. I didn't worry about tag attributes as I didn't need to use them; so remember that if you use this method, you are only getting the tag name and the data inside the tag...that is all, no attributes!! I am not going to include the xml class as it has been copy pasted multiple times already on this thread. Just scroll down for the xml class. First let me show just an example of the EXTREMELY simple xml structure I was working with. Again, you will need to make modifications depending on the structure of the xml file you are working with! (I know I could use simplexml but I have php4 and not 5). <?xml version="1.0"?> <menuitems> <menutype>1</menutype> <product>Just some product info</product> <shipping>some stuff</shipping> </menuitems> The custom associative array push function taken from: http://us.php.net/manual/en/function.array-push.php#58705 The xml class file is located here: http://us.php.net/manual/en/ref.xml.php#81910 <?php // Obtain the exact path to the xml file $xmlfile = "mydata.xml"; $fp = fopen($xmlfile,"r"); // open the xml file $xml = fread($fp, filesize($xmlfile)); // read in the size of the file into the variable xml fclose($fp); // close the stream $xml_parser = new xml(); // create a new xml class instance $xml_parser->parse($xml); // parse the variable xml which contains our xml data $dom = $xml_parser->dom; // make a variable that holds the entire dom /* This part extracts the xml nodes from the dom and places them into an associative array. The associative array key is the name of the tag; the value is the tag contents. We simply create an array on the fly using the name and contents, and hit that array with our original array using the array_push_associative function. We then check if isset to prevent errors from being displayed. If the tag contents are empty, I put the string isempty inside so I can easily check to see later if there is contents or not. */ $xmlarray = array(); // the array we are going to store the information within the tag $contents = ""; foreach($dom['child_nodes'][0]['child_nodes'] as $key => $value) { $tagname = $value['tag_name']; if(isset($value['child_nodes'][0])) { $numarrays = count($value['child_nodes']); if($numarrays > 1) { foreach($value['child_nodes'] as $key => $value2) { $contents .= $value2; } }else { $contents = $value['child_nodes'][0]; } }else { $contents = 'isempty'; } $artmp = array($tagname => $contents); array_push_associative($xmlarray,$artmp); unset($artmp); } unset($xml); // free up resources unset($xml_parser); // free up resources unset($dom); // free up resources ?> You may be wondering why there is a nested count and foreach loop inside the main foreach loop. The reason that exists is that the xml class that I am using in this example, the one that is four posts down from this one, has the wonderful behavior in that when something hits the length of 1024 characters, it creates a new element in the array and puts the next 1024 characters into that next element etc. This caused me massive confusion as to why some of my data was getting cut off. So say I wanted to display the data inside the product tag, all I would need to do is this: <?php echo $xmlarray['product'] ?> I sincerely hope this helps people figure out how to utilize the xml class quicker than I did! If anyone has suggestions, modifications, or whatever, please post it here! Thanks

« previous php.notes (#166631) next »