note 52583 added to ref.simplexml
| From: | paul at preinheimer dot com | Date: | Fri, 06 May 2005 12:11:32 +0000 |
| Subject: | note 52583 added to ref.simplexml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-88998@lists.php.net to get a copy of this message | ||
Hi,
If you want to access an element that has a dash in its name, (as is common with the XML documents
provided by the Library of Congress, as well as the NWS) you will need to handle it a little bit
differently.
You can either use XPATH, which works fine, but will return an array of results every time, even if
there is a single result.
eg.
$xml->xpath('/data/time-layout/start-valid-time'}
You can also choose just to encapsulate the element names containing a dash:
$xml->data->{'time-layout'}->{'start-valid-time'}
--
On a only partially related note, dealing with SimpleXML is one of the only times I have employed
casting with PHP. While iterating (foreach) through the valid times, echo'ing the element
worked great (it merely echo'ed the apropriate time), assigning it to another variable resulted
in a SimpleXML object containing the time to be assigned, rather than just the time itself. This was
resolved by casting the time to a string:
foreach($xml->data->{'time-layout'}->{'start-valid-time'} AS $time)
{
$weatherDates[] = (string) $time;
}
----
Manual Page -- http://www.php.net/manual/en/ref.simplexml.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+52583
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52583&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52583&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52583&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52583&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52583&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+52583&report=yes
Search -- http://master.php.net/manage/user-notes.php