Introduction on using PHP to create XML documents

You can create XML documents using the PHP DOM. If you have worked earlier in any script or language using DOM then you can very well work with the PHP DOM too. As with other language you have to first create a new document object.

The DOM has lot of function like createElement, createTextNode etc which can be used to create an element and to create a text node for an element. Then you can use add the textnode to an element and that element to another element and so on using the appendChild function.

Suppose you have create a new document object using the following command,

$domobj = new DomDocument();

Then you can create an element using command like,

$book = $domobj->createElement("book");
$author = $domobj->createElement("author");
$authtext = $domobj->createTextNode("Jeffrey Archer");

Now to add the elements to the document object, you have to perform the following commands,

$author->appendChild($authtext);
$book->appendChild($author);
$domobj->documentElement->appendChild($book);

Now you are free to save that XML file or print it out on the screen.




SetApp - 100 Apps for everything you ever wanted to do on Mac

FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

_______________________________________


 

 

FREE Subscription

Stay Current With the Latest Technology Developments Realted to XML. Signup for Our Newsletter and Receive New Articles Through Email.

Name:

Email:

Note : We never rent, trade, or sell our email lists to anyone. We assure that your privacy is respected and protected.

 

 

Add to My Yahoo!

Visit XML Training Material Guide Homepage

 

 

“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

Copyright - © 2004 - 2019 - All Rights Reserved.