Home  >  Article  >  Backend Development  >  Related knowledge points about PHP XML DOM

Related knowledge points about PHP XML DOM

jacklove
jackloveOriginal
2018-05-07 14:44:251398browse

The built-in DOM parser makes it possible to process XML documents in PHP, which will be explained in this article.

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 structured document Defining a standard set of objects
* XML DOM - Defining a standard set of objects for XML documents
* HTML DOM - Defining a standard set of objects for HTML documents

To learn more about XML DOM knowledge, visit our XML DOM tutorial.

XML Parsing

To read and update - create and process - an XML document, you need an 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:

<?xml version="1.0" encoding="ISO-8859-1"?>
<from>Jani</from>

XML DOM Consider the above XML as a tree structure:

Level 1: XML document

Level 2: Root element: 66fd2ada9ebb04d4250c850dc1e3737e

Level 3: Text element: "Jani"

Installation

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

XML file

The following XML file will be used in our example:

<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don&#39;t forget me this weekend!</body>
</note>


Loading and outputting XML

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

Example

<?php
$xmlDoc = new DOMDocument();
$xmlDoc->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:

<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don&#39;t forget me this weekend!</body>
</note>

The above example creates a DOMDocument-Object and puts "note.xml" in The XML is loaded into this document object. The

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

Traverse XML

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

Example

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

Above The 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.

This article explains in detail the processing of XML documents in the DOM parser PHP. For more learning materials, please pay attention to the PHP Chinese website.

Related recommendations:

About the basics of PHP XML Expat parser

Related content about PHP database ODBC

Knowledge related to PHP MySQL Delete statement

The above is the detailed content of Related knowledge points about PHP XML DOM. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn