note 82795 added to ref.xml
| From: | galendotsenoglesatgmaildotcom at osu1 dot php dot net | Date: | Fri, 25 Apr 2008 21:28:34 +0000 |
| Subject: | note 82795 added to ref.xml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-138784@lists.php.net to get a copy of this message | ||
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
----
Server IP: 208.69.120.35
Probable Submitter: 24.199.49.211
----
Manual Page -- http://www.php.net/manual/en/ref.xml.php
Edit -- https://master.php.net/note/edit/82795
Del: integrated -- https://master.php.net/note/delete/82795/integrated
Del: useless -- https://master.php.net/note/delete/82795/useless
Del: bad code -- https://master.php.net/note/delete/82795/bad+code
Del: spam -- https://master.php.net/note/delete/82795/spam
Del: non-english -- https://master.php.net/note/delete/82795/non-english
Del: in docs -- https://master.php.net/note/delete/82795/in+docs
Del: other reasons-- https://master.php.net/note/delete/82795
Reject -- https://master.php.net/note/reject/82795
Search -- https://master.php.net/manage/user-notes.php