John Davidson

Parse xml & delete a node in PHP

0 comments
Message:


I tried to parse an XML & delete a node "displayTextFlow" from the XML. Below is my code:


$filePath = file_get_contents('sample.xml');
$xml = new SimpleXMLIterator($filePath);
$iterator = new RecursiveIteratorIterator($xml, RecursiveIteratorIterator::SELF_FIRST);
foreach ($iterator as $key => $element) {
if ($key == 'displayTextFlow') {
unset($iterator[$key]);
}
}
$finalXML = $xml->asXML();
print_r($finalXML);
exit;

In my sample XML, I'm not sure about the exact place for the tag "displayTextFlow". It may come in any other place. So I need to write a code to parse the XML & delete the tag "displayTextFlow" in all the places
Note: I need to do it only using SimpleXMLIterator.


Please check the sample XML:


<?xml version="1.0"?>
<document savedInEditor="false">
<pages numIncludedPages="2">
<item includeInOutputComputed="true">
<frames>
<item id="2555D134-0F82-49A8-B3CD-4536DB607AE5" name="New Frame">
sample text
</item>
<item id="C15D2F81-3925-4E3E-B1F4-3F6729CAF8AD" name="New Frame 1">
sample text
</item>
<item id="AD0216C3-CD41-4994-B8AC-9E2DACF7439E" name="New Frame 2">
<displayTextFlow>
<TextFlow xmlns="http://ns.adobe.com/textLayout/2008" />
</displayTextFlow>
<textFlow>
<TextFlow>
<span>Sampe span​</span>
</TextFlow>
</textFlow>
</item>
<item id="0DBBAAB8-A572-49BF-8E77-BDAC88219DC7" name="New Frame 3">
<displayTextFlow>
<TextFlow xmlns="http://ns.adobe.com/textLayout/2008" />
</displayTextFlow>
<textFlow>
<TextFlow>
<span>Sampe span​ frame 3</span>
</TextFlow>
</textFlow>
</item>
<item id="4B392B37-3B2C-443A-A6B6-4219184D848D" name="New Frame 4">
<displayTextFlow>
<TextFlow xmlns="http://ns.adobe.com/textLayout/2008" />
</displayTextFlow>
<textFlow>
<TextFlow>
<span>Sampe span​ frame 4</span>
</TextFlow>
</textFlow>
</item>
<item id="E82B3F8D-02F9-45E5-8955-4F161C0DCAF4" name="New Frame 5">
<displayTextFlow>
<TextFlow xmlns="http://ns.adobe.com/textLayout/2008">
<span>needs to be removed</span>
</TextFlow>
</displayTextFlow>
<textFlow>
<TextFlow>
<span>needs to be removed</span>
</TextFlow>
</textFlow>
</item>
</frames>
<sectionOptions includeInOutput="true" newSection="true" pageStart="1"/>
</item>
</pages>
<item id="E82B3F8D-02F9-45E5-8955-4F161C0DCAF4" name="sample 1">
<displayTextFlow>
<TextFlow xmlns="http://ns.adobe.com/textLayout/2008">
<span>needs to be removed</span>
</TextFlow>
</displayTextFlow>
<textFlow>
<TextFlow>
<span>needs to be removed</span>
</TextFlow>
</textFlow>
</item>
<item id="E82B3F8D-02F9-45E5-8955-4F161C0DCAF4" name="sample 2">
<displayTextFlow>
<TextFlow xmlns="http://ns.adobe.com/textLayout/2008">
<span>needs to be removed</span>
</TextFlow>
</displayTextFlow>
<textFlow>
<TextFlow>
<span>needs to be removed</span>
</TextFlow>
</textFlow>
</item>
</document>


My Answer:
You can achieve this by using a recursive function to iterate through all the nodes in the XML and delete the "displayTextFlow" nodes. Here's an updated version of your code:

php
$filePath = file_get_contents('sample.xml');
$xml = new SimpleXMLElement($filePath);

function deleteNode($node) {
foreach ($node->children() as $child) {
if ($child->getName() == 'displayTextFlow') {
unset($child[0]);
}
deleteNode($child);
}
}

deleteNode($xml);

$finalXML = $xml->asXML();
print_r($finalXML);
exit;


This code defines a recursive function `deleteNode` that iterates through all the children of a given node and deletes any "displayTextFlow" nodes it encounters. The function is then called on the root node of the XML document to delete the nodes from all levels. Finally, the modified XML is printed out.

Rate this post

3 of 5 based on 5431 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us