|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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:
Then i figured to get the second to last i could just go... PHP Code:
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. |
|
#2
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
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:
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 |
|
#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:
takes the xml string, gets a count of linkdata nodes under the main xml node and subtracts 1 then unset()s it. |
|
#5
|
|||
|
|||
|
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 |
![]() |
| Viewing: Codewalkers Forums > PHP Related > PHP Coding > Removing last node of XML with XML DOM??? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|