Reading and writing XML DOM in PHP

Source: Internet
Author: User
Tags foreach

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:

Jack Herrington.

This tag contains some text: Jack Herrington.

XML tags that do not contain text look like this:

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

You can also add attributes to an XML tag. For example, this tag contains the properties:

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

&

XML files that contain tags and attributes, if formatted like the example, are well-formed, meaning 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.

O ' Reilly

Jack Herrington.

O ' Reilly

The XML in Listing 1 contains a list of books. The parent tag contains a set of tags, each of which contains,

O ' Reilly

Jack Herrington.

O ' Reilly

%

The real value of using the DOM is that the XML it creates is always well-formed. But what if you can't create XML with the DOM?

Writing XML in PHP

If the DOM is not available, you can use PHP's text template to write XML. Listing 7 shows how PHP constructs the book XML file.

Listing 7. Writing book XML in PHP

$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 of the script is similar to the DOM script. At the bottom of the script, open the books tag, and then iterate through each book to create the tag and all the internal title, author, and publisher tags.

The problem with this approach is to encode the entities. To ensure that the entity encoding is correct, you must call the Htmlentities function on each project, as shown in Listing 8.

Listing 8. To encode an entity using the Htmlentities function

foreach ($books as $book)

{

$title = htmlentities ($book [' title '], ent_quotes);

$author = htmlentities ($book [' Author '], ent_quotes);

$publisher = htmlentities ($book [' publisher '], ent_quotes);

?>

}

?>

This is the annoying thing about writing XML with basic PHP. You thought you created the perfect XML, but when you tried to use the data, you immediately found that some elements were incorrectly encoded.

Conclusion

There's always a lot of exaggeration and confusion around XML. But it's not as hard as you think-especially in a good language like PHP. After understanding and implementing XML correctly, there are many powerful tools that you can use. XPath and XSLT are the two tools worth researching.

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.