Read and write XML and DOM with PHP

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags content data data storage document echo example file files

Reading and writing Extensible Markup Language (XML) with PHP can seem a bit scary. In fact, XML and all its related technologies can be scary, but reading and writing XML in PHP is not necessarily a scary task. First, you need to learn a 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, but there are many ways to do this.

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 just defines the tags and the attributes of these tags. The well-formed XML markup looks like this:

<name> This test for php100 </ name>
This <name> tag contains some text: Jack Herrington. The XML markup that does not contain text looks like this:

<powerUp />
There's more than one way to write something in XML. For example, this tag produces the same output as the previous one:

<powerUp> </ powerUp>
You can also add attributes to XML tags. For example, this <name> tag contains the first and last attributes:

<name first = "Jack" last = "Herrington" />
Special characters can also be encoded in XML. For example, the & symbol can be encoded like this:

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

Listing 1. XML book list example

<Book> <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>

The XML in Listing 1 contains a list of books. The parent tag <books> contains a set of <book> tags, each of which contains in addition the <author>, <title>, and <publisher> tags. The XML document is correct when the markup structure and contents of the XML document are validated by external schema files. Pattern files can be specified in different formats. All you need for this article is well-formed XML.

If you think that XML looks like Hypertext Markup Language (HTML), then that's it. XML and HTML are markup languages ​​that have many similarities. However, it is important to point out that although XML documents may be well-formed HTML, not all HTML documents are well-formed XML. The line feed (br) is a good example of the difference between XML and HTML. This line feed markup is well-formed HTML, but not well-formed XML:

<p> This is a paragraph <br> With a line break </ p>
This line feed markup is well-formed XML and HTML:

<p> This is a paragraph <br /> With a line break </ p>
If you are writing HTML in the same well-formed XML, follow the W3C Committee's Extensible Hypertext Markup Language (XHTML) standard (see Resources). All modern browsers can render XHTML. Moreover, you can also use XML tools to read XHTML and find the data in the document, which is much easier than parsing HTML.

Use the DOM library to read XML

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

The books node at the top of the tree has two book subtags. In each book, there are author, publisher and title nodes. The author, publisher, and title nodes each have text subnodes containing text. The code to read the book XML file and display the content in the DOM is shown in Listing 2.

Listing 2. Reading book XML with DOM

$ books- $ doc-> getElementsByTagName ("book"); foreach ($ books as $ book) {$ authors = $ book-> getElementsByTagName ("author"); $ author = $ authors-> item (0) -> nodeValue; $ publishers = $ book-> getElementsByTagName ("publisher"); $ publisher = $ publishers-> item $ title = $ book-> getElementsByTagName ("title"); $ title = $ titles-> item (0) -> nodeValue; echo "$ title - $ author - $ publishern";}?>
The script first creates a new DOMdocument object that loads the book XML into this object using the load method. After that, the script gets a list of all the elements under the specified name using the getElementsByName method. In the book node loop, the script gets the nodeValue of author, publisher, and title tags with the getElementsByName method. nodeValue is the text in the node. The script then shows these values. You can run a PHP script like this on the command line:

% php e1.php PHP Hacks - lvtao.net for php100Podcasting Hacks - lvtao.net for php100%
As you can see, each book block outputs one line. This is a good start. However, what if I can not access the XML DOM library?

Read SAX parser XML

Another way to read XML is to use the XML Simple API (SAX) parser. Most installations of PHP include SAX parsers. The SAX parser runs on the callback model. Each time a marker is opened or closed, or each time the parser sees the text, the user-defined function is called back with node or text information.

The beauty of the SAX parser is that it is truly lightweight. The parser does not hold the content in memory for a long time, so it can be used for very large files. The disadvantage is that writing a SAX parser callback is a very troublesome thing. Listing 3 shows the code for reading a book XML file using SAX and displaying the content.

Listing 3. Reading book XML with SAX parser

<? php $ g_books = array (); $ g_elem = null; function startElement ($ parser, $ name, $ attrs) {global $ g_books, $ g_elem; if ($ name == 'BOOK' function ($ parser, $ text) {global $ g_books, $ g_elem; if ($ parser, $ name) {global $ g_elem; $ g_elem = null; $ g_elem == 'AUTHOR' || $ g_elem == 'PUBLISHER' || $ g_elem == 'TITLE') {$ g_books [count ($ g_books) - 1] [$ g_elem] = $ text;}} $ parser xml_set_element_handler ($ parser, "startElement", "endElement"); xml_set_character_data_handler ($ parser, "textData"); $ f = fopen ('books.xml', 'r' foreach ($ g_books as $ book) {echo $ book ['TITLE']. "-". $ book [$ TILE] $ { 'AUTHOR']. "-"; echo $ book ['PUBLISHER']. "N";}?>

Related Article

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.