PHP: Displaying Dom object and Creating xml file
Posted
by pavun_cool
on Stack Overflow
See other posts from Stack Overflow
or by pavun_cool
Published on 2010-05-21T07:02:21Z
Indexed on
2010/05/21
7:10 UTC
Read the original article
Hit count: 270
<?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.
© Stack Overflow or respective owner