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:
<?xml version='1.0' encoding='utf-8'?>
<package xmlns="http://www.idpf.org/2007/opf" prefix="ibooks: http://vocabulary.itunes.apple.com/rdf/ibooks/vocabulary-extensions-1.0/" unique-identifier="pub-id" version="3.0" xml:lang="en">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:title>test</dc:title>
</metadata>
</package>
Code I used to check for which tags are seen:
$xml = simplexml_load_file('test.xml');
echo '<pre>';var_dump($xml);echo '</pre>';
And the result:
object(SimpleXMLElement)#1 (2) {
["@attributes"]=>
array(3) {
["prefix"]=>
string(80) "ibooks: http://vocabulary.itunes.apple.com/rdf/ibooks/vocabulary-extensions-1.0/"
["unique-identifier"]=>
string(6) "pub-id"
["version"]=>
string(3) "3.0"
}
["metadata"]=>
object(SimpleXMLElement)#2 (0) {
}
}
metadata is in fact, empty. Thoughts?
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 snippetwill output
You have to register a prefix for the namespace for usage with xpath, then you can use it for selection:
This will output
test
of the element you want to get.