PHP read and write XML and DOM details

Source: Internet
Author: User

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 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:
<Name> This test for php100 </name>

This <name> flag contains some text: Jack Herrington. XML tags that do not contain text look like this:
<PowerUp/>

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:
<PowerUp> </powerUp>

You can also add attributes to the XML tag. For example, the <name> tag contains the first and last attributes:
<Name first = "Jack" last = "Herrington"/>

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

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
<Books>
<Book>
<Author> Jack Herrington </author>
<Title> PHP Hacks </title>
<Publisher> OReilly </publisher>
</Book>
<Book>
<Author> Jack Herrington </author>
<Title> Podcasting Hacks </title>
<Publisher> OReilly </publisher>
</Book>
</Books>

XML in Listing 1 contains a list of books. The parent tag <books> contains a set of <book> tags. Each <book> tag contains <author>, <title>, and <publisher> tags. 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:
<P> This is a paragraph <br>
With a line break </p>

This line feed mark is a well-formatted XML and HTML:
<P> This is a paragraph <br/>
With a line break </p>

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.

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.