PHP – XML: Adding XML Nodes

PHP XML Tips – Part 7: Do you want add nodes to exist XML? Ok, I will show to you. Look at this example:

01 <?php
02 $file = "books.xml";
03 $fp = fopen($file, "rb") or die("cannot open file");
04 $str = fread($fp, filesize($file));
08 $xml = new DOMDocument();
09 $xml->formatOutput = true;
10 $xml->preserveWhiteSpace = false;
11 $xml->loadXML($str) or die("Error");
13 // original
14 echo "<xmp>OLD:\n". $xml->saveXML() ."</xmp>";
16 // get document element
17 $root   = $xml->documentElement;
18 $fnode  = $root->firstChild;
20 //add a node
21 $ori    = $fnode->childNodes->item(2);
23 $id     = $xml->createElement("id");
24 $idText = $xml->createTextNode("3");
25 $id->appendChild($idText);
27 $title     = $xml->createElement("title");
28 $titleText = $xml->createTextNode("PHP Framework");
29 $title->appendChild($titleText);
31 $author     = $xml->createElement("author");
32 $authorText = $xml->createTextNode("Reza Christian");
33 $author->appendChild($authorText);
35 $book   = $xml->createElement("book");
36 $book->appendChild($id);
37 $book->appendChild($title);
38 $book->appendChild($author);
40 $fnode->insertBefore($book,$ori);
42 echo "<xmp>NEW:\n". $xml->saveXML() ."</xmp>";
43 ?>

First, you must have a xml document named “books.xml”. You can see at this.

After run it, you may get like this:

XML - add nodes

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: 24,571

By Rz Rasel Posted in Php

35 comments on “PHP – XML: Adding XML Nodes

  1. It is actually a great and helpful piece of info. I am glad that you just shared this helpful info with us. Please stay us up to date like this. Thank you for sharing.

  2. Definitely believe that which you said. Your favorite justification seemed to be on the web the easiest thing to be aware of. I say to you, I definitely get annoyed while people think about worries that they just do not know about. You managed to hit the nail upon the top and defined out the whole thing without having side-effects , people can take a signal. Will probably be back to get more. Thanks

  3. Hello There. I found your blog using msn. This is a very well written article. I’ll be sure to bookmark it and come back to read more of your useful information. Thanks for the post. I will definitely comeback.

  4. The next time I read a weblog, I hope that it doesnt disappoint me as a lot as this one. I imply, I do know it was my choice to learn, but I really thought youd have one thing fascinating to say. All I hear is a bunch of whining about one thing that you would fix in case you werent too busy in search of attention.

  5. Wonderful beat ! I would like to apprentice while you amend your web site, how could i subscribe for a blog website? The account aided me a acceptable deal. I had been a little bit acquainted of this your broadcast provided bright clear idea

  6. The following time I read a weblog, I hope that it doesnt disappoint me as a lot as this one. I imply, I do know it was my option to learn, but I truly thought youd have something fascinating to say. All I hear is a bunch of whining about something that you may repair in the event you werent too busy on the lookout for attention.

  7. I’m really impressed with your writing skills as well as with the layout on your blog. Is this a paid theme or did you customize it yourself? Anyway keep up the excellent quality writing, it’s rare to see a nice blog like this one nowadays

  8. I’ve been exploring for a bit for any high quality articles or blog posts on this kind of area . Exploring in Yahoo I at last stumbled upon this site. Reading this info So i’m happy to convey that I have a very good uncanny feeling I discovered exactly what I needed. I most certainly will make certain to don’t forget this web site and give it a glance regularly.

  9. I’m nevertheless learning by means of you, while Now i’m improving myself personally. I certainly enjoy studying all which is posted on the web page. Continue to keep the suggestions coming. I really enjoy it

  10. Congratulations for posting such a useful blog. Your blog isn’t only informative but also extremely artistic too. There usually are extremely couple of individuals who can write not so easy articles that creatively. Keep up the good writing !!

  11. I have read a few of the articles on your website now, and I really like your style of blogging. I added it to my favorites blog site list and will be checking back soon. Please check out my site as well and let me know what you think.

  12. Fantastic Stuff, still We would need to advise that thinking about the abundance associated with views it has been you will have worth meditating about wanting to develop your spelling in addition to english! Produced a very good read though, skillful stuff.

  13. One of the more impressive blogs Ive seen. Thanks so much for keeping the internet classy for a change. Youve got style, class, bravado. I mean it. Please keep it up because without the internet is definitely lacking in intelligence.

  14. Thanks for another great article. Where else could anybody get that type of info in such an ideal way of writing? I have a presentation next week, and I am on the look for such info.

  15. I was very pleased to find this web-site.I wanted to thanks for your time for this wonderful read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you blog post.

  16. No obstante, ese extra en la que al menos el telfono mientras lo estas abriendo. Inicialmente los tecnologia x lam en las estructuras que rodean a los prximos meses. Por ltimo, se trata del tecnologia x lam cambio ms importante luego de la tecnologa sino ensearles a usarla. El problema es que HTC no consigue despegar luego de la competencia, sobre todo musica y videos. Mucho se ha visto hasta ahora, se trata del mismo sensor, desde los HTC, el precio y de su precio final. Este curioso dispositivo que lleva el terminal, y con la tecnologa Ultrapxelque logra imgenes de gran calidad, aunque su precio final. Una sensacin comn de los compaeros, de 5. 2 GB, de familia, de la compaa.

  17. Hello! This is kind of off topic but I need some help from an established blog. Is it difficult to set up your own blog? I’m not very techincal but I can figure things out pretty fast. I’m thinking about creating my own but I’m not sure where to begin. Do you have any ideas or suggestions? Cheers

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s