Home  >  Article  >  Backend Development  >  Implementation code for reading and writing XML DOM with PHP_PHP tutorial

Implementation code for reading and writing XML DOM with PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:31:39708browse

Reading and writing Extensible Markup Language (XML) in PHP can seem a little scary. In fact, XML and all its related technologies can be scary, but reading and writing XML in PHP doesn't have to be a scary task. First, you need to learn a little bit about XML—what it is and what you can do with it. Then, you need to learn how to read and write XML in PHP, and there are many ways to do this.
This article provides a brief introduction to XML and then explains how to read and write XML with PHP.
What is XML?
XML is a data storage format. It does not define what data is saved, nor does it define the format of the data. XML simply defines tags and the attributes of those tags. Well-formed XML tags look like this:
Jack Herrington
This tag contains some text: Jack Herrington.
An XML tag that does not contain text looks like this:

There is more than one way to write something in XML. For example, this tag forms the same output as the previous tag:

You can also add attributes to XML tags. For example, this tag contains first and last attributes:

Special characters can also be encoded in XML. For example, the & symbol can be encoded like this:
&
An XML file containing tags and attributes is well-formed if it is formatted like the example, meaning that the tags are symmetrical and the characters are encoded correctly. Listing 1 is an example of well-formed XML.

Listing 1. XML book list example

Copy code The code is as follows:



Jack Herrington
PHP Hacks
O'Reilly


Jack Herrington
Podcasting Hacks O'Reilly



The XML in Listing 1 contains a list of books. The parent tag contains a set of tags, each of which contains , , and <publisher> tags. <br>An XML document is correct when its markup structure and content are verified by an external schema file. Schema files can be specified in different formats. For the purposes of this article, all that is needed is well-formed XML. <br>If you think XML looks a lot like Hypertext Markup Language (HTML), you're right. XML and HTML are both markup-based languages ​​and they have many similarities. However, it is important to note that while an XML document may be well-formed HTML, not all HTML documents are well-formed XML. The newline tag (br) is a good example of the difference between XML and HTML. This line break tag is well-formed HTML, but not well-formed XML: <br><p>This is a paragraph<br> <br>With a line break</p> XML and HTML: <br><p>This is a paragraph<br /> <br>With a line break</p> <br>If you want to write HTML as well-formed XML, please follow W3C committee's Extensible Hypertext Markup Language (XHTML) standard. All modern browsers can render XHTML. Furthermore, you can use XML tools to read XHTML and find the data in the document, which is much easier than parsing HTML. <br><br>Read XML using a DOM library<br> <strong>The easiest way to read a well-formed XML file is to use a Document Object Model (DOM) library compiled into some PHP installations. The DOM library reads the entire XML document into memory and represents it as a node tree, as shown in Figure 1. </strong>Figure 1. XML DOM tree for book XML <br><br><br>The books node at the top of the tree has two book child tags. In each book, there are several nodes: author, publisher and title. The author, publisher, and title nodes each have text child nodes that contain text. <img src="http://www.bkjia.com/uploads/allimg/131016/122GS438-0.gif" alt="Implementation code for reading and writing XML DOM with PHP_PHP tutorial" >The code to read the book XML file and display the content using DOM is shown in Listing 2. <br>List 2. Use DOM to read book XML <br><br><br><div class="codetitle">Copy the code<span style="max-width:90%" onclick="doCopy('code87375')"><u> The code is as follows:</u><div class="codebody" id="code87375"> <br><?php <BR>$doc = new DOMDocument(); <BR>$doc->load( 'books.xml' ); <br>$books = $doc->getElementsByTagName ( "book" ); <br>foreach( $books as $book ) <br>{ <br>$authors = $book->getElementsByTagName( "author" ); <br>$author = $authors-> item(0)->nodeValue; <br>$publishers = $book->getElementsByTagName( "publisher" ); <br>$publisher = $publishers->item(0)->nodeValue; <br> $titles = $book->getElementsByTagName( "title" ); <br>$title = $titles->item(0)->nodeValue; <br>echo "$title - $author - $publishern"; <br>} <br>?> <br> </div> <br>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. <br>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. <br>You can run a PHP script on the command line like this: <br>% php e1.php <br>PHP Hacks - Jack Herrington - O'Reilly <br>Podcasting Hacks - Jack Herrington - O'Reilly <br>% <br>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? <br>Reading XML with a SAX parser <br>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. <br>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. <br>Listing 3. Reading book XML with SAX parser <br><div class="codetitle"> <span style="CURSOR: pointer" onclick="doCopy('code65349')"><u>Copy code </u></span> The code is as follows: </div> <div class="codebody" id="code65349"> <br><?php <BR>$g_books = array(); <BR>$g_elem = null; <BR>function startElement( $parser, $name, $attrs ) <BR>{ <BR>global $g_books, $g_elem; <BR> if ( $name == 'BOOK' ) $g_books []= array(); <BR>$g_elem = $name; <BR>} <BR>function endElement( $parser, $name ) <BR>{ <BR>global $g_elem; <BR>$g_elem = null; <BR>} <BR>function textData( $parser, $text ) <BR>{ <BR>global $g_books, $g_elem; <BR>if ( $g_elem == 'AUTHOR' || <BR>$g_elem == 'PUBLISHER' || <BR>$g_elem == 'TITLE' ) <BR>{ <BR>$g_books[ count( $g_books ) - 1 ][ $ g_elem ] = $text; <BR>} <BR>} <BR>$parser = xml_parser_create(); <BR>xml_set_element_handler( $parser, "startElement", "endElement" ); <BR>xml_set_character_data_handler( $parser, " textData" ); <BR>$f = fopen( 'books.xml', 'r' ); <BR>while( $data = fread( $f, 4096 ) ) <BR>{ <BR>xml_parse( $parser , $data ); <BR>} <BR>xml_parser_free( $parser ); <BR>foreach( $g_books as $book ) <BR>{ <BR>echo $book['TITLE']." - ".$ book['AUTHOR']." - "; <BR>echo $book['PUBLISHER']."n"; <BR>} <BR>?> <br> </div> <br>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. <br>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. <br>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. <br>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? <br>-------------------------------------------------- ---------------------------------- <br>Back to top<br>Parsing XML with regular expressions <br> 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. <br>Listing 4. Reading XML with regular expressions <br><div class="codetitle"> <span style="CURSOR: pointer" onclick="doCopy('code4870')"><u>Copy the code</u></span> The code is as follows:</div> <div class="codebody" id="code4870"> <br><?php <br>$xml = ""; <br>$f = fopen( 'books.xml', 'r' ); <br>while( $data = fread( $f , 4096 ) ) { $xml .= $data; } <br>fclose( $f ); <br>preg_match_all( "/<book>(.*?)</book>/s", <br> $xml, $bookblocks ); <br>foreach( $bookblocks[1] as $block ) <br>{ <br>preg_match_all( "/<author>(.*?)</author>/", <br>$block, $author ); <br>preg_match_all( "/<title>(.*?)/",
$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> <br><publisher>O'Reilly</publisher> <br></book> <br><book> <br><author>Jack Herrington</author> <br><title> ;Podcasting Hacks
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 )
{
?>

<?php echo( $book['title'] ); ?>





}
?>



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 ); ?>




}
?>


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.

www.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...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:String manipulation functions commonly used in PHP development_PHP tutorialNext article:String manipulation functions commonly used in PHP development_PHP tutorial

Related articles

See more