Get a special type of child in an xml document with PHP -
i'm looking way special type of child out of xml file php. xml:
<notify type="post" name="max" />
i want grab name out of there. code: `$sender =
$sender = $node->getchild('notify'); $sender = $sender->getchild('name'); $sender = $sender->getdata();
but expected isn't working way. in advance help
you can use xpath
expression job done. sql query xml.
$results = $xml->xpath("//notify[@type='post']/@name");
assuming xml in $xml
, expression reads like
select notify nodes, type-attribute post, give name-attribute.
$results
will array, , code example made simplexml
. can use same xpath-expression
dom
though.
here's complete code:
$x = <<<xml <root> <notify type="post" name="max" /> <notify type="get" name="lisa" /> <notify type="post" name="william" /> </root> xml; $xml = simplexml_load_string($x); $results = $xml->xpath("//notify[@type='post']/@name"); foreach ($results $result) echo $result . "<br />";
output:
max william
see working: http://codepad.viper-7.com/eo29fk
Comments
Post a Comment