PaulSkinner
PaulSkinner

Reputation: 628

Removing parent nodes from XML in PHP

I have some XML like this:

<tree path="Masters">
    <item  id="Masters\2014"  name="2014"  isFolder="true"  path="Masters\2014" >
        <item  id="Masters\2014\Brochures"  name="Brochures"  isFolder="true"  path="Masters\2014\Brochures" >
            <item  id="Masters\2014\Brochures\PLEASE DO NOT COPY"  name="PLEASE DO NOT COPY"  isFolder="true"  path="Masters\2014\Brochures\PLEASE DO NOT COPY" >
                <item  id="a4e6f520-9b26-42c0-af92-bbd17ab6e8b6"  name="00001"  isFolder="false"  path="Masters\2014\Brochures\PLEASE DO NOT COPY\00001.xml" >
                    <fileInfo fileSize="141.23 Kb"></fileInfo>
                </item>
                <item  id="6b8cbff5-cf03-4d2c-9931-bb58d7f3ff8a"  name="00002"  isFolder="false"  path="Masters\2014\Brochures\PLEASE DO NOT COPY\00002.xml" >
                    <fileInfo fileSize="192.19 Kb"></fileInfo>
                </item>
            </item>
            <item  id="65773008-4e64-4316-92dd-6a535616ccf6"  name="Sales Brochure A4"  isFolder="false"  path="Masters\2014\Brochures\Sales Brochure A4.xml" >
                <fileInfo fileSize="34.38 Kb"></fileInfo>
            </item>
        </item>
    </item>
</tree>

I need to remove all nodes (including its children) where the attribute name matches the regex /^[0-9]{5,6}$/ (it is a 5 or 6 digit long name) and also remove its parent.

As well as this I also need to remove any elements that have the attribute isFolder set to false.

The code I have so far is:

<?php

$simple_xml = simplexml_load_string($xml);

//Foreach item tag
foreach($simple_xml->xpath('//item') as $item) {

    //This correctly identifies the nodes
    if(preg_match('/^[0-9]{5,6}$/', $item->attributes()->name)) {

        //This doesn't work. I'm guessing chaining isn't possible?
        $dom = dom_import_simplexml($item);
        $dom->parentNode->parentNode->removeChild($dom);


    } else {

        //This correctly identifies the nodes
        if($item->attributes()->isFolder == 'false') {

            //This part works correctly and removes the nodes as required
            $dom = dom_import_simplexml($item);
            $dom->parentNode->removeChild($dom);

        }

    }

}

//At this point $simple_xml should contain the rebuilt xml tree in simplexml style

?>

As can be seen from the comments, I have the isFolder section working as I need, but I can't seem to remove the parent node when an item node's attribute name has a value of a 5 or 6 digit long name.

Thanks in advance for any help.

Upvotes: 0

Views: 1145

Answers (1)

user142162
user142162

Reputation:

The main issue was that you were trying to remove the <item> node from it's grandparent. The code below has been re-factored so the parent will be removed from the grandparent.

$simple_xml = simplexml_load_string($xml);
foreach ($simple_xml->xpath('//item') as $item) {
  if (preg_match('/^[0-9]{5,6}$/', $item['name'])) {
    $dom = dom_import_simplexml($item);
    $parent = $dom->parentNode;
    if ($parent && $parent->parentNode) {
      $parent->parentNode->removeChild($parent);
    }
  } else if ($item['isFolder'] == 'false') {
    $dom = dom_import_simplexml($item);
    $dom->parentNode->removeChild($dom);
  }
}

Upvotes: 1

Related Questions