![]() |
Dynamic XML in PHP
Creating dynamic XML with PHPWhen working with XML-based applications, developers often find themselves facing the requirement to generate XML-encoded data structures on the fly. Examples of this include an XML order template based on user input in a Web form, or an XML representation of a server request or client response based on run-time parameters. Although this task might seem intimidating, it's actually quite simple when one takes into account PHP's sophisticated DOM API for dynamic node construction and manipulation. In this article, you will get know the main functions in this API, showing you how to programmatically generate a complete well-formed XML document from scratch and save it to disk. Note: This article assumes a working Apache/PHP5 installation with the DOM functions enabled, and a working knowledge of basic XML constructs such as elements, attributes and CDATA blocks. You can obtain an introduction to these topics from the introductory material at Melonfire (http://melonfire.com/community/colum...icle.php?id=78 and http://melonfire.com/community/colum...icle.php?id=79) Creating the Doctype declarationLet's start right at the top, with the XML declaration. In PHP, this is fairly simple; it only requires you to instantiate an object of the DOMDocument class and supply it with a version number. To see it in action type out the example script in Listing A. Listing A Code: PHP
When you view the output in your browser, you should see something like this: Code: XML
Adding elements and text nodesNow that's all very pretty and fine, but the real power of XML comes from its elements and the content they enclose. Fortunately, once you've got the basic DOMDocument initialised, this becomes extremely simple. There are two steps to the process: 1. For each element or text node you wish to add, call the DOMDocument object's createElement() or createTextNode() method with the element name or text content. This will result in the creation of a new object corresponding to the element or text node. 2. Append the element or text node to a parent node in the XML tree by calling that node's appendChild() method and passing it the object produced in the previous step. An example will make this clearer. Consider the script in Listing B. Listing B Code: PHP
Code: XML
Listing C Code: PHP
Code: XML
Adding attributesYou can also add qualifying information to your elements, through the thoughtful use of attributes. With the PHP DOM API, attributes are added in a two-step process: first create an attribute node holding the name of the attribute with the DOMDocument object's createAttribute() method, and then append a text node to it holding the attribute value. Listing D is an example. Listing D Code: PHP
Code: XML
Adding CDATA blocks and processing instructionsWhile not used quite as often, CDATA blocks and processing instructions (PI) are also well-supported by the PHP API, through the DOMDocument object's createCDATASection() and createProcessingInstruction() methods. Listing E shows you an example. Listing E Code: PHP
Code: XML
Saving the resultsOnce you've got the tree the way you want it, you can either save it to a file or store it in a PHP variable. The former function is performed by calling the save() method with a file name, while the latter is performed by calling the saveXML() method and assigning the result to a string. Here's an example (Listing F). Listing F Code: PHP
|
Re: Dynamic XML in PHP
Hi,
Liked so much your article, I have a basic knowledge on PHP, but i dont know what is the "->" Thanks, ~abilondias |
Re: Dynamic XML in PHP
nice explain
|
Re: Dynamic XML in PHP
Thanks to share this
|
Re: Dynamic XML in PHP
Hi.....I think it is very nice sharing in here.........thanks a lot.......:charming::charming:
|
Re: Dynamic XML in PHP
Thank you so much...excellent post!!
Just what i needed.! |
Re: Dynamic XML in PHP
it's actually quite simple when one takes into account PHP's sophisticated DOM API for dynamic node construction and manipulation. really a lot of information share in your website. thanks
|
Re: Dynamic XML in PHP
Well explained tutorial. Keep it up!
|
| All times are GMT +5.5. The time now is 17:42. |