The code is as follows:
$doc = new DOMDocument();
$doc->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 - $publishern";
}
?>
The script first creates a new DOMdocument object and loads the book XML into this object using the load method. Afterwards, the script uses the getElementsByName method to get a list of all elements under the specified name.
In the loop of the book node, the script uses the getElementsByName method to obtain the nodeValue of the author, publisher and title tags. nodeValue is the text in the node. The script then displays these values.
You can run a PHP script on the command line like this:
% php e1.php
PHP Hacks - Jack Herrington - O'Reilly
Podcasting Hacks - Jack Herrington - O'Reilly
%
As you can see, each book block outputs one line. This is a good start. But what if you don't have access to the XML DOM library?
Reading XML with a SAX parser
Another way to read XML is to use an XML Simple API (SAX) parser. Most installations of PHP include a SAX parser. The SAX parser runs on a callback model. Each time a tag is opened or closed, or each time the parser sees text, the user-defined function is called back with information about the node or text.
The advantage of the SAX parser is that it is truly lightweight. The parser does not keep content in memory for long periods of time, so it can be used for very large files. The disadvantage is that writing SAX parser callbacks is a pain in the ass. Listing 3 shows code that uses SAX to read a book XML file and display the contents.
Listing 3. Reading book XML with SAX parser
Copy code The code is as follows:
$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( 'books.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";
}
?>
The script first sets up g_books An array that holds all books and book information in memory, and the g_elem variable holds the name of the tag currently being processed by the script. The script then defines the callback function. In this example, the callback functions are startElement, endElement, and textData. When opening and closing the markup, call the startElement and endElement functions respectively. Call textData on the text between the opening and closing tags.
In this example, the startElement tag looks for the book tag to start a new element in the book array. The textData function then looks at the current element to see if it is a publisher, title, or author tag. If so, the function puts the current text into the current book.
To allow parsing to continue, the script creates a parser using the xml_parser_create function. Then, set the callback handle. Afterwards, the script reads the file and sends chunks of the file to the parser. After the file is read, the xml_parser_free function removes the parser. The end of the script prints the contents of the g_books array.
As you can see, this is much more difficult than writing the same functionality for the DOM. What if there is no DOM library and no SAX library? Are there any alternatives?
-------------------------------------------------- ----------------------------------
Back to top
Parsing XML with regular expressions
To be sure, some engineers will criticize me even for mentioning this method, but it is indeed possible to parse XML with regular expressions. Listing 4 shows an example of using the preg_ function to read a book file.
Listing 4. Reading XML with regular expressions
Copy the code The code is as follows:
$xml = "";
$f = fopen( 'books.xml', 'r' );
while( $data = fread( $f , 4096 ) ) { $xml .= $data; }
fclose( $f );
preg_match_all( "/
(.*?)/s",
$xml, $bookblocks );
foreach( $bookblocks[1] as $block )
{
preg_match_all( "/
(.*?)/",
$block, $author );
preg_match_all( "/
(.*?)/",
$block, $title );
preg_match_all( "/
(.*?)/",
$block, $publisher );
echo( $title[1][0]." - ".$author[1] [0]." - ".
$publisher[1][0]."n" );
}
?>
Please note this How short is the code. Initially, it reads the file into a large string. Then use a regex function to read each book item. Finally, use a foreach loop to loop through each book block and extract the author, title, and publisher.
So, where are the flaws? The problem with using regular expression code to read XML is that it doesn't first check to make sure the XML is well-formed. This means that there is no way to know whether the XML is well-formed before reading it. Also, some well-formed XML may not match the regular expression, so they must be modified later.
I never recommend using regular expressions to read XML, but sometimes it is the most compatible way because the regular expression functions are always available. Do not use regular expressions to read XML directly from the user because you have no control over the format or structure of such XML. You should always use a DOM library or a SAX parser to read XML from the user.
-------------------------------------------------- ----------------------------------
Back to top
Writing XML using DOM
Read Fetching the XML is only part of the equation. How to write XML? The best way to write XML is with the DOM. Listing 5 shows how the DOM builds the book XML file.
Listing 5. Writing book XML using DOM
Copy the code The code is as follows:
$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 example books. This data can come from the user or from 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 a text node for each node. The final step for each book node is to re-add it to the root books node.
At the end of the script, use the saveXML method to output XML to the console. (You can also use the save method to create an XML file.) The output of the script is shown in Listing 6.
Listing 6. Output of DOM build script
Copy the code The code is as follows:
php e4.php
Jack Herrington
PHP Hacks title>
O'Reilly
Jack Herrington
O'Reilly
The real value of using DOM is that the XML it creates is always well-formed. But what if you can't create XML with the DOM?
-------------------------------------------------- ----------------------------------
Back to top
Writing XML with PHP
If DOM is not available, XML can be written using PHP's text template. Listing 7 shows how PHP builds the book XML file.
Listing 7. Writing book XML in PHP
Copy the code The code is as follows:
$books = array();
$books [] = array(
'title' => 'PHP Hacks',
'author' => 'Jack Herrington',
'publisher ' => "O'Reilly"
);
$books [] = array(
'title' => 'Podcasting Hacks',
'author' => 'Jack Herrington ',
'publisher' => "O'Reilly"
);
?>
foreach( $books as $ book )
{
?>
}
?>
The top part of the script is similar to the DOM script. The bottom of the script opens the books tag and then iterates through each book, creating the book tag and all the inner title, author, and publisher tags.
The problem with this approach is encoding the entities. To ensure that entities are encoded correctly, the htmlentities function must be called on each item, as shown in Listing 8.
Listing 8. Encoding entities using the htmlentities function
Copy the code The code is as follows:
foreach( $books as $book )
{
$title = htmlentities( $book['title'], ENT_QUOTES );
$author = htmlentities( $book[ 'author'], ENT_QUOTES );
$publisher = htmlentities( $book['publisher'], ENT_QUOTES );
?>
php echo( $title ); ?>
php echo( $publisher ); ?>
}
?>
This is the annoying thing about writing XML in basic PHP. You think you've created perfect XML, but as soon as you try to use the data, you discover that some elements are encoded incorrectly.
-------------------------------------------------- ----------------------------------
Conclusion
There is a lot of exaggeration and confusion surrounding XML place. However, it’s not as difficult as you think – especially in a language as great as PHP. Once you understand and implement XML correctly, you'll find many powerful tools at your disposal. XPath and XSLT are two such tools worth investigating.
http://www.bkjia.com/PHPjc/322974.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322974.htmlTechArticleReading and writing Extensible Markup Language (XML) with PHP may seem a little scary. In fact, XML and all its related technologies can be scary, but reading and writing XML in PHP doesn't...