Solved

Remove XML parent node based on value of child node with php domdocument

Posted on 2014-09-03
8
1,288 Views
Last Modified: 2014-09-04
I've tried everything I could find and think of to get this working correctly, to no avail.  I'm trying to delete an xml node based on the node value of one of it's children (entrynum).  Again, I've tried several different approaches and variations of them with no success.  The logic seems correct, but I've been getting inexplicable output with no errors.  I've gotten close, but still with erratic, unpredictable behavior.  Here's where I'm at with it now (my latest attempt anyway).  Any suggestions.  Thanks in advance.

The XML in entries.xml:

<?xml version="1.0" encoding="utf-8"?>
<entries>
  <entry>
    <title>0</title>
    <entrydate>0</entrydate>
    <contents>0</contents>
    <entrynum>0</entrynum>
  </entry>
  <entry>
    <title>1</title>
    <entrydate>1</entrydate>
    <contents>1</contents>
    <entrynum>1</entrynum>
  </entry>
  <entry>
    <title>2</title>
    <entrydate>2</entrydate>
    <contents>2</contents>
    <entrynum>2</entrynum>
  </entry>
  <entry>
    <title>3</title>
    <entrydate>3</entrydate>
    <contents>3</contents>
    <entrynum>3</entrynum>
  </entry>
</entries>

Open in new window


And here's the php: (if entrynum = $GLOBALS[num] I want that node removed when the script runs).

$entries = new DOMDocument('1.0', 'utf-8');
	$entries->formatOutput = true;
	$entries->preserveWhiteSpace = true;
	$entries->load('entries.xml');

	$entry_node = $entries->getElementsByTagName("entries")->item(0);
	
	
	foreach($entry_node->getElementsByTagName("entry") as $node){
	if($node->getElementsByTagName("entrynum")->nodeValue = $GLOBALS['num']){
	$node->parentNode->removeChild($node);}}
		
		
	$entries->save('entries.xml');

Open in new window

0
Comment
Question by:Kaiser
  • 4
  • 4
8 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40303416
Please see: http://iconoun.com/demo/temp_kwkened.php
<?php // demo/temp_kwkened.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Web_Development/WebApplications/Q_28511238.html

// TEST DATA
$xml = <<<EOD
<?xml version="1.0" encoding="utf-8"?>
<entries>
  <entry>
    <title>0</title>
    <entrydate>0</entrydate>
    <contents>0</contents>
    <entrynum>0</entrynum>
  </entry>
  <entry>
    <title>1</title>
    <entrydate>1</entrydate>
    <contents>1</contents>
    <entrynum>1</entrynum>
  </entry>
  <entry>
    <title>2</title>
    <entrydate>2</entrydate>
    <contents>2</contents>
    <entrynum>2</entrynum>
  </entry>
  <entry>
    <title>3</title>
    <entrydate>3</entrydate>
    <contents>3</contents>
    <entrynum>3</entrynum>
  </entry>
</entries>
EOD;

// DELETION SIGNAL (if entrynum = $GLOBALS[num]
$GLOBALS['num'] = 2;

// MAKE AN OBJECT
$obj = SimpleXML_Load_String($xml);

// REMOVE THE NODE
unset($obj->entry[$GLOBALS['num']]);

// RECOVER THE XML AND SHOW THE WORK PRODUCT
$new = $obj->AsXML();
echo '<pre>' . htmlentities($new);

Open in new window

0
 

Author Comment

by:Kaiser
ID: 40303586
Hi Ray,

Thank you for the response.  To be sure I understand, when removing the node with
unset($obj->entry[$GLOBALS['num']]);

Open in new window

 won't this remove the node based on its position in the xml tree?  What I'd like to do is remove a child node (entrynum) of the entry node.  So something like this:  
$obj->entry->entrynum

Open in new window

to target the child node.  But to delete that child node based on its value rather than its position, how should I target its value?
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40303645
Ahh, I get it - coincidental alignment.  Let me get another code sample for you.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:Kaiser
ID: 40303658
Ray,

I need to be able to remove the entry node based on the value of entrynum, so that when entry nodes are removed I can still remove the desired node based on the value of entrynum, because every time a node is removed, the remaining nodes below it move up a position, so targeting their position in the xml tree won't work.  Hope I'm explaining that clearly.  Thanks again.

<entries>
  <entry>
    <title>0</title>
    <entrydate>0</entrydate>
    <contents>0</contents>
    <entrynum>0</entrynum>
  </entry>
  <entry>
    <title>2</title>
    <entrydate>2</entrydate>
    <contents>2</contents>
    <entrynum>2</entrynum>
  </entry>
  <entry>
    <title>6</title>
    <entrydate>6</entrydate>
    <contents>6</contents>
    <entrynum>6</entrynum>
  </entry>
  <entry>
    <title>3</title>
    <entrydate>3</entrydate>
    <contents>3</contents>
    <entrynum>3</entrynum>
  </entry>
</entries>

Open in new window

0
 

Author Comment

by:Kaiser
ID: 40303667
Yes exactly, coincidental alignment.  I should have presented it more clearly.
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40303700
Try this.  Note the use of "foo" in the script.

<?php // demo/temp_kwkened.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Web_Development/WebApplications/Q_28511238.html

// TEST DATA
$xml = <<<EOD
<?xml version="1.0" encoding="utf-8"?>
<entries>
  <entry>
    <title>0</title>
    <entrydate>0</entrydate>
    <contents>0</contents>
    <entrynum>0</entrynum>
  </entry>
  <entry>
    <title>1</title>
    <entrydate>1</entrydate>
    <contents>1</contents>
    <entrynum>1</entrynum>
  </entry>
  <entry>
    <title>2</title>
    <entrydate>2</entrydate>
    <contents>2</contents>
    <entrynum>foo</entrynum>
  </entry>
  <entry>
    <title>3</title>
    <entrydate>3</entrydate>
    <contents>3</contents>
    <entrynum>bar</entrynum>
  </entry>
</entries>
EOD;

// DELETION SIGNAL (if entrynum = $GLOBALS[num]
$GLOBALS['num'] = 'foo';

// MAKE AN OBJECT
$obj = SimpleXML_Load_String($xml);

// USE A COUNTING ITERATOR TO FIND THE ELEMENT WE WANT TO DELETE
$cnt = 0;
foreach ($obj as $ent)
{
    if ($ent->entrynum == $GLOBALS['num'])
    {
        unset($obj->entry[$cnt]);
    }
    $cnt++;
}

// RECOVER THE XML AND SHOW THE WORK PRODUCT
$new = $obj->AsXML();
echo '<pre>' . htmlentities($new);

Open in new window

0
 

Author Comment

by:Kaiser
ID: 40303759
Wow!  Simply awesome.  I tried using a counter like that to cycle through the nodes and test the condition, but with a while loop.  This is great, thank you!
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40304358
Thanks for the points - it's a great question, ~Ray
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to send multiple emails at the same time in PHP 12 58
How to stop user from going back to a previous page in PHP 3 31
paypal ipn url 5 57
XSLT Display Label Name 1 22
Developer tools in browsers have been around for a while, yet they are still heavily underused by developers. Developers still fix html or CSS then refresh page to see effect, or they put alert or debugger in JavaScript and then try again and again …
Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question