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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog