Solved

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

Posted on 2014-09-03
8
1,244 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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
Ahh, I get it - coincidental alignment.  Let me get another code sample for you.
0
 

Author Comment

by:Kaiser
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Kaiser
Comment Utility
Yes exactly, coincidental alignment.  I should have presented it more clearly.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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
Comment Utility
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 108

Expert Comment

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

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
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…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now