Home Backend Development PHP Tutorial Implementation code for reading and writing XML DOM with PHP_PHP tutorial

Implementation code for reading and writing XML DOM with PHP_PHP tutorial

Jul 21, 2016 pm 03:31 PM
dom php xml code Scalable and accomplish mark use of write language read

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="/static/imghw/default1.png" data-src="http://www.bkjia.com/uploads/allimg/131016/122GS438-0.gif" class="lazy" 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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP: Handling Databases and Server-Side Logic PHP: Handling Databases and Server-Side Logic Apr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

See all articles