20.3 Parsing an XML Document into an Array


You want to load an XML document into an array and examine it that way.

Technique

Use the xml_parse_into_struct() function to place your data into an array:

 faq-test.xml: <faqs>     <faq id="1">         <title>FAQ 1</title>         <code>             zval_dtor(tmp);         </code>         <text>             The above is a piece of code from rand.c in             <important>ext/standard</important> in the PHP distribution.         </text>     </faq> </faqs> 

The PHP file:

 <?php $parser = xml_parser_create (); $data = implode ("", file("faq-test.xml")); xml_parse_into_struct($parser, $data, $d_ar, $i_ar); $i = 0; foreach ($d_ar as $element) {     if (!preg_match("/^\s+$/", $element[value])) {         $tag = $element[tag];         $faq[$i][strtolower($tag)] = $element[value];     } elseif (isset($element[attributes])) {         $faq[$i][id] = $element[attributes][ID];         $i++;     } } ?> 

Comments

In recipe 20.2, you learned how to parse a basic XML document. Now you are exploring how to load an XML document into an array via the xml_parse_into_ struct() function and then extract the information from that array into a final and permanent array.

In the example, we are parsing an XML file, faq-test.xml. Now, let's go over the steps. First, we create a new parser and assign it to $parser . Then we load the file we want to parse, faq-test.xml, into the $data variable. After this is done, we call the xml_ parse_into_struct() function, which loads the XML file into an array specified by the third argument with an index structure specified by the fourth argument (note that you must pass these by reference).

When we have the data loaded into the array, it is simply a matter of extracting the data from that array. The $d_ar array has the following format:

 (    Array (        [tag] => "value for tag (ie ID)"        [type] => (openclosecomplete)        [value] => "what's inside the tags"        [attributes] => Array (                            [attrname] => "Name of the attribute"                               ...                        )        [level] => depth of XML    ) ) 

We immediately eliminate the outside array via a foreach loop, and we focus on the inside array. The first thing we test is whether the value of the element is completely whitespace. If so, we will have nothing to do with it unless it has attributes. If the value of the element contains information, we extract that information and assign it to the appropriate space in the $faq array.

The next test is whether current $element has an attributes parameter. If it does, it must be a <faq> tag and therefore contain the "ID" attribute (because our document has only one tag that contains attributes). Therefore, we assign the ID to the element in our associative array, and then move on to the next entry in our $faq array by incrementing $i .

As you can see, it is probably easier to use the xml_parse() function rather than the xml_parse_into_struct() function, simply because using xml_parse() is so much simpler. However, should you need to use the xml_parse_into_struct() function, I suggest that you take a quick look at the array of values returned by using the print_r() function.



PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net