simplexml_load_file does not recognize tags

2019-03-01 16:20发布

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?

1条回答
SAY GOODBYE
2楼-- · 2019-03-01 16:45

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

$namespaces = $xml->getNamespaces(true);
var_dump($namespaces);

will output

array(3) {
  '' =>
  string(28) "http://www.idpf.org/2007/opf"
  'xml' =>
  string(36) "http://www.w3.org/XML/1998/namespace"
  'dc' =>
  string(32) "http://purl.org/dc/elements/1.1/"
}

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

$xml->registerXPathNamespace('b', 'http://purl.org/dc/elements/1.1/');
$a =$xml->xpath('//b:*/text()');
foreach($a as $t) {
   print($t."\n");
}

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

查看更多
登录 后发表回答