简体   繁体   English

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

[英]how to change specific node value in xml using Dom php

Hi i want to change the value of cost to $6.0 where the color of cupcake is Red. 嗨,我想将成本的值更改为$ 6.0,其中纸杯蛋糕的颜色是红色。 how can i achieve that..here is my two sample cupcake though i have many cupcake in my xml file..so i want to first find the cupcake whose color is red and then change the corresponding cupcake price to whatever i like.. 我怎么能实现..这是我的两个示例蛋糕,尽管我的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>

and my php file is 和我的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); 
?>

You XML is missing a document element. 您的XML缺少文档元素。 It is not a valid XML file. 这不是有效的XML文件。

DOMNode::getElementsByTagName() returns a node list, not a single node. DOMNode::getElementsByTagName()返回节点列表,而不是单个节点。 $nodeValue is a property of DOMNode , not DOMNodeList . $nodeValueDOMNode的属性,而不是DOMNodeList的属性。 Just checking the color value, will not do it. 仅检查颜色值,不会做。 A cupcake can have several colors. 纸杯蛋糕可以有几种颜色。 If you use XPath you can have conditions like that: 如果使用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();

Output: 输出:

<?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 allows you to fetch nodes and scalar values. XPath允许您获取节点和标量值。 It this case: 在这种情况下:

Fetch all cupcake nodes... 提取所有纸杯蛋糕节点...
//cupcake

... with a color node that equals "Red"... ...其颜色节点等于“红色” ...
//cupcake[colors/color = "Red"]

...and get its cost child node: ...并获取其成本子节点:
//cupcake[colors/color = "Red"]/cost

Tell us, what is wrong with your code, what is the error message if any... 告诉我们,您的代码有什么问题,错误消息是什么(如果有)...

and I suggest you try better the simpleXML usage since the syntax of it is way easier (to me). 并且我建议您尝试更好地使用simpleXML,因为它的语法更简单(对我而言)。 Here is a link you might check http://php.net/manual/en/simplexml.examples-basic.php 这是您可以查看的链接http://php.net/manual/zh/simplexml.examples-basic.php

The syntax you use in your example resembles the js one. 您在示例中使用的语法类似于js。 but you can you use a more easy one in php 但您可以在php中使用更简单的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM