PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old October 19th, 2009, 04:31 PM
pqalex pqalex is offline
Registered User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 8 pqalex User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 17 m
Reputation Power: 0
Removing last node of XML with XML DOM or SimpleXML???

Hi,

I've got a XML document that looks like this...
<alllinks>
<linkdata>blahblahblah</linkdata>
<linkdata>blahblahblah</linkdata>
<linkdata>blahblahblah</linkdata>
<linkdata>blahblahblah</linkdata>
<linkdata>blahblahblah</linkdata>
<linkdata>blahblahblah</linkdata>
</alllinks>

Users add new information to this XML and it is appended as the new last child of "alllinks". I need to give them the ability to delete a node, but cannot figure out how to reference the node to be deleted. The last 50 nodes are shown on a html page in reverse order (as the information thats being shown is magazine articles, so the users add a new article("linkdata" node) and the last one (newest) is shown first. Now, on an editing site for them, when they click on the 1st link (the last node) it takes them to a html form where they have the option to delete that "link" or node. So basically, I need to get the last child node of "alllinks" (the last "linkdata") and delete it. Then I just need to be able to reference each node in relation to the last node and delete those if i want.

Here is the PHP i have so far...

PHP Code:
 $file "MagazineArticles.xml"
$xml = new DomDocument(); 
$xml->preserveWhitespace false
$xml->load($file);  

$root $xml->documentElement

//The problem is in this next line, I need to be able to reference the last child node ("linkdata" node) of "alllinks" and delete it. Then I need to repeat this code, referencing the second to last and third to last and so on (this will be done in a series of if else statements depending on what link the user selects, which i already have worked out).
$node $root->getElementsByTagName('linkdata[position()=last()]');

$oldnode $root->removeChild($node);  
echo 
$xml->saveXML(); 
$xml->save($file); 


Then i figured to get the second to last i could just go...

PHP Code:
 $node $root->getElementsByTagName('linkdata[position()=last()-1]'); 


But this isn't working. Can anyone help me figure this out?

It works fine if I call them by element name and item # but obviously in this case we do not know the item # as new nodes are constantly being added...

Seems like a simple task, for some reason I just can't get it....

IAmALlama - thanks for helping me out before, as you can see I used the position()=last() thing that you had shown me, I believe that only works for SimpleXML but I tried to use it here too, looks like it doesn't want to work here. It worked great for the other problem though. I may be going about this all wrong trying to use the DOM. It's as though I am missing half from each one (simple and dom) with the simpleXML i can easily use the way you showed to reference the nodes, but can't figure out how to remove. with the dom i can figure out how to remove but not how to reference... feeling a bit hopeless...

Thanks in advance for any help,
Alex

Last edited by pqalex : October 19th, 2009 at 04:45 PM.

Reply With Quote
  #2  
Old October 19th, 2009, 04:49 PM
IAmALlama IAmALlama is offline
Me
Click here for more information. Click here for more information
Click here for more information
 
Join Date: Apr 2007
Location: Seattle, WA
Posts: 1,937 IAmALlama User rank is Private First Class (20 - 50 Reputation Level)IAmALlama User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 5 Days 1 h 54 m 18 sec
Reputation Power: 4
looks like you are trying to use a xpath query in getElementsByTagName. that function doesn't allow for a xpath query, only an exact node name match. You need to specifically use the xpath functions in dom to use an xpath query.
http://us.php.net/manual/en/domxpath.query.php

Reply With Quote
  #3  
Old October 19th, 2009, 05:05 PM
pqalex pqalex is offline
Registered User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 8 pqalex User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 17 m
Reputation Power: 0
I can't figure out how to go about getting the last node with xpath. Is there a way with the getElementsByTagName to get the last "linkdata"? or do you know of a way to use simpleXML to remove a node? I can't find anything about removing nodes with simpleXML, that's why i went back to the DOM. But when I try this:

PHP Code:
 $file "editMagazines.xml";
$xml = new DomDocument();
$xml->preserveWhitespace false;
$xml->load($file);

$xpath = new DOMXPath($xml);
$root $xml->documentElement;
$firstLink $xpath->query('/alllinks')->lastChild;
$removeLink $root->removeChild($firstLink);

echo 
$xml->saveXML();
$xml->save($file); 


I get this...

Catchable fatal error: Argument 1 passed to DOMNode::removeChild() must be an instance of DOMNode, null given in /htdocs/SupportPages/anothertest.php on line 10

line 10 is the "$removeLink" line

Reply With Quote
  #4  
Old October 19th, 2009, 08:10 PM
IAmALlama IAmALlama is offline
Me
Click here for more information. Click here for more information
Click here for more information
 
Join Date: Apr 2007
Location: Seattle, WA
Posts: 1,937 IAmALlama User rank is Private First Class (20 - 50 Reputation Level)IAmALlama User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 5 Days 1 h 54 m 18 sec
Reputation Power: 4
in simplexml you can just unset() the node. You can also use count() to get a count of nodes under a specific node. ex:
PHP Code:
<?php

$string 
=<<<XML
<alllinks>
  <linkdata>blahblahblah</linkdata>
  <linkdata>blahblahblah</linkdata>
  <linkdata>blahblahblah</linkdata>
  <linkdata>blahblahblah</linkdata>
  <linkdata>blahblahblah</linkdata>
  <linkdata>last</linkdata>
</alllinks>
XML;
$xml simpleXML_Load_String($string);
unset(
$xml->linkdata[count($xml)-1]);
var_dump($xml);

?>

takes the xml string, gets a count of linkdata nodes under the main xml node and subtracts 1 then unset()s it.

Reply With Quote
  #5  
Old October 20th, 2009, 10:06 AM
pqalex pqalex is offline
Registered User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 8 pqalex User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 17 m
Reputation Power: 0
IAmALlama Rules!

IAmALlama, I gotta give it up, you are the man!

I believe that you have just helped me solve the final problem (fingers crossed) in my PHP/XML/HTML/Flash content editing system. I owe you big time!

Best Regards,
Alex

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > Removing last node of XML with XML DOM???


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek