mdarling
mdarling

Reputation: 77

How does simplexml handle elements that are prefixed?

in the application simplexml has been converting an rss feed to an object and the elements have been extremely easy to reference. are prefixed elements (eg <this:that> )referenced the same way as a non prefixed element($item->this) in the object.

I have found no information in the manual on php.net.

Upvotes: 0

Views: 909

Answers (2)

Crashspeeder
Crashspeeder

Reputation: 4311

Not quite. When you do a print_r() or var_dump() of an object representing namespaced XML you'll notice all the namespaced nodes are missing. There are a few ways to get namespaced nodes. One way is using registerXPathNamespace() in conjunction with xpath():

$xml = simplexml_load_file('some/namespaced/xml/file.xml');
$xml->registerXPathNamespace('prefix', 'http://Namespace/Uri/Here');
$xml->xpath('//prefix:node'); //get all <prefix:node> XML nodes.

Another way is to use children() to get child nodes:

$xml = simplexml_load_file('some/namespaced/xml/file.xml');
$xml->children('prefix', true); //get all nodes with the 'prefix' prefix that are direct descendants of the root node.
$xml->someNode->children('blah', true); //get all nodes with the 'blah' prefix that are direct descendants of <someNode>

Upvotes: 3

mseancole
mseancole

Reputation: 1672

What you are looking for is registerXPathNamespace(). You register each namespace then you can use each node like you normally would.

Upvotes: 0

Related Questions