Page 1 of 1

Formatting an awkward XML response

Posted: Sat Dec 24, 2011 7:56 am
by joshjones612
Hi guys,
I have created a HTTP client using the Zend Framework, I am able to make a request to the server and I get an xml response in this format

Code: Select all

<pre>HTTP/1.1 200 OK
Date: Sat, 24 Dec 2011 13:22:24 GMT
Server: Apache/2.2.21 (Win32) mod_ssl/2.2.21 OpenSSL/1.0.0e PHP/5.3.8 mod_perl/2.0.4 Perl/v5.10.1
X-powered-by: PHP/5.3.8
Content-disposition: inline; filename="response.xml"
Cache-control: private, must-revalidate, pre-check=0, post-check=0, max-age=0
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Pragma: no-cache
Accept-ranges: none
Content-length: 1017
Connection: close
Content-type: application/xml; charset=utf-8

<?xml version="1.0" encoding="UTF-8" ?>
<RESPONSE>
<MULTIPLE>
<SINGLE>
<KEY name="id"><VALUE>1</VALUE>
</KEY>
<KEY name="username"><VALUE>guest</VALUE>
</KEY>
<KEY name="firstname"><VALUE>Guest user</VALUE>
</KEY>
<KEY name="lastname"><VALUE> </VALUE>
</KEY>
<KEY name="fullname"><VALUE>Guest user  </VALUE>
</KEY>
<KEY name="department"><VALUE></VALUE>
</KEY>
<KEY name="firstaccess"><VALUE>0</VALUE>
</KEY>
<KEY name="lastaccess"><VALUE>0</VALUE>
</KEY>
<KEY name="auth"><VALUE>manual</VALUE>
</KEY>
<KEY name="confirmed"><VALUE>1</VALUE>
</KEY>
<KEY name="idnumber"><VALUE></VALUE>
</KEY>
<KEY name="lang"><VALUE>en</VALUE>
</KEY>
<KEY name="theme"><VALUE></VALUE>
</KEY>
<KEY name="timezone"><VALUE>99</VALUE>
</KEY>
<KEY name="mailformat"><VALUE>1</VALUE>
</KEY>
<KEY name="profileimageurlsmall"><VALUE>http://localhost/moodle/pluginfile.php/29/user/icon/f2</VALUE>
</KEY>
<KEY name="profileimageurl"><VALUE>http://localhost/moodle/pluginfile.php/29/user/icon/f1</VALUE>
</KEY>
</SINGLE>
</MULTIPLE>
</RESPONSE>
</pre>
[ /code ]

I am trying to get only the username, this is the code I use to get the response, but it won't work due to the quote marks, any ideas how I could structure this to only get the username or id? 

[syntax="php" ] 
$response = $client->request('POST');
$data = simplexml_load_string($response->getBody());
$id = (string) $data->response->multiple->single->KEY name="username"->value;
print ($id)[ /syntax]

Thank you in advance.

Re: Formatting an awkward XML response

Posted: Tue Dec 27, 2011 1:36 am
by Christopher
Instead of:

Code: Select all

<KEY name="id"><VALUE>1</VALUE>
</KEY>
<KEY name="username"><VALUE>guest</VALUE>
</KEY>
Do:

Code: Select all

<id>1</id>
<username>guest</username>