在 PHP 中更改 XML 节点元素值并保存文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2956601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:14:26  来源:igfitidea点击:

Change XML node element value in PHP and save file

phpxmldom

提问by Hannes

<testimonials>
    <testimonial id="4c050652f0c3e">
        <nimi>John</nimi>
        <email>[email protected]</email>
        <text>Some text</text>
        <active>1</active>
        </testimonial>
    <testimonial id="4c05085e1cd4f">
        <name>ats</name>
        <email>[email protected]</email>
        <text>Great site!</text>
        <active>0</akctive>
    </testimonial>
</testimonials>

I have this XML strcuture and i need to find a testimonial with specific id and changeits valueand save file. I have a PHP script deleting specific testimonial according its ID:

我有这个 XML 结构,我需要找到具有特定 ID 的推荐书并更改并保存文件。我有一个 PHP 脚本根据其 ID 删除特定的推荐:

<?php
$xmlFile = file_get_contents('test.xml');
$xml = new SimpleXMLElement($xmlFile);

$kust_id = $_GET["id"];

foreach($xml->testimonial as $story) {
    if($story['id'] == $kust_id) {
        $dom=dom_import_simplexml($story);
        $dom->parentNode->removeChild($dom);

        $xml->asXML('test.xml');
        header("Location: newfile.php");
    }
}
?>

回答by VolkerK

You can use XPathto find the specific element. SimpleXMLElement->xpath()returns an array of (matching) SimpleXMLElement objects, i.e. you can access and change the data of each element just like you would in "your" foreach loop.

您可以使用XPath来查找特定元素。SimpleXMLElement->xpath()返回一个(匹配的)SimpleXMLElement 对象数组,也就是说,您可以像在“您的”foreach 循环中一样访问和更改每个元素的数据。

<?php
// $testimonials = simplexml_load_file('test.xml');
$testimonials = new SimpleXMLElement('<testimonials>
    <testimonial id="4c050652f0c3e">
        <nimi>John</nimi>
        <email>[email protected]</email>
        <text>Some text</text>
        <active>1</active>
        </testimonial>
    <testimonial id="4c05085e1cd4f">
        <name>ats</name>
        <email>[email protected]</email>
        <text>Great site!</text>
        <active>0</active>
    </testimonial>
</testimonials>');

// there can be only one item with a specific id, but foreach doesn't hurt here
foreach( $testimonials->xpath("testimonial[@id='4c05085e1cd4f']") as $t ) {
  $t->name = 'LALALA';
}

echo $testimonials->asXML();
// $testimonials->asXML('test.xml');

prints

印刷

<?xml version="1.0"?>
<testimonials>
    <testimonial id="4c050652f0c3e">
        <nimi>John</nimi>
        <email>[email protected]</email>
        <text>Some text</text>
        <active>1</active>
        </testimonial>
    <testimonial id="4c05085e1cd4f">
        <name>LALALA</name>
        <email>[email protected]</email>
        <text>Great site!</text>
        <active>0</active>
    </testimonial>
</testimonials>