Zend: How to manage XML data where multiple elements have the same name using Zend_Config_Xml?

995 Views Asked by At

In attempting to extract data from an XML file using Zend_Config_Xml, I am looking for the best way of processing this data where multiple elements have the same name. Please take a look at the following example.

Here is the XML file:

<?xml version="1.0" encoding="UTF-8" ?>
<root>
    <stylesheets>
        <stylesheet>example1.css</stylesheet>
        <stylesheet>example2.css</stylesheet>
    </stylesheets>
</root>

Here is the code:

$data = new Zend_Config_Xml('./path/to/xml_file.xml', 'stylesheets');
$stylesheets = $data->stylesheet->toArray();

What I would like to do is iterate through the $stylesheet array using a foreach loop, extracting the file name(s) and then append the stylesheet(s) to headLink(). This works fine... however, I run into issues when the number of <stylesheet> elements are less than 2. So for instance, if we remove <stylesheet>example2.css</stylesheet> from the XML file, I run into a Fatal error: Call to a member function toArray() on a non-object. How would you deal with this situation?

UPDATE 1 - Alternative SimpleXML Solution:

Personally, I solved this problem using SimpleXML as the Zend was causing me too many grey hairs. This will work even if there are no <stylesheet> elements. Unfortunately, I don't feel its very "slick" and was hoping for a Zend solution.

// define path to skin XML config file
$path = './path/to/file';

if (file_exists($path)) {
    // load the config file via SimpleXML
    $xml = simplexml_load_file($path);
    $stylesheets = (array)$xml->stylesheets;

    // append each stylesheet
    foreach ($stylesheets as $stylesheet) {
        if (is_array($stylesheet)) {
            foreach ($stylesheet as $key => $value) {
                $this->setStylesheet('/path/to/css/' . $value);
            }
        } else {
            $this->setStylesheet('/path/to/css/' . $stylesheet);
        }
    }
}

// function to append stylesheets
private function setStylesheet($path)
{
    $this->view->headLink()->appendStylesheet($path);
}

UPDATE 2 - Unwieldy Zend Solution:

Based on feedback, this solution works with 0 to many number stylesheet elements... it isnt very pretty. I was hoping for a loosely coupled design, something to standardize on which you could use interchangeably and yet simple to implement at the same time.

// load the skin config file
$path = './path/to/file.xml';
if (file_exists($path)) {
    $data = new Zend_Config_Xml($path, 'stylesheets');
    $stylesheets = $data->toArray();

    // append each stylesheet
    if (array_key_exists('stylesheet', $stylesheets)) {
        foreach ((array)$stylesheets['stylesheet'] as $key => $value) {
            $this->view->headLink()->appendStylesheet(
                '/path/to/css/' . $value);
        }
    }
}
1

There are 1 best solutions below

0
On

Get the array and force to array if only 1 element:

$data = new Zend_Config_Xml($c, 'stylesheets');
$data = $data->toArray();
var_dump((array) $data['stylesheet']);