php simplexml 如何删除节点

php simplexml删除节点的实现方法:首先创建一个PHP示例文件;然后通过“unset($book->year);”方法删除year节点即可。

推荐:《PHP视频教程》

PHP SimpleXML 操作xml文档,删除元素

sxe_delete.php:

<?php
//[需求]:删除所有书籍的year节点
$sxe = simplexml_load_file("bookstore.xml");
foreach ($sxe->book as $book) {
unset($book->year);  //删除year节点。删除变量用unset()。
}
 
$sxe->asXML('sxe_delete_book.xml');

bookstore.xml:

<?xml version="1.0" encoding="utf-8" ?>
<bookstore>
    <book category="COOKING">
        <title>Everyday Italian</title>
        <author>Giada De Laurentiis</author>
        <year>2005</year>
        <price>30.00</price>
    </book>
    <book category="CHILDREN">
        <title>Harry Potter</title>
        <author>J K. Rowling</author>
        <year>2005</year>
        <price>29.99</price>
    </book>
    <book category="WEB">
        <title>Learning XML</title>
        <author>Erik T. Ray</author>
        <year>2003</year>
        <price>39.95</price>
    </book>
</bookstore>

hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » php simplexml 如何删除节点