note 91057 added to ref.simplexml
| From: | codejunky32atNOSPAMdotcom at osu1 dot php dot net | Date: | Fri, 22 May 2009 15:45:53 +0000 |
| Subject: | note 91057 added to ref.simplexml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-155892@lists.php.net to get a copy of this message | ||
Just wanted to add a post as to how you can extract the value from a SimpleXMLElement. Its not as
straightforward as you think. Because its a complex object you can't just access the element
directly. Here is a sample of data that represents a var_dump of a SimpleXmlElement
array(1) {
[0]=>
object(SimpleXMLElement)#13 (2) {
["@attributes"]=>
array(1) {
["name"]=>
string(5) "title"
}
[0]=>
string(19) "PHP Tech Book"
}
}
If you want to extract the title of the book you have to cast the specified element to a string like
so.
$newTitle = (string) $title[0];
The $title variable is the SimpleXMLElement that you have extracted from the xml document using
simplexml_load_string for instance. To initially access the title element from the xml document you
can do like so, using xpath.
$title = $doc->xpath('str[@name="title"]');
Hope this helps someone out there.
----
Server IP: 209.41.74.194
Probable Submitter: 70.99.150.250
----
Manual Page -- http://www.php.net/manual/en/ref.simplexml.php
Edit -- https://master.php.net/note/edit/91057
Del: integrated -- https://master.php.net/note/delete/91057/integrated
Del: useless -- https://master.php.net/note/delete/91057/useless
Del: bad code -- https://master.php.net/note/delete/91057/bad+code
Del: spam -- https://master.php.net/note/delete/91057/spam
Del: non-english -- https://master.php.net/note/delete/91057/non-english
Del: in docs -- https://master.php.net/note/delete/91057/in+docs
Del: other reasons-- https://master.php.net/note/delete/91057
Reject -- https://master.php.net/note/reject/91057
Search -- https://master.php.net/manage/user-notes.php