Home >Backend Development >PHP Tutorial >DOM manipulation guide in PHP
In web development, DOM (Document Object Model) is a very important concept. It allows developers to easily modify and operate the HTML or XML document of a web page, such as adding, deleting, modifying elements, etc. The built-in DOM operation library in PHP also provides developers with rich functions. This article will introduce the DOM operation guide in PHP, hoping to help everyone.
DOM is a cross-platform, language-independent API that can represent XML or HTML documents as a tree structure, in which each Nodes are all objects, and there is a parent-child relationship between objects. This tree structure allows developers to easily traverse and manipulate documents.
In DOM, each element in the web document is represented as a node. Nodes can be divided into several types such as element nodes, text nodes, comment nodes and attribute nodes. Developers can access and modify the content of nodes through the node's name, attributes, sub-nodes, etc.
In PHP, the DOM extension library provides a set of commonly used APIs that can help developers easily manipulate and modify HTML or XML document. Some of the commonly used APIs are as follows:
(1) Create a DOM document object
Before using the DOM extension library for document operations, you must first create a DOM document object. This can be achieved through the DOMDocument class:
$doc=new DOMDocument();
(2) Reading XML or HTML documents
You can use the load() or loadHTML() method of the DOMDocument class to read XML or HTML documents, for example :
$doc->load("example.xml");
(3) Traverse document nodes
You can use the methods provided by the DOMDocument class to traverse document nodes, for example:
$root=$doc->getElementsByTagName("root")->item(0); foreach($root->childNodes as $child){ //处理子节点 }
(4) Add and delete nodes
You can use the methods provided by the DOMDocument class to add and delete nodes, for example:
//添加一个元素节点 $newElement=$doc->createElement("newElement"); $root->appendChild($newElement); //删除一个元素节点 $oldElement=$doc->getElementsByTagName("oldElement")->item(0); $oldElement->parentNode->removeChild($oldElement);
(5) Find nodes
You can use the methods provided by the DOMDocument class to find nodes, for example:
//通过标签名查找元素节点 $elements=$doc->getElementsByTagName("example"); foreach($elements as $element){ //处理元素节点 } //通过属性值查找元素节点 $elements=$doc->getElementsByAttribute("id","exampleId"); foreach($elements as $element){ //处理元素节点 }
(6) Modify node content
You can use the methods provided by the DOMElement class to modify the node content, for example:
//修改元素节点的文本内容 $element=$doc->getElementsByTagName("example")->item(0); $element->textContent="new text"; //修改元素节点的属性值 $element=$doc->getElementsByTagName("example")->item(0); $element->setAttribute("attrName","attrValue");
Here, we will introduce some practical DOM operations so that developers can better understand the application of DOM operations.
(1) Replace an element node
In the following example, we will use DOM operations to replace an element node with another new element node:
$doc=new DOMDocument(); $doc->load("example.xml"); $root=$doc->getElementsByTagName("root")->item(0); $newElement=$doc->createElement("newElement"); $oldElement=$doc->getElementsByTagName("oldElement")->item(0); $root->replaceChild($newElement,$oldElement);
(2) Find an element node
In the following example, we will use DOM operations to find an element node with a specified name:
$doc=new DOMDocument(); $doc->load("example.xml"); $elements=$doc->getElementsByTagName("example"); foreach($elements as $element){ //处理元素节点 }
(3) Add an element node
In the following example, we will use DOM operations to add a new element node to the specified position:
$doc=new DOMDocument(); $doc->load("example.xml"); $root=$doc->getElementsByTagName("root")->item(0); $newElement=$doc->createElement("newElement"); $insertBefore=$doc->getElementsByTagName("insertBefore")->item(0); $root->insertBefore($newElement,$insertBefore);
This article introduces PHP DOM operation guide, including the concept of DOM, the use of DOM API and practical applications. The powerful functions of DOM can help developers easily modify and operate HTML or XML documents and improve development efficiency, which is very valuable for web developers. I hope this article can help developers who need to learn DOM operations.
The above is the detailed content of DOM manipulation guide in PHP. For more information, please follow other related articles on the PHP Chinese website!