PHP – XML: Removing Node

PHP XML Tips – Part 8: Do you want to remove child from exist XML? You can use removeChild(). Look at this:

01 <?php
02 $file = "books.xml";
03 $fp = fopen($file, "rb") or die("cannot open file");
04 $str = fread($fp, filesize($file));
05
06
07        
08 $xml = new DOMDocument();
09 $xml->formatOutput = true;
10 $xml->preserveWhiteSpace = false;
11 $xml->loadXML($str) or die("Error");
12
13 // original
14 echo "<xmp>OLD:\n". $xml->saveXML() ."</xmp>";
15
16 // get document element
17 $root   = $xml->documentElement;
18 $fnode  = $root->firstChild;
19
20 //get a node
21 $ori    = $fnode->childNodes->item(1);
22
23 // remove
24 $fnode->removeChild($ori);
25
26 echo "<xmp>NEW:\n". $xml->saveXML() ."</xmp>";
27 ?>

Books.xml, you can see at this.

Result, like this:

Series this article:
PHP – XML: Read from String
PHP – XML: Read from a File
PHP – XML: Retrieving Node Values
PHP – XML: Retrieving Attribute Values
PHP – XML: Processing XML
PHP – XML: Creating XML Document
PHP – XML: Adding XML Nodes
PHP – XML: Removing Node
PHP – XML: Replacing Node
PHP – XML: Filtering XML Nodes with XPath
PHP – XML: Filtering XML Nodes by Namespace

Bookmark and Share Tag: xml, node Category: PHP BasicPost : March 23rd 2008 Read: 18,776

By Rz Rasel Posted in Php

36 comments on “PHP – XML: Removing Node

  1. You produced some decent points there. I looked on the internet for that problem and located most individuals goes along with along together with your internet web site. 866714

  2. Wow! I’m really enjoying the layout of your website. Are you using a custom made template or is this readily available to all individuals? If you do not want to say the name of it out in the public, please e-mail me at: Groshans7489@gmail.com. I’d absolutely love to get my hands on this template! Kudos.

  3. I was suggested this web website by my cousin. Im not certain whether this post is written by him as nobody else know such detailed about my issue. Youre incredible! Thanks! 290555

  4. Hello! I basically would wish to offer a huge thumbs up for that wonderful information youve here during this post. I is going to be returning to your internet site to get far more soon. 694158

  5. Wow! This could be one particular of the most helpful blogs Weve ever arrive across on this subject. Actually Great. Im also an expert in this topic therefore I can understand your hard work. 230792

  6. You ought to essentially take into consideration working on growing this blog into a serious authority on this market. You evidently have a grasp handle of the subjects everyone seems to be searching for on this web site anyhow and you possibly can actually even earn a buck or two off of some advertisements. I’d discover following latest subjects and elevating the quantity of write ups you set up and I assure you’d start seeing some superb targeted traffic in the close to future. Just a thought, good luck in whatever you do!

  7. Thanks for another magnificent post. Where else could anyone get that kind of info in such a perfect way of writing? I have a presentation next week, and I am on the look for such info.

  8. Thanks, I have just been looking for info about this topic for a long time and yours is the greatest I have found out so far. But, what about the bottom line? Are you certain in regards to the supply?

  9. Thank you, Ive just been searching for information about this topic for a while and yours is the greatest Ive discovered till now. But, what in regards to the conclusion? Are you sure concerning the supply? 434333

  10. Someone essentially help to make seriously articles I would state. This is the first time I frequented your web page and thus far? I amazed with the analysis you made to create this actual post extraordinary. Excellent activity!

  11. Thank you for the auspicious writeup. It in reality was a amusement account it. Look complicated to far delivered agreeable from you! Nevertheless, how can we keep in touch? 946441

  12. Wow, Thank you because of this blog. Thats all I’m able to say. You certainly have made this site into something thats eye opening and important. You evidently know so much about the topic, youve covered a lot of bases. Great stuff using this part of the internet. Again, thank you with this blog.

  13. Wonderful defeat! I ought to apprentice although you amend your site, how can i subscribe for ones blog website? The consideration taught me to be a appropriate deal. I was bit acquainted with this your transmit offered dazzling transparent concept

  14. I hope you never stop! This is one of the best blogs Ive ever read. Youve got some mad skill here, man. I just hope that you dont lose your style because youre definitely one of the coolest bloggers out there. Please keep it up because the internet needs someone like you spreading the word.

  15. It’s a shame you don’t have a donate button! I’d most certainly donate to this brilliant blog! I suppose for now i’ll settle for book-marking and adding your RSS feed to my Google account. I look forward to brand new updates and will talk about this website with my Facebook group. Talk soon!

  16. Hello there, just became aware of your blog through Google, and found that it’s really informative. I’m gonna watch out for brussels. I will be grateful if you continue this in future. Many people will be benefited from your writing. Cheers!

  17. I really enjoy looking through on this website, it contains fantastic articles. “Beware lest in your anxiety to avoid war you obtain a master.” by Demosthenes.

  18. Attractive section of content. I just stumbled upon your site and in accession capital to assert that I get in fact enjoyed account your blog posts. Any way I’ll be subscribing to your augment and even I achievement you access consistently quickly.

Leave a reply to Christian Werdlow Cancel reply