0

I have a xml file I load it as the following:

//$file the file system path of the xml file
function getTopicsList($file){
        $doc = new DOMDocument();
        $doc->load( $file );
        var_dump($doc->getElementsByTagName('topic'));
        return $doc->getElementsByTagName('topic');
    }

The loaded xml file contents is something like the following:

<?xml version="1.0" encoding="UTF-8"?>
<topics>
    <topic>
        <title>Title1</title>
        <keywords>"Some Keys"</keywords>
    </topic>
    <topic>
        <title>The Title</title>
        <keywords>Another Key</keywords>
    </topic>
    <topic>
        <title>A Title</title>
        <keywords>Key two</keywords>
    </topic>
</topics>

The var_dump() in the above code just printout limited information such as:

object(DOMNodeList)#30 (1) {
  ["length"]=>
  int(3)
}

I expected that it should print at least the properties of that object i.e the xml tags and its values. I tried to use other functions such as print_r() and var_export() but there is no details I want.

SaidbakR
  • 13,303
  • 20
  • 101
  • 195

1 Answers1

0

No, this is node list. You can iterate it with foreach or access nodes using the item() method.

Node lists are used at different places, getElementsByTagName() is one, another is the $childNodes property. Xpath expressions return node lists, too.

Be aware that the nodes can be not only elements but several node types. Like text, cdata section or attribute.

You can use var_dump() to dump a single node. This works with PHP >= 5.3.11 or >= 5.4.1.

$dom = new DOMDocument();
$dom->loadXML('<foo/>'); 
var_dump($dom->documentElement);

Output:

object(DOMElement)#2 (18) { 
  ["schemaTypeInfo"]=> 
  NULL 
  ["tagName"]=> 
  string(3) "foo" 
  ["textContent"]=> 
  string(0) "" 
  ["baseURI"]=> 
  string(1) "/" 
  ["localName"]=> 
  string(3) "foo" 
  ["prefix"]=> 
  string(0) "" 
  ["ownerDocument"]=> 
    ...
ThW
  • 19,120
  • 3
  • 22
  • 44