Le Trung Hieu
Le Trung Hieu

Reputation: 83

Zend_Config_Xml

So i will list my problems here, please help

First of all, i use Zend_Http_Client to call to an url:

$client = new Zend_Http_Client('http://mydomain.com/Statistics.aspx?activity=top');
$response = $client->request('POST');
$response = $response->getRawBody();

Then i get an Xml document structure for $response when i print it out:

[?xml version='1.0' encoding='UTF-8'?]

[root]
    [member]
        [username>gh_MXH[/username]
        [money]129300[/money]
    [/member]
    [member]
        [username]sonhothiet_MXH[/username]
        [money]107400[/money]
    [/member]
[/root]

After that, i use Zend_Config_Xml:

$xmlReader = new Zend_Config_Xml($response);
$xml = $xmlReader->toArray();

But all i get is just only the first element of the array when i print out $xml:

Array
(
    [member] => Array
        (
            [username] => gh_MXH
            [money] => 129300
        )

)

Please show me how to get all the elements so that i can do the looping like:

foreach($xml as $key => $value){
    echo $value['username'] . 'has' . $value['money'];
}

And one more question, when i wrote:

$xml = new Zend_Config_Xml($response, 'root');

It appears an error saying: Section 'root' cannot be found in. Really need your help. Thank you and sorry for bad English.

Upvotes: 0

Views: 613

Answers (1)

Tim Fountain
Tim Fountain

Reputation: 33148

Don't use Zend_Config_Xml to parse standard XML docments, it is for config files. PHP can easily do this natively:

$xml = simplexml_load_string($response);
foreach($xml->member as $member){
    echo $member->username . 'has' . $member->money;
}

Upvotes: 1

Related Questions