Reading and Writing XML Documents with PHP (Using DOM)
Todays entry will be on reading and writing XML using the Document Object Model (DOM) (located in the PHP libraries), since it is the easiest way to read/write a well-formed XML file. The DOM library reads the entire XML document into memory and represents it as a tree of nodes.
If you are interested in learning more about XML, check out this post:
A Short Introduction to the Basics of XML
Sample XML Document
Joe Smith
O'Reilly
Jack Herrington
O'Reilly
Reading XML documents:
load( 'books.xml' );
$books = $doc->getElementsByTagName( "book" );
foreach( $books as $book ) {
$authors = $book->getElementsByTagName( "author" );
$author = $authors->item(0)->nodeValue;
$publishers = $book->getElementsByTagName( "publisher" );
$publisher = $publishers->item(0)->nodeValue;
$titles = $book->getElementsByTagName( "title" );
$title = $titles->item(0)->nodeValue;
echo "$title - $author - $publisher\n";
}
?>
Output:
PHP is Cool - Joe Smith - O'Reilly
Podcasting Hacks - Jack Herrington - O'Reilly
Create Dom Object
$doc = new DOMDocument();
Load xml document into memory
$doc->load( 'books.xml' );
Determine outer tag (i.e.
$books = $doc->getElementsByTagName( "book" );
Looping through all sections of XML document and printing results
foreach( $books as $book ) {
$authors = $book->getElementsByTagName( "author" );
$author = $authors->item(0)->nodeValue;
$publishers = $book->getElementsByTagName( "publisher" );
$publisher = $publishers->item(0)->nodeValue;
$titles = $book->getElementsByTagName( "title" );
$title = $titles->item(0)->nodeValue;
echo "$title - $author - $publisher\n";
}
i.e. Each section looks like this:
Joe Smith
O'Reilly
Not so bad right? You can easily change this code to fit your specific needs.
Writing XML documents
The best way to write XML is to use the DOM, which is demonstrated below.
'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();
?>
Array which contains data that will be placed in XML document
$books = array();
$books [] = array(
'title' => 'PHP Hacks',
'author' => 'Jack Herrington',
'publisher' => "O'Reilly"
);
$books [] = array(
'title' => 'Podcasting Hacks',
'author' => 'Jack Herrington',
'publisher' => "O'Reilly"
);
Create Dom object and make sure to turn on output formatting. Then create first element (i.e.
$doc = new DOMDocument();
$doc->formatOutput = true;
$r = $doc->createElement( "books" );
Loop through original array and create elements for XML document and then append the data from the array to it.
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 );
}
Take final output, which currently resides in memory at this point and print to screen. You could easily just output it to a .xml file by using file_put_contents() function from php library.
echo $doc->saveXML();
Output of Script
Jack Herrington
O'Reilly
Jack Herrington
O'Reilly
Much of this content (well actually most) is from a post by Jack Herrington (IBM Senior Software Engineer). I would like to share the post with everyone, but the page is no longer available. The article that he wrote was a lot longer, but these are his exact examples, which are excellent for starting out with XML. I really wish I could have shared that post with you guys.
If anyone has any questions or find a problem in my post, I would love to hear about it. Just drop me an email at:
jays@techexperiment.com