You are here: Re: [PHP] SimpleXML doesn't work with one single XML tag line... « PHP « IT news, forums, messages
Re: [PHP] SimpleXML doesn't work with one single XML tag line...

Posted by Christian Stocker on 10/01/31 11:21

On 7/15/05, Daevid Vincent <daevid@daevid.com> wrote:
> Maybe I'm missing something about XML specifications, but this doesn't seem
> right to me.
>
> http://www.php.net/manual/en/ref.simplexml.php
>
> Here is a code fragment I use:
>
> $xml = simplexml_load_string($returnData);
> var_dump($xml);
> if (is_object($xml))
> {
> //first lets make sure we didn't get an <ERROR>
> if ($xml->error)
> {
> echo "An ERROR was encountered\n";
> echo "CODE: ".((string)$xml->error->code)."\n";
> echo "STRING: ".((string)$xml->error->string)."\n";
> exit;
> }
>
> ...
> }
>
> I have my XML setup such that if there is an error, I return:
>
> <?xml version="1.0" encoding="iso-8859-1" ?>
> <error code="tbd" string="user not found." />

this are attributes, which have to accessed differently. and the first
object you get is already the root node (error in your case).

try echo "CODE: ".((string)$xml['code'])."\n";

(not tested, but should work)

chregu

>
> But simple XML will not detect this as illustrated by var_dump($xml)
>
> object(SimpleXMLElement)#1 (0) {
> }
>
> However, if I change my output to be:
>
> <?xml version="1.0" encoding="iso-8859-1" ?>
> <error>
> <code>tbd</code>
> <string>user not found with those credentials.</string>
> </error>
>
> Then I get...
>
> object(SimpleXMLElement)#1 (2) {
> ["code"]=>
> string(3) "tbd"
> ["string"]=>
> string(38) "user not found."
> }
>
> Notice also that my object is not nested inside an ["error"] as I would
> expect it to be.
> So I would expect to get this (in BOTH cases actually):
>
> object(SimpleXMLElement)#1 (1) {
> ["error"]=>
> object(SimpleXMLElement)#1 (2) {
> ["code"]=>
> string(3) "tbd"
> ["string"]=>
> string(38) "user not found."
> }
> }
>
> More useful information:
>
> root@DAEVID:/lockdown/includes/api# php -v
> PHP 5.0.3 (cli) (built: Jul 11 2005 12:33:48)
> Copyright (c) 1997-2004 The PHP Group
> Zend Engine v2.0.3, Copyright (c) 1998-2004 Zend Technologies
> with Zend Extension Manager v1.0.6, Copyright (c) 2003-2004, by Zend
> Technologies
> with Zend Optimizer v2.5.7, Copyright (c) 1998-2004, by Zend
> Technologies
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


--
christian stocker | Bitflux GmbH | schoeneggstrasse 5 | ch-8004 zurich
phone +41 1 240 56 70 | mobile +41 76 561 88 60 | fax +41 1 240 56 71
http://www.bitflux.ch | chregu@bitflux.ch | gnupg-keyid 0x5CE1DECB

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация