Use PHP to read and write XML and DOM_PHP tutorials

Source: Internet
Author: User
Use PHP to read and write XML and DOM. Reading and Writing Extensible Markup Language (XML) in PHP may seem a little scary. In fact, XML and all its related technologies may be terrible, but reading and writing XML with PHP may not necessarily be reading and writing Extensible Markup Language (XML) using PHP) it may seem a little scary. In fact, XML and all its related technologies may be terrible, but reading and writing XML with PHP is not necessarily a terrible task. First, you need to learn a little about XML: what it is and what it is used. Then, you need to learn how to read and write XML in PHP, and there are many ways to do this.

What is XML?

XML is a data storage format. It does not define what data to save or define the data format. XML only defines the attributes of tags and these tags. Well-formatted XML markup looks like this:

 
 
  1. This test for php100

This Mark contains some text: Jack Herrington. XML tags that do not contain text look like this:

 
 

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

 
 

You can also add attributes to the XML tag. For example Tag contains first and last attributes:

 
 

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

 
 
  1. &

If the XML file containing tags and attributes is formatted as in the example, the format is good, which means that the tags are symmetric and the characters are correctly encoded. Listing 1 is an example of a well-formatted XML.

Listing 1. XML library list example

 
 
  1. Jack Herrington
  2. PHP Hacks
  3. OReilly
  4. Jack Herrington
  5. Podcasting Hacks
  6. OReilly

XML in listing 1 contains a list of books. Parent tag Contains a group Tag, each Mark and include, And Mark. After the XML document's tag structure and content are verified by the external mode file, the XML document is correct. Mode files can be specified in different formats. For this article, all we need is a well-formatted XML.

If XML looks like HTML, that's right. XML and HTML are both tag-based languages with many similarities. However, it is important to note that although XML documents may be well-formed HTML documents, not all HTML documents are well-formed XML documents. Line Feed mark (br) is a good example of the difference between XML and HTML. This line feed mark is HTML in good format, but not XML in good format:

 
 
  1. This is a paragraph

  2. With a line break

This line feed mark is a well-formatted XML and HTML:

 
 
  1. This is a paragraph

  2. With a line break

To write HTML into XML in the same format, follow the W3C Committee's Extensible HyperText Markup Language (XHTML) standard (see references ). All modern browsers can render XHTML. In addition, you can use XML tools to read XHTML and find the data in the document, which is much easier than parsing HTML.

XML may seem a little scary. In fact, XML and all its related technologies may be terrible, but reading and writing XML with PHP is not necessarily...

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.