Skip to content
Advertisement

simplexml_load_file does not recognize tags

I am trying to use simplexml_load_file() to load an XML file and reach <dc:title> tags, but when I dump the $xml variable that loaded my xml file all the tags that starts with <dc: doesn’t show. Is there a way of telling the parser to include them? should I use a different parser?

Here’s my example file’s code:

JavaScript

Code I used to check for which tags are seen:

JavaScript

And the result:

JavaScript

metadata is in fact, empty. Thoughts?

Advertisement

Answer

First, dc is a namespace, not part of tag name. For testing purpose you can print the namespaces used in the given xml. Executing the snippet

JavaScript

will output

JavaScript

You have to register a prefix for the namespace for usage with xpath, then you can use it for selection:

JavaScript

This will output test of the element you want to get.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement