PHP implements method of creating xml document based on DOM

墨辰丷
Release: 2023-03-28 06:28:02
Original
1020 people have browsed it

This article mainly introduces the method of creating xml documents based on PHP based on DOM, and analyzes the relevant operating skills of using PHP to create xml format files using DOM in the form of examples. Friends in need can refer to the following

Examples of this article Introduces PHP's method of creating xml documents based on DOM. Share it with everyone for your reference, the details are as follows:

DOM creates xml document

Use dom to create the following document:


  
    天龙八部
    金庸
    
      
    
  
Copy after login

Implementation steps:

1. Create DOM object——》2. Create node——》3. Create subordinate node——》4. Add subordinate node Add to the superior node——》5. Create an attribute node——》6. Add the attribute node to the node with the attribute——》7. If there are still nodes, repeat steps 2~6——》8. The highest-level node (i.e., the root node) is added to the DOM object——》9. Open or store the xml document.

In the process of creating a node, you can start from the lowest node or the root node. The implementation code is as follows:

createElement('booklist');//创建普通节点:booklist
$dom->appendChild($no1);//把booklist节点加入到DOM文档中
$no2 = $dom->createElement('book');//创建book节点
$no1->appendChild($no2);//把book节点加入到booklist节点中
$no3 = $dom->createAttribute('id');//创建属性节点:id
$no3->value = 1;//给属性节点赋值
$no2->appendChild($no3);//把属性节点加入到book节点中
$no3 = $dom->createElement('title');
$no2->appendChild($no3);
$no4 = $dom->createTextNode('天龙八部');//创建文本节点:天龙八部
$no3->appendChild($no4);//把天龙八部节点加入到book节点中
$no3 = $dom->createElement('author');
$no2->appendChild($no3);
$no4 = $dom->createTextNode('金庸');//创建文本节点:天龙八部
$no3->appendChild($no4);//把天龙八部节点加入到book节点中
$no3 = $dom->createElement('content');
$no2->appendChild($no3);
$no4 = $dom->createCDATASection('天龙八部是金庸写的一本武侠小说,非常好看!');//创建文CDATA节点
$no3->appendChild($no4);//把天龙八部节点加入到book节点中
header('Content-type:text/html;charset=utf-8');
echo $dom->save('booklist.xml')?'存储成功':'存储失败';//存储为xml文档
/*直接以xml文档格式打开
header('Content-type:text/xml');
echo $dom->savexml();
*/
?>
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone's learning.


Related recommendations:

Detailed explanation of how PHP uses DOM and simplexml to read xmldocument

Method of ajax traversing xmldocument

php implements interception of GBKdocument starting at a certain position n character method

The above is the detailed content of PHP implements method of creating xml document based on DOM. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!