Hi there PHP Builders 🙂 I would like to say 'Hello World!' because that's my first post in this forum.

At the beginning of my XML adventure I faced such a problem:

structure.xml

<?xml version="1.0" encoding="utf-8"?>
<gallery>
	<category id="1" txt="Blues">
		<pic desc="Coma">1.jpg</pic>
		<pic desc="Jaka&#347;tam kapelka">2.jpg</pic>
		<pic desc="Majka jerzowska">3.jpg</pic>
	</category>

<category id="2" txt="Przyroda">
	<pic desc="Jakie&#347; drzewo">4.jpg</pic>
</category>	

<category id="3" txt="Samochody">
	<pic desc="Maluch">5.jpg</pic>
	<pic desc="Mercedes">6.jpg</pic>
</category>	
</gallery>

index.php

$doc = new DomDocument;
$doc->Load('structure.xml');
$list = $doc->getElementsByTagName('category');
echo count($list)."<br />";
var_dump($list);
  • both files exist in root website folder
  • php code returns:

1
object(DOMNodeList)#4 (0) { } &#65279;

Why php code doesn't put those three 'category' elements from .xml file to $list?

I would be thankful for tips. Greetings. P.

    I believe it does, but as a dom node list and not an array. Try

    for ($i = 0; i < $list->length; ++$i) {
    	// Use this with whatever you need...
    	$list->item($i);
    }
      Write a Reply...