如何使用Dom php更改xml中的特定节点值


how to change specific node value in xml using Dom php

嗨,我想把成本的值改为$6.0,其中杯子蛋糕的颜色是红色。这是我的两个纸杯蛋糕样本,尽管我的XML文件中有很多纸杯蛋糕,所以我想首先找到颜色为红色的纸杯蛋糕,然后将相应的纸杯蛋糕价格更改为我喜欢的任何价格。

<cupcake>
    <name> Cookies and Cream</name>
    <flavours>
        <ingredient>Chocolate Cake</ingredient>
        <ingredient>Salted Caramel Buttercream</ingredient>
        <ingredient>Buttercream</ingredient>
    </flavours>
    <colors>
        <color>Red</color>
    </colors>
    <energy>1900.6Cal</energy>
    <cost>$22.50</cost>
</cupcake>
<cupcake>
    <name> Killer Carrot</name>
    <flavours>
        <ingredient>Carrot Spice cake</ingredient>
        <ingredient>Cream Cheese Frosting</ingredient>
        <ingredient>Candied Carrots</ingredient>
        <ingredient>Chocolate</ingredient>
    </flavours>
    <colors>
        <color>Aqua</color>
    </colors>
    <energy>1500.0KJ</energy>
    <cost>$15.80</cost>
</cupcake>

和我的PHP文件

<?php
$xml = new DOMDocument();  
$xml->load('cupcakes.xml');  
if ($xml->schemaValidate('cupcakes.xsd')==FALSE)     
die ('<div class="error">Validation failed</div>'); 
$xsl = new DOMDocument(); 
$xsl->load('cupcakes.xsl'); 
$proc = new XSLTProcessor(); 
$proc->importStyleSheet($xsl); // attach the xsl rules  
echo $proc->transformToXML($xml);
echo "<hr/>";
echo "<h2> the first cupcake having color red has changed the cost value to $6.0"; 
$a = $xml->getElementsByTagName('color');
foreach ($a->nodeValue as $A){
if ($A = "Red")
$a->getElementsByTagName('cost')->nodeValue="$6.00";
}
echo $proc->transformToXML($xml); 
?>

您的XML缺少一个文档元素。这不是一个有效的XML文件。

DOMNode::getElementsByTagName()返回一个节点列表,而不是单个节点。$nodeValueDOMNode的性质,不是DOMNodeList的性质。只是检查颜色值,不会这样做。一个纸杯蛋糕可以有几种颜色。如果使用XPath,可以有这样的条件:

$document = new DOMDocument();
$document->load($xmlFile);
$xpath = new DOMXpath($document);
foreach ($xpath->evaluate('//cupcake[colors/color = "Red"]/cost') as $cost) {
  $cost->nodeValue = '';
  $cost->appendChild($document->createTextNode('$6.00'));
}
echo $document->saveXml();
输出:

<?xml version="1.0"?>
<cupcakes>
<cupcake>
    <name> Cookies and Cream</name>
    <flavours>
        <ingredient>Chocolate Cake</ingredient>
        <ingredient>Salted Caramel Buttercream</ingredient>
        <ingredient>Buttercream</ingredient>
    </flavours>
    <colors>
        <color>Red</color>
    </colors>
    <energy>1900.6Cal</energy>
    <cost>$6.00</cost>
</cupcake>
<cupcake>
    <name> Killer Carrot</name>
    <flavours>
        <ingredient>Carrot Spice cake</ingredient>
        <ingredient>Cream Cheese Frosting</ingredient>
        <ingredient>Candied Carrots</ingredient>
        <ingredient>Chocolate</ingredient>
    </flavours>
    <colors>
        <color>Aqua</color>
    </colors>
    <energy>1500.0KJ</energy>
    <cost>$15.80</cost>
</cupcake>
</cupcakes>

XPath允许获取节点和标量值。在这种情况下:

取所有的cupcake节点…
//cupcake

…color node = "Red"…
//cupcake[colors/color = "Red"]

…并得到它的cost子节点:
//cupcake[colors/color = "Red"]/cost

告诉我们,你的代码出了什么问题,如果有错误消息是什么…

和我建议你更好地尝试simpleXML的使用,因为它的语法更容易(对我来说)。这里有一个链接,你可以看看http://php.net/manual/en/simplexml.examples-basic.php

您在示例中使用的语法类似于js。不过你可以用php中更简单的