开发者

How to read all Nodes from XML file with using PHP? Not just specific nodes

开发者 https://www.devze.com 2023-03-25 03:36 出处:网络
this is my xml file: <ROOT> <WEB> <thumb>images/thumbs/thumb.jpg</thumb> <TITEL>Website</TITEL>

this is my xml file:

<ROOT>
  <WEB>
    <thumb>images/thumbs/thumb.jpg</thumb>
    <TITEL>Website</TITEL>
  </WEB>
  <FLYER>
    <thumb>images/thumbs/thumb.jpg</thumb>
    <TITEL>Flyer</TITEL>
  </FLYER>
  <CD>
    <thumb>images/thumbs/thumb.jpg</thumb>
    <TITEL>cd</TITEL>
  </CD>
</ROOT>

In my PHP file I want to read out from tha开发者_JAVA百科t xml file.

<?php
$q=$_GET["q"];

$xmlDoc = new DOMDocument();
$xmlDoc->load("references.xml");

if ($q=="ALL"){
    $root = $xmlDoc->documentElement;
    $products = $root->childNodes;
} else {
    $products=$xmlDoc->getElementsByTagName($q);
}

foreach( $products as $product ){
    $titles = $product->getElementsByTagName( "TITEL" );
    $title = $titles->item(0)->nodeValue;

    $thumbs = $product->getElementsByTagName( "thumb" );
    $thumb = $thumbs->item(0)->nodeValue;

    echo '<div style="float:left; margin:0 3px;">';
    echo "<img src='".$thumb."' /><br />";
    echo "<p style='text-align: center;'><b>".$title."</b></p></div>";
}
?>

The PHP code should check if the input is "ALL" and then just print out everything. If the input is "WEB", "FLYER", or "CD" then it prints out only these items, which is doing fine. But with all nodes, it prints out not correctly. there are a lot of div-tags with nothing in it. Does anyone know why and how I can solve it??

thanks


Where you have

$root = $xmlDoc->documentElement;
$products = $root->childNodes;

if you put

$products = $xmlDoc->getElementsByTagName("ROOT");

does it work?

0

精彩评论

暂无评论...
验证码 换一张
取 消