doutiaosu2310 2016-11-21 18:41
浏览 99

通过选择值删除XML dom doc上的节点

Trying to make an API for currency conversion,

Need to select a specific currency and delete it from the xml file...

XML file looks like this:

<currencies>
    <currency>
        <ccode>CAD</ccode>
        <cname>Canadian Dollar</cname>
        <cntry>Canada</cntry>
    </currency>
    <currency>
        <ccode>CHF</ccode>
        <cname>Swiss Franc</cname>
        <cntry>Liechtenstein, Switzerland</cntry>
    </currency>
    <currency>
        <ccode>CNY</ccode>
        <cname>Yuan Renminbi</cname>
        <cntry>China</cntry>
    </currency>
...etc

I need to use php to select and delete the specific currency, at the moment trying this:

<?php
$dom = new DOMDocument("1.0", "utf-8");
$dom->load('data/ccodes.xml');
$nodes = $dom->getElementsByTagName("currencies");
foreach ($nodes as $n){
    if($n->getAttribute("ccode") == "CAD") {
        $parent = $n->parentNode;
        $parent->removeChild($n);
    }
  }
echo $dom->saveXML();
?>

But It's not working.... I'm pretty sure it's really simple but I have no idea what I'm doing with coding... :/

Need to make it so I can just change CAD to whatever to delete any currency I need to...

  • 写回答

1条回答 默认 最新

  • duanfa0072 2016-11-21 19:23
    关注

    Your iterating the root node currencies but I think you meant to iterate the currency nodes. ccode is not an attribute node, but a child element node. Even if you iterate currency nodes with the correct condition it would still not fully work.

    DOMElement::getElementsByTagName() returns a live result. Inside the loop you modify the DOM and the list is modified as well. You could us a for loop to iterate it backwards, use iterator_to_array() to materialize the node list into an array or use Xpath. DOMXpath::evaluate() returns a node list, but it is not a live result. So the list will not change if you modify the document.

    $document = new DOMDocument();
    //$document->load('data/ccodes.xml');
    $document->loadXml($xml);
    $xpath = new DOMXpath($document);
    
    foreach ($xpath->evaluate('/currencies/currency[ccode="CAD"]') as $node) {
      $node->parentNode->removeChild($node);    
    }
    
    echo $document->saveXML();
    

    Output:

    <?xml version="1.0"?>
    <currencies>
    
        <currency>
            <ccode>CHF</ccode>
            <cname>Swiss Franc</cname>
            <cntry>Liechtenstein, Switzerland</cntry>
        </currency>
        <currency>
            <ccode>CNY</ccode>
            <cname>Yuan Renminbi</cname>
            <cntry>China</cntry>
        </currency>
    </currencies>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败