Harness XML with PHP 5 extensions
A time to query
Posted in Developer, 15th August 2008 15:02 GMT
Free Download - Security Web 2.0
Hands on PHP is one of the most commonly used languages for developing web sites while XML has become an industry standard for exchanging data. Increasingly, web sites use XML to transfer data through web feeds such as RSS and Atom, or through web services.
PHP 5 XML extensions provide support for parsing, transformation, XPath navigation, and schema validation of XML documents. The SimpleXML extension in PHP 5 simplifies node selection by converting an XML document to a PHP object that may be accessed with property selectors and array iterators. The XSL extension in PHP 5 is used to transform an XML document.
In this article, I'll show you how to process an example XML document, catalog.xml using the PHP 5 XML extensions.
First things first, though. Before you go anywhere, you'll need to install PHP 5 in Apache HTTP Server, and activate the XSL extension in the php.ini configuration file.
extension=php_xsl.dll
Restart Apache Server after modifying php.ini.
Create your XML
To create an XML document with the PHP 5 DOM extension create a PHP file, createXML.php , in the C:/Program Files/Apache Group/Apache2/htdocs directory, the document root directory of the Apache server. An XML document in PHP 5 is represented with DOMDocument class. Therefore, create a DOMDocument object. Specify the XML version and encoding in the DOMDocument constructor.
$domDocument = new DOMDocument('1.0','utf-8');
An element is represented with the DOMElement class. Create root element catalog with createElement(). Add the root element to the DOMDocument object with appendChild().
$catalog= $domDocument->createElement("catalog");
$domDocument->appendChild ($catalog);
An attribute in a DOMElement object is represented with the DOMAttr class. Create attribute title with createAttribute(). Set the value of the title attribute using the value property. Add the title attribute to catalog element using setAttributeNode().
$titleAttribute= $domDocument->createAttribute("title");
$titleAttribute->value="XML Zone";
$catalog->setAttributeNode ($titleAttribute);
Create a journal element, including the date attribute, within the catalog element. Add an article element, including sub elements title and author, within the journal element. A text node in an element is represented with the DOMText class. Create a text node using the createTextNode() to set the text of title element.
$titleText= $domDocument->createTextNode("The Java XPath API");
$title->appendChild ($titleText);
Output the XML document created to the browser using saveXML().
$domDocument->saveXML();
Run the PHP script with URL http://localhost/createXML.php. The XML document, catalog.xml, gets generated.
Next page: Parse the XML

An Improved Architecture for High-Efficiency, High-Density Data Centers [WP126]
Implementing Energy Efficient Data Centers [WP114]
Blind SQL Injection [3-2APYM5E]
The Register Guide to Extended Validation
High-level Best Practices in Software Configuration Management
Java and Linux - an open marriage in search of success
How Microsoft blew its own RIA invention
Rock-solid Fedora 10 brings salvation to Ubuntu weary
Hidden recipes for OS X charts and graphs