Jump to content

Archived

This topic is now archived and is closed to further replies.

ivobenedito

PHP DOM & DOM->saveXML() prblm !!

Recommended Posts

Hi,

I'm creating a new XML Document in php using PHP DOM and when i use the method saveXML() i just get a blank string instead of a string with the content of the xmlDoc that i created. Here is the code :

[code]<?php
$doc = new DOMDocument('1.0');
$doc->formatOutput = true;

$root = $doc->createElement('book');
$root = $doc->appendChild($root);

$title = $doc->createElement('title');
$title = $root->appendChild($title);

$text = $doc->createTextNode('This is the title');
$text = $title->appendChild($text);

echo "Retrieving all the document:\n";
echo $doc->saveXML() . "\n";

echo "Retrieving only the title part:\n";
echo $doc->saveXML($title);
?>[/code]

Does anyone know what may be the problm ?? It just doesn't work with or without argument's in saveXML method and doesn't output any compilation error. I just get a blank browser page ;( !!

Share this post


Link to post
Share on other sites
Odd. I know you got that code from the manual, so I tried the same code myself and got the same results. I'll look into it some more.

Share this post


Link to post
Share on other sites

I found this one for you hope it works, I looked around the net and the above code is all over the net but dosent seem to work but try this good luck.
[code]
<?php
  $books = array();
  $books [] = array(
  'title' => 'PHP Hacks',
  'author' => 'Jack Herrington',
  'publisher' => "O'Reilly"
  );
  $books [] = array(
  'title' => 'Podcasting Hacks',
  'author' => 'Jack Herrington',
  'publisher' => "O'Reilly"
  );
  
  $doc = new DOMDocument();
  $doc->formatOutput = true;
  
  $r = $doc->createElement( "books" );
  $doc->appendChild( $r );
  
  foreach( $books as $book )
  {
  $b = $doc->createElement( "book" );
  
  $author = $doc->createElement( "author" );
  $author->appendChild(
  $doc->createTextNode( $book['author'] )
  );
  $b->appendChild( $author );
  
  $title = $doc->createElement( "title" );
  $title->appendChild(
  $doc->createTextNode( $book['title'] )
  );
  $b->appendChild( $title );
  
  $publisher = $doc->createElement( "publisher" );
  $publisher->appendChild(
  $doc->createTextNode( $book['publisher'] )
  );
  $b->appendChild( $publisher );
  
  $r->appendChild( $b );
  }
  
  echo $doc->saveXML();
  ?>

[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.