Solved

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

Posted on 2014-09-03
8
1,373 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 110

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 110

Expert Comment

by:Ray Paseur
ID: 40303645
Ahh, I get it - coincidental alignment.  Let me get another code sample for you.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 110

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 110

Expert Comment

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

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
The viewer will learn how to count occurrences of each item in an array.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

689 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