. Updated Daily. Editions SDA India   SDA Indonesia
JAX Asia 2008 - Conference for Enterprise Java, SOA, Spring, Web Services, Ajax, Agile and more
BUSINESS ENTERPRISE SOLUTIONS ARCHITECTURE INFORMATION SECURITY WIRELESS & MOBILITY DATA & STORAGE DEVELOPMENT HARDWARE













News

Thursday, 25 January 2007

Article: Build a XML Document with the PHP DOM

 

 

An article in Builderau web site provides a tutorial on how to build a XML document with the PHP DOM. Over the course of this article, the author introduces you to the main functions in this API, showing you how to programmatically generate a complete XML document from scratch and save it to disk with the help of illustrative listings.

The article is divided into the following parts:

  • Creating the Doctype declaration: this pointer starts with the XML declaration. It claims that this step in PHP is fairly simpler as it requires you to instantiate an object of the DOMDocument class and supply it with a version number. He describes this with an example script in Listing A.
  • Adding elements and text nodes: the article informs that the real power of XML comes from its elements and the content they enclose. If the basic DOMDocument is initialized, this becomes simple. Two steps to the process is defined. In the first step by using DOMDocument object's createElement() or createTextNode() method with the element name or text content will help in the creation of a new object corresponding to the element or text node. The second step appends 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. The article provides an example in Listing B to make it clearer.
  • Adding Attributes: this point informs that you can add qualifying information to your elements, through the use of attributes. With the PHP DOM API, attributes are added in a two-step process: first by creating 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 provides an example to understand the process.
  • Adding CDATA blocks and processing instructions: it says though not used 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 to facilitate better understanding.
  • Saving the results: once the tree is formed , 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 explains the article. Listing F carries an example to make it logically viable.
 

Read the Article

 
 
print save email comment

print

save

email

comment

 
 

Search SDA Asia

Free eNewsletter

SDA Asia Magazine Free Download
 
 
 
Copyright @ 2008 SDA Asia Magazine - All Right Reserved Privacy Policy | Terms of Use