Zend XML parsing

Posted by Vincent on Stack Overflow See other posts from Stack Overflow or by Vincent
Published on 2010-04-05T18:12:45Z Indexed on 2010/04/05 18:33 UTC
Read the original article Hit count: 224

Filed under:
|

I have an xml file called error.xml like this:

<?xml version="1.0" encoding="UTF-8"?>
<errorList>
    <error>
        <code>0</code>
        <desc>Fault</desc>
        <ufmessage>Fault</ufmessage>
    </error>
    <error>
        <code>1</code>
        <desc>Unknown</desc>
        <ufmessage>Unknown</ufmessage>
    </error>
    <error>
        <code>2</code>
        <desc>Internal Error</desc>
        <ufmessage>Internal Error</ufmessage>
    </error>
</errorList>

I am using Zend and have the above xml file stored in a Zend Registry variable called "apperrors" like this:

$apperrors = new Zend_Config_Xml( 'error.xml');
Zend_Registry::set('apperrors', $apperrors->errorList);

If my code throws an error code of 2, I want a snippet of code to check the error code with "code" key in "apperrors" array and echo the ufmessage value corresponding to it. How can I do this?

Ex: 

//Error thrown by the code. Hardcoded for now...
$errorThrownByCode  = 2;

//Get the Zend Registry value for all errors
$errorList =  Zend_Registry::get('apperrors');

//Write some code to compare $errorThrownByCode with $errorList->error->code

//If found echo $errorList->error->ufmessage, else echo "An Unknown error occured";

Thanks

© Stack Overflow or respective owner

Related posts about zend-framework

Related posts about php5