Rumah >pembangunan bahagian belakang >masalah PHP >php怎么修改xml文件内容

php怎么修改xml文件内容

藏色散人
藏色散人asal
2021-06-03 09:24:332871semak imbas

php修改xml文件内容的方法:首先遍历一下xml文档;然后通过语句“foreach ($books as $book) {...}”修改内容;最后重新sava一下即可。

php怎么修改xml文件内容

本文操作环境:Windows7系统、PHP7.1版,DELL G3电脑

php怎么修改xml文件内容?

dcfca7026a45b9ac961791801f15ad5.png

例如:

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>

首先遍历一下该xml文档

<?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>";
    }

运行结果为:

1-title1
2-title2
3-title3
4-title4
5-title5

修改:

<?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;);

修改之后为:

<?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>

删除操作:

<?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;);

删除后的结果为:

<?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>

推荐学习:《PHP视频教程

Atas ialah kandungan terperinci php怎么修改xml文件内容. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:php post 无参数怎么设置Artikel seterusnya:deepin怎么安装php环境