Skip to content

Instantly share code, notes, and snippets.

@micheee
Created May 15, 2011 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save micheee/ca015c4f81f8c7c25f53 to your computer and use it in GitHub Desktop.
Save micheee/ca015c4f81f8c7c25f53 to your computer and use it in GitHub Desktop.
Namespaces with simplexml
<?php
$str = <<<EOD
<emp>
<name>Peter</name>
<category name="Employee" label="Mitarbeiter"/>
<xy:gender>Male</xy:gender>
<xy:website xy:href="http://www.xyz.com/peter">Peter&#x27;s Homepage</xy:website>
</emp>
EOD;
$xmlObj = simplexml_load_string($str,'SimpleXMLElement', LIBXML_NOWARNING | LIBXML_NOXMLDECL | LIBXML_NOERROR);
$xmlObj->registerXPathNamespace('xy','http://myapp.org/xy');
$web = $xmlObj->xpath('/emp/website');
var_dump($web);
/*
outputs:
array(1) {
[0]=>
object(SimpleXMLElement)#2 (2) {
["@attributes"]=>
array(1) {
["href"]=>
string(24) "http://www.xyz.com/peter"
}
[0]=>
string(16) "Peter's Homepage"
}
}
*/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment