【问题标题】:Need to remove the xml child by attribute value in PHP需要在PHP中通过属性值删除xml子项
【发布时间】:2017-07-22 12:23:31
【问题描述】:

我想从此 xml 中删除子标签。例如我想删除这个

"<ExtraCategory diffgr:id="ExtraCategory1" msdata:rowOrder="0">
        <Barcode>5051694676090</Barcode>
        <CategoryID>1</CategoryID>
        <Value>High Quality</Value>
</ExtraCategory>"

孩子。我就是这样解析xml的。

$doc = new DOMDocument();
$doc->loadXML($Xmlresult);
$xpath = new DOMXpath($doc);
unset($doc->xpath("ExtraCategory[@diffgr:id=$id]")[0]);

但未能删除。我已经在stackoverflow上完成了所有的anwsers,但未能得到解决方案。任何帮助将不胜感激。我的 xml 如下:

<DocumentElement
    xmlns="">
    <ExtraCategory diffgr:id="ExtraCategory1" msdata:rowOrder="0">
        <Barcode>5051694676090</Barcode>
        <CategoryID>1</CategoryID>
        <Value>High Quality</Value>
    </ExtraCategory>
    <ExtraCategory diffgr:id="ExtraCategory2" msdata:rowOrder="1">
        <Barcode>5051694676090</Barcode>
        <CategoryID>2</CategoryID>
        <Value>Stylish Appearance</Value>
    </ExtraCategory>
    <ExtraCategory diffgr:id="ExtraCategory3" msdata:rowOrder="2">
        <Barcode>5051694676090</Barcode>
        <CategoryID>3</CategoryID>
        <Value>In Fashion</Value>
    </ExtraCategory>

【问题讨论】:

    标签: php xml xml-parsing


    【解决方案1】:

    您不能只unset() 一个 XML 节点,您需要从 DOM 中删除该节点。比如……

       $Xmlresult = <<<XML
    <?xml version="1.0" encoding="UTF-8"?>
    <DocumentElement
        xmlns:diffgr="http://someURL" xmlns:msdata="http://someURL">
        <ExtraCategory diffgr:id="ExtraCategory1" msdata:rowOrder="0">
            <Barcode>5051694676090</Barcode>
            <CategoryID>1</CategoryID>
            <Value>High Quality</Value>
        </ExtraCategory>
        <ExtraCategory diffgr:id="ExtraCategory2" msdata:rowOrder="1">
            <Barcode>5051694676090</Barcode>
            <CategoryID>2</CategoryID>
            <Value>Stylish Appearance</Value>
        </ExtraCategory>
        <ExtraCategory diffgr:id="ExtraCategory3" msdata:rowOrder="2">
            <Barcode>5051694676090</Barcode>
            <CategoryID>3</CategoryID>
            <Value>In Fashion</Value>
        </ExtraCategory>
    </DocumentElement>
    XML;
    
    $doc = new DOMDocument();
    $doc->loadXML($Xmlresult);
    $xpath = new DOMXpath($doc);
    $id="ExtraCategory1";
    $xpath->registerNamespace("diffgr", "http://someURL");
    $removeNode = $xpath->query("//ExtraCategory[@diffgr:id=\"$id\"]")[0];
    $removeNode->parentNode->removeChild($removeNode);
    echo $doc->saveXML();
    

    需要registerNamespace 以允许您在 XPath 表达式中引用命名空间,您需要更正命名空间,因为这只是我用来使其工作的虚拟名称。

    主要的一点是使用removeChild,这是一个有点复杂的语句,但它需要你要删除的节点,上升到它的父节点,然后从中删除子节点。

    会给...

    <?xml version="1.0" encoding="UTF-8"?>
    <DocumentElement xmlns:diffgr="http://someURL" xmlns:msdata="http://someURL">
    
        <ExtraCategory diffgr:id="ExtraCategory2" msdata:rowOrder="1">
            <Barcode>5051694676090</Barcode>
            <CategoryID>2</CategoryID>
            <Value>Stylish Appearance</Value>
        </ExtraCategory>
        <ExtraCategory diffgr:id="ExtraCategory3" msdata:rowOrder="2">
            <Barcode>5051694676090</Barcode>
            <CategoryID>3</CategoryID>
            <Value>In Fashion</Value>
        </ExtraCategory>
    </DocumentElement>
    

    (注意我已经添加了命名空间的声明,您可能会有自己的定义,但我需要添加一些东西。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-23
      • 2020-09-07
      • 2019-12-18
      • 2013-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多