XML : Error while creating XML document in PHP

I got this error while trying to create XML document from database data:

error on line 2 at column 1: Extra content at the end of the document

Here's my code:

  <?php  header('Content-type: text/xml');    $mysqli = new mysqli('127.0.0.1', 'root', '', 'db_test');    $xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><root></root>');    $currency = $xml->addChild('currency');  $currency->addAttribute('rate', '8');  $currency->addAttribute('code', 'PLZ');    $catalog = $xml->addChild('catalog');  $items = $xml->addChild('items');    $result = $mysqli->query("SELECT * FROM `catalog`");  while($it = $result->fetch_object()) {      $item = $items->addChild('item');      $item->addAttribute('id', $it->id);      $item->addAttribute('selling_type', "r");      $item->addChild('name', $it->name.", ".$it->comment);      $item->addChild('categoryId', $it->parent_id);      $item->addChild('priceplz', $it->cost);            $r = $mysqli->query("SELECT `image` FROM `catalog_images` WHERE `catalog_id`='".$it->id."' AND `main`=1 LIMIT 1");      if($i = $r->fetch_object()){        $item->addChild('image', "/Media/images/catalog/big/".$i->image);      }      unset($r);      unset($i);        $item->addChild('barcode', $it->artikul);      $item->addChild('description', $it->text);      $item->addChild('available', "true");      unset($item);  }  unset($result);  unset($it);    $result = $mysqli->query("SELECT * FROM `catalog_tree`");  while($cat = $result->fetch_object()) {      $category = $catalog->addChild('category', $cat->name);      $category->addAttribute('id', $cat->id);      if($cat->parent_id != 0) {          $category->addAttribute('portal_id', $cat->id);          $category->addAttribute('parentId', $cat->parent_id);      }      unset($category);  }  unset($result);  unset($cat);    $mysqli->close();    $xml->asXML('text.xml');    ?>    

If I'll comment the 'items' loop it works fine, otherwise it doesn't work... Am I blind or what?

No comments:

Post a Comment