Example code for modifying and deleting XML content with PHP

高洛峰
Release: 2023-03-04 16:36:01
Original
1050 people have browsed it

This article mainly introduces the method of modifying and deleting XML content in PHP. Without further ado, let’s look at the example

The schematic diagram is as follows

Example code for modifying and deleting XML content with PHP

Example code

example.xml

<?xml version="1.0" encoding="utf-8"?>
<root>
 <book id="1">
  <title>title1</title>
 </book>
 <book id="2">
  <title>title2</title>
 </book>
 <book id="3">
  <title>title3</title>
 </book>
 <book id="4">
  <title>title4</title>
 </book>
 <book id="5">
  <title>title5</title>
 </book>
</root>
Copy after login

First traverse the xml document

<?php $doc = new DOMDocument(); $doc->load(&#39;example.xml&#39;); $books = $doc -> getElementsByTagName("book"); //遍历 foreach ($books as $book) { echo $book->getAttribute(&#39;id&#39;)."-"; echo $book->getElementsByTagName("title")->item(0)->nodeValue; echo "<br>"; }
Copy after login

The running result is:

1-title1 
2-title2 
3-title3 
4-title4 
5-title5
Copy after login

Modify:

<?php
 
 $doc = new DOMDocument();
 $doc->load(&#39;example.xml&#39;);
 
 $books = $doc -> getElementsByTagName("book");
 //遍历
 foreach ($books as $book) {
  //将id=3的title设置为33333
  if($book->getAttribute(&#39;id&#39;)==3){
   echo $book->getAttribute(&#39;id&#39;)."-";
   echo $book->getElementsByTagName("title")->item(0)->nodeValue="33333";
   echo "<br>";
  }
 }
 //对文件做修改后,一定要记得重新sava一下,才能修改掉原文件
 $doc -> save(&#39;example.xml&#39;);
Copy after login

After modification:

<?xml version="1.0" encoding="utf-8"?>
<root>
 <book id="1">
  <title>title1</title>
 </book>
 <book id="2">
  <title>title2</title>
 </book>
 <book id="3">
  <title>33333</title>
 </book>
 <book id="4">
  <title>title4</title>
 </book>
 <book id="5">
  <title>title5</title>
 </book>
</root>
Copy after login

Delete operation:

<?php
 
 $doc = new DOMDocument();
 $doc->load(&#39;example.xml&#39;);
 
 $root = $doc -> documentElement;//根标签
 $books = $doc -> getElementsByTagName("book");
 //遍历
 foreach ($books as $book) {
  //将id=4的删除
  if($book->getAttribute(&#39;id&#39;)==4){
   $root->removeChild($book);
  }
 }
 //对文件做修改后,一定要记得重新sava一下,才能修改掉原文件
 $doc -> save(&#39;example.xml&#39;);
Copy after login

The result after deletion is:

<?xml version="1.0" encoding="utf-8"?>
<root>
 <book id="1">
  <title>title1</title>
 </book>
 <book id="2">
  <title>title2</title>
 </book>
 <book id="3">
  <title>33333</title>
 </book>
 
 <book id="5">
  <title>title5</title>
 </book>
</root>
Copy after login

Summary

The above is the entire content of this article. I hope the content of this article will be useful to everyone. It can bring some help to your study or work. If you have any questions, you can leave a message to communicate.

For more articles related to PHP modifying and deleting XML content example codes, please pay attention to 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!