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

时间:2015-06-03 03:31:38

标签: php xml dom

您好我想将蛋糕价值更改为6.0美元,其中蛋糕颜色为红色。我怎么能实现这一点......我的两个样品蛋糕虽然我的xml文件中有很多蛋糕...所以我想首先找到颜色为红色的蛋糕,然后将相应的蛋糕价格改为我喜欢的颜色.. < / p>

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

2 个答案:

答案 0 :(得分:0)

您的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[colors/color = "Red"]

...并获得其成本子节点:
//cupcake[colors/color = "Red"]/cost

答案 1 :(得分:-1)

告诉我们,您的代码有什么问题,如果有错误消息是什么......

我建议你尝试更好的simpleXML用法,因为它的语法更容易(对我来说)。您可以在此处查看How do I restrict route extensions in @RequestMapping paths for Spring MVC controllers?

链接

您在示例中使用的语法类似于js one。但是你可以在php中使用更简单的一个