I'm trying to display specific nodes from a .xml file with PHP depending on the variable
I would like to know if there is another way to make it work or to know what's wrong with my code.
I'm trying to only display <name>
from the .xml file.
Here is my code that doesn't work:
function display_xml_nodes($xmlstr, $node) {
$xmlstr = simplexml_load_file("$xmlstr") or die("Error: name not found");
echo $xmlstr->foods->products->product->name . "\n";
}
display_xml_nodes("ex_04.xml", "name");
Here is the .xml
<food>
<foods>
<title>Products</title>
<products>
<product>
<name>Chcolate Bar</name>
<price>1</price>
</product>
<product>
<name>Milk</name>
<price>0.50</price>
</product>
<product>
<name>Water</name>
<price>0.50</price>
</product>
<product>
<name>Donut</name>
<price>1.50</price>
</product>
</products>
</foods>
</food>