jhylands

Write to XML

Jan 15th, 2012
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.87 KB | None | 0 0
  1. <?php
  2. $books = array();
  3. $books [] = array(
  4. 'title' => 'PHP Hacks',
  5. 'author' => 'Jack Herrington',
  6. 'publisher' => "O'Reilly"
  7. );
  8. $books [] = array(
  9. 'title' => 'Podcasting Hacks',
  10. 'author' => 'Jack Herrington',
  11. 'publisher' => "O'Reilly"
  12. );
  13.  
  14. $doc = new DOMDocument();
  15. $doc->formatOutput = true;
  16.  
  17. $r = $doc->createElement( "books" );
  18. $doc->appendChild( $r );
  19.  
  20. foreach( $books as $book )
  21. {
  22. $b = $doc->createElement( "book" );
  23.  
  24. $author = $doc->createElement( "author" );
  25. $author->appendChild(
  26. $doc->createTextNode( $book['author'] )
  27. );
  28. $b->appendChild( $author );
  29.  
  30. $title = $doc->createElement( "title" );
  31. $title->appendChild(
  32. $doc->createTextNode( $book['title'] )
  33. );
  34. $b->appendChild( $title );
  35.  
  36. $publisher = $doc->createElement( "publisher" );
  37. $publisher->appendChild(
  38. $doc->createTextNode( $book['publisher'] )
  39. );
  40. $b->appendChild( $publisher );
  41.  
  42. $r->appendChild( $b );
  43. }
  44.  
  45. echo $doc->saveXML();
  46. ?>
Advertisement
Add Comment
Please, Sign In to add comment