PHP development basic tutorial PHP XML DOM

1. What is DOM?

W3C DOM provides a standard set of objects for HTML and XML documents, as well as a standard interface for accessing and manipulating these documents.
W3C DOM is divided into different parts (Core, XML and HTML) and different levels (DOM Level 1/2/3):

  • Core DOM - for any structure A set of objects that defines a standard for XML documents

  • XML DOM - A set of objects that defines a standard for XML documents

  • HTML DOM - A set of objects that defines a standard for HTML documents Object Set
    To learn more about XML DOM, visit our XML DOM tutorial.

2. XML parsing

If you need to read and update - create and process - an XML document, you Requires XML parser.

There are two basic types of XML parsers:

Tree-based parsers: This parser converts the XML document into a tree structure. It analyzes the entire document and provides access to elements in the tree, such as the Document Object Model (DOM).

Time-based parser: Treat XML documents as a series of events. When a specific event occurs, the parser calls a function to handle it.

The DOM parser is a tree-based parser.

Please look at the following XML document fragment:

 Jani

XML DOM Consider the above XML as a tree structure:

  • Level 1: XML Document

  • Level 2: Root element:

  • ##Level 3: Text element: "Jani"

3. Installation

DOM XML parser function is an integral part of the core of PHP. No installation is required to use these functions.

4. XML file

The following XML file will be used in our example:

  Tove Jani Reminder Don't forget me this weekend! 

5. Load and output XML

We need to initialize the XML parser, load XML, and output it:

Example

load("note.xml"); print $xmlDoc->saveXML(); ?>

The above code will output:

ToveJaniReminder Don't forget me this weekend!

If you view the source code in a browser window, you will see the following HTML:

  Tove Jani Reminder Don't forget me this weekend! 

The above example creates a DOMDocument-Object and loads the XML in "note.xml" into this document object.

The saveXML() function puts the internal XML document into a string so we can output it.

6. Traverse XML

We need to initialize the XML parser, load XML, and traverse all elements of the element:

Example

load("note.xml"); $x = $xmlDoc->documentElement; foreach ($x->childNodes AS $item) { print $item->nodeName . " = " . $item->nodeValue . "
"; } ?>

The above code will output:

#text =
to = Tove
#text =
from = Jani
#text =
heading = Reminder
#text =
body = Don't forget me this weekend!
#text =

In the example above, you see that there are empty text nodes between each element.

When XML is generated, it will usually contain whitespace between nodes. The XML DOM parser treats them as normal elements, which can sometimes cause problems if you don't pay attention to them.


Continuing Learning
||
Tove Jani Reminder Don't forget me this weekend!
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!