August 9, 2011

Problem reading xml file from Zend

Question by mrN

I created a simple xml file to store name of stylesheets, where is my xml file.

<?xml version="1.0" encoding="UTF-8"?>
<skin>
    <stylesheets>
        <stylesheet>default.css</stylesheet>
    </stylesheets>
</skin>

Now I tried to read the data using the following code

protected function loadSkin() {
    $skinData = new Zend_Config_Xml('./template/'.$this -> _template.'/skins/'.$this -> _skin.'/skin.xml');
    var_dump($skinData);
    $stylesheets = $skinData -> stylesheets -> stylesheet -> toArray();
    if(is_array($stylesheets)) {
        foreach($stylesheets as $stylesheet) {
            $this -> view -> headLink() -> appendStylesheet('/template/'.$this -> _template.'/skins/' . $this -> _skin . '/css/' . $stylesheet);
        }
    }       
}

But its gives Call to a member function toArray() on a non-object. What am i doing wrong?

Answer by Starx

Actually, there is nothing wrong with your code. Its just a logical error. You have told Zend_Config_Xml to parse the stylesheet as an array. But since there is just one stylesheet you are giving, it will not parse it as an array.

Your Solution

Just add another stylesheet to the xml file.

Like

<?xml version="1.0" encoding="UTF-8"?>
<skin>
    <stylesheets>
        <stylesheet>default.css</stylesheet>
        <stylesheet>another.css</stylesheet>
    </stylesheets>
</skin>

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!