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:
- 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:
- &
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
-
-
- Jack Herrington
- PHP Hacks
- OReilly
-
-
- Jack Herrington
- Podcasting Hacks
- 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:
This is a paragraph
- With a line break
This line feed mark is a well-formatted XML and HTML:
This is a paragraph
- 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...