# SimpleXML returning attributes as objects instead of strings

- Thread: https://externals.io/message/11832
- Original archive: https://news-web.php.net/php.internals/11832
- Started by David Sklar on 2004-08-02T20:53:49+00:00
- Messages: 3
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #11832 — David Sklar — 2004-08-02T20:53:49+00:00

URL: https://externals.io/message/11832#11832

SimpleXML returns attribute values as SimpleXMLElement objects instead 
of strings. E.g, given this:

$sxe = simplexml_load_string('<octopus arms="eight" legs="">Doc 
Ock</octopus>');

$sxe['arms'] and $sxe['legs'] are SimpleXMLElement objects.

This mostly works OK, but causes problems when using empty() to test if 
an attribute has a value. empty($sxe['arms']) is false, but so is 
empty($sxe['legs']).

An easy workaround is to use `strlen()` instead of empty(), I suppose 
(except if an attribute value is "0"), but I am wondering why SimpleXML 
works this way.

Thanks,
David

---

## #11833 — Sterling Hughes — 2004-08-02T20:58:59+00:00

URL: https://externals.io/message/11832#11833 · In reply to: #11832

iterators...


> SimpleXML returns attribute values as SimpleXMLElement objects instead
> of strings. E.g, given this:
> 
> $sxe = simplexml_load_string('<octopus arms="eight" legs="">Doc
> Ock</octopus>');
> 
> $sxe['arms'] and $sxe['legs'] are SimpleXMLElement objects.
> 
> This mostly works OK, but causes problems when using empty() to test if
> an attribute has a value. empty($sxe['arms']) is false, but so is
> empty($sxe['legs']).
> 
> An easy workaround is to use `strlen()` instead of empty(), I suppose
> (except if an attribute value is "0"), but I am wondering why SimpleXML
> works this way.
> 
> Thanks,
> David

---

## #11834 — David Sklar — 2004-08-02T21:40:27+00:00

URL: https://externals.io/message/11832#11834 · In reply to: #11833

Ahh. Thanks.

David

Sterling Hughes wrote:
> iterators...
> 
> 
> 
> > […]
