2015-06-03 63 views
0

嗨,我想将成本值更改为$ 6.0,其中蛋糕的颜色为红色。我怎么能做到这一点..在这里是我的两个样品蛋糕,虽然我有很多蛋糕在我的XML文件..所以我想先找到蛋糕的颜色是红色,然后更改相应的蛋糕价格,无论我喜欢什么..如何使用Dom更改xml中的特定节点值php

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

使用xpath'// cupcake [.// color [text()=“Red”]]/cost'来查找需要的元素并更改值。也许你应该用'''包装你的xml。$ xml。'' – splash58

回答

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

+0

谢谢你这么这么多..我是新的XML,所以我没有意识到..谢谢你再次..和我的父节点是纸杯蛋糕,我没有粘贴在我的上述文件sry –

-1

告诉我们,什么是你的代码错误,什么是如果任何错误消息...

,我建议你尝试的SimpleXML更好因为它的语法比较简单(对我来说)。这里是你可能检查的链接http://php.net/manual/en/simplexml.examples-basic.php

你在你的例子中使用的语法类似于js之一。但你可以使用更简单的php

+0

我没有得到它,并记住,我必须使用DOM ..所以你可以请写一段代码,以便我能明白.. –

+0

nope没有工作 –

+0

它并没有改变它的值,说:未定义的属性:DOMNodeList :: $ nodeValue在C:\ wamp \ www \ Assignment \ ChangeNodeValue.php在第16行 –