PHP implementation to read and write XML DOM code

  • 2020-03-31 20:32:05
  • OfStack

 
//Read the XML with the DOM
$doc = new DOMDocument(); 
$doc->load( ' test.xml'); 
$books = $doc->getElementsByTagName( " book " ); 
foreach( $books as $book ){ 
$authors = $book->getElementsByTagName( " author " ); 
$author = $authors->item(0)->nodeValue; //A nodeValue property can set or return the value of a node based on the type of node.
$publishers = $book->getElementsByTagName( " publisher " ); 
$publisher = $publishers->item(0)->nodeValue; 
$titles = $book->getElementsByTagName(  " title "  ); 
$title = $titles->item(0)->nodeValue; 
echo  " Title: $title <br> Author: $author <br> Publisher: $publisher<br><hr><br> " ; 
} 


 
//Read the XML with a SAX parser
$g_books = array(); 
$g_elem = null; 
function startElement( $parser, $name, $attrs ){ 
global $g_books, $g_elem; 
if ( $name == 'BOOK' ) $g_books []= array(); 
$g_elem = $name; 
} 
function endElement( $parser, $name ){ 
global $g_elem; 
$g_elem = null; 
} 
function textData( $parser, $text ){ 
global $g_books, $g_elem; 
if ( $g_elem == 'AUTHOR' || $g_elem == 'PUBLISHER' || $g_elem == 'TITLE' ){ 
$g_books[ count( $g_books ) - 1 ][ $g_elem ] = $text; 
} 
} 
$parser = xml_parser_create(); 
xml_set_element_handler( $parser,  " startElement " ,  " endElement "  ); 
xml_set_character_data_handler( $parser,  " textData "  ); 
$f = fopen( 'test.xml', 'r' ); 
while( $data = fread( $f, 4096 ) ){ 
xml_parse( $parser, $data ); 
} 
xml_parser_free( $parser ); 
foreach( $g_books as $book ){ 
echo $book['TITLE']. "  -  " .$book['AUTHOR']. "  -  " ; 
echo $book['PUBLISHER']. " n " ; 
} 


// parsing XML with regular expressions
 
$xml =  " "; 
$f = fopen( 'test.xml', 'r' ); 
while( $data = fread( $f, 4096 ) ) { $xml .= $data; } 
fclose( $f ); 
preg_match_all(  " /<book>(.*?)</book>/s " , $xml, $bookblocks ); 
foreach( $bookblocks[1] as $block ){ 
preg_match_all(  " /<author>(.*?)</author>/ " , $block, $author ); 
preg_match_all(  " /<title>(.*?)</title>/ " , $block, $title ); 
preg_match_all(  " /<publisher>(.*?)</publisher>/ " , $block, $publisher ); 
echo( $title[1][0]. "  -  " .$author[1][0]. "  -  " . $publisher[1][0]. " n "  ); 
} 


// write XML in the DOM
 
$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'] ) ); 
$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(); 

/ *
At the top of the script, the books array is loaded with some sample books. This data can come from either the user or the database.
After the sample book is loaded, the script creates a new DOMDocument and adds the root node books to it. The script then creates nodes for each book's author, title, and publisher, and adds text nodes for each node. The final step for each book node is to add it back to the root node point books.
The real value of using the DOM is that the XML it creates is always well-formed. But what if you can't create XML with the DOM?
Xml code
 
<?php 
PHP  write xml 
$books = array(); 
$books [] = array( 
'title' => 'PHP Hacks', 
'author' => 'Jack Herrington', 
'publisher' =>  " O'Reilly "  
); 
$books [] = array( 
'title' => 'Podcasting Hacks', 
'author' => 'Jack Herrington', 
'publisher' =>  " O'Reilly "  
); 
?> 
<books> 
<?php 
foreach( $books as $book ) 
{ 
?> 
<book> 
<title><?php echo( $book['title'] ); ?></title> 
<author><?php echo( $book['author'] ); ?> 
</author> 
<publisher><?php echo( $book['publisher'] ); ?> 
</publisher> 
</book> 
<?php 
} 
?> 
</books> 

The test.xml used in the example is as follows:
 
<?xml version= " 1.0 "  encoding= " utf8 " ?> 
<books> 
<book> 
<author>Jack Herrington</author> 
<title>PHP Hacks</title> 
<publisher>O'Reilly</publisher> 
</book> 
<book> 
<author>Jack Herrington</author> 
<title>Podcasting Hacks</title> 
<publisher>O'Reilly</publisher> 
</book> 
</books> 

Related articles: