php simplexml how to delete node

藏色散人
Release: 2023-03-06 19:24:02
Original
2103 people have browsed it

php simplexml implementation method of deleting nodes: first create a PHP sample file; then delete the year node through the "unset($book->year);" method.

php simplexml how to delete node

Recommendation: "PHP Video Tutorial"

PHP SimpleXML operates xml documents and deletes elements

sxe_delete.php:

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

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>
Copy after login

The above is the detailed content of php simplexml how to delete node. 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