Check out following PHP code folr dynamic creation and output it in browser
$books = array(
array (
id => 1,
author => "Someone",
name => "Professional PHP"
),
array (
id => 2,
author => "Nicholas C",
name => "Professional Ajax"
),
array (
id => 3,
author => "Joe Williams",
name => "Beginning PHP"
)
);
$dom = new DomDocument();
$dom->formatOutput = true;
$root = $dom->createElement( "books" );
$dom->appendChild( $root );
foreach( $books as $book )
{
$bn = $dom->createElement( "book" );
$bn->setAttribute( 'id', $book['id'] );
$author = $dom->createElement( "author" );
$author->appendChild( $dom->createTextNode( $book['author'] ) );
$bn->appendChild( $author );
$name = $dom->createElement( "name" );
$name->appendChild( $dom->createTextNode( $book['name'] ) );
$bn->appendChild( $name );
$root->appendChild( $bn );
}
//output the xml in browser
header( "Content-type: text/xml" );
echo $dom->saveXML();
//or you can save the xml in file by doing the following
$dom->save("/somedirname/somefile.xml");
?>
No comments:
Post a Comment