Reading and writing XML DOM in PHP

Source: Internet
Author: User
Tags define command line contains object model
Dom|xml There are many techniques that can be used to read and write XML in PHP. This article provides three ways to read XML: Using a DOM library, using a SAX parser, and using regular expressions. It also describes using DOM and PHP text templates to write XML.

Reading and writing Extensible Markup Language (XML) in PHP may seem a bit scary. In fact, XML and all of its related technologies may be scary, but reading and writing XML in PHP is not necessarily a scary task. First, you need to learn a little bit about XML-what it is and what it does with it. Then you need to learn how to read and write XML in PHP, and there are a number of ways to do it.

This article provides a brief introduction to XML and then explains how to read and write XML in 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 the tags and the attributes of those tags. Well-Formed XML tags look like this:

<name> Jack Herrington </name>

This <name> tag contains some text: Jack Herrington.

XML tags that do not contain text look like this:

<powerup/>

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

<powerUp> </powerUp>

You can also add attributes to an XML tag. For example, this <name> tag contains the properties of the both the A and the last:

<name first= "Jack" last= "Herrington"

You can also encode special characters in XML. For example, the,& symbol can be encoded like this:

&

An XML file that contains tags and attributes, if formatted like an example, is well-formed, which means that the tags are symmetric and the characters are encoded correctly. Listing 1 is an example of well-formed XML.

Listing 1. An example of XML book column representation

   
   
    
    
      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 <books> contains a set of <book> tags, and each <book> tag contains <author>, <title>, and <publisher> tags.

XML documents are correct when the markup structure and content of XML documents are validated by external schema files. The schema file can be specified in a different format. For this article, all you need is well-formed XML.

If you think that XML looks like Hypertext Markup Language (HTML), that's right. Both XML and HTML are based on markup languages, and they have many similarities. However, it is emphasized that although the XML document may be well-formed HTML, not all HTML documents are well-formed XML. A newline marker (BR) is a good example of the difference between XML and HTML. This newline mark is well-formed HTML, but not well-formed XML:

<p> This is a paragraph <br>
With a line break </p>

This line break tag is well-formed XML and HTML:

<p> This is a PARAGRAPH<BR/>
With a line break </p>

If you want to write HTML as well-formed XML, follow the Extensible Hypertext Markup Language (XHTML) standard for the consortium (see Resources). All modern browsers are capable of rendering XHTML. Also, you can read XHTML with XML tools and find the data in the document, which is much easier than parsing HTML.

   reading XML using the DOM library

The easiest way to read well-formed XML files is to use a Document Object Model (DOM) library that is compiled into certain PHP installations. The DOM library reads the entire XML document into memory and represents it in the node tree, as shown in Figure 1.

Fig. 1. xml DOM tree for book XML


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. Author, publisher, and title nodes have text child nodes that contain text, respectively.

The code that reads the book XML file and displays the content in the DOM is shown in Listing 2.

Listing 2. Reading book XML with DOM

   
  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";   }   ? >   


The script first creates a new DOMDocument object and loads the book XML into the object using the Load method. The script then uses the Getelementsbyname method to get a list of all the elements under the specified name.

In the book node loop, the script obtains the nodevalue of author, publisher, and title tags using the Getelementsbyname method. NodeValue is the text in the node. The script then displays these values.

You can run PHP scripts like this on the command line:

% PHP e1.php
PHP hacks-jack Herrington-o ' Reilly
Podcasting Hacks-jack Herrington-o ' Reilly
%

You can see that each book block outputs one row. This is a good start. But what if you don't have access to the XML DOM library?

[1] [2] [3] Next page



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.