<?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'] )
);
#$author->appendChild( $doc->createTextNode( 'pavunkumar'));
$new = $doc->createElement("Developer");
$a=$doc->createTextNode('I am developer ' );
$new->appendChild($a);
$b->appendChild( $author );
$b->appendChild($new);
$b->appendChild($new);
$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() ;
?>
When I run this code in command line. I am getting following things
<?xml version="1.0"?>
<books>
<book>
<author>Jack Herrington</author>
<Developer>I am developer </Developer>
<title>PHP Hacks</title>
<publisher>O'Reilly</publisher>
</book>
<book>
<author>Jack Herrington</author>
<Developer>I am developer </Developer>
<title>Podcasting Hacks</title>
<publisher>O'Reilly</publisher>
</book>
</books>
When I run the code in web browser it gives me following things
Jack Herrington I am developer O'Reilly Jack Herrington I am developer O'Reilly
I want to above output to be like command line output. And one more things is that instead of displaying , how could I create a xml file using $doc Dom object.