Feeds

Harness XML with PHP 5 extensions

A time to query

Security and trust: The backbone of doing business over the internet

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.

Providing a secure and efficient Helpdesk

Next page: Parse the XML

More from The Register

next story
New 'Cosmos' browser surfs the net by TXT alone
No data plan? No WiFi? No worries ... except sluggish download speed
'Windows 9' LEAK: Microsoft's playing catchup with Linux
Multiple desktops and live tiles in restored Start button star in new vids
iOS 8 release: WebGL now runs everywhere. Hurrah for 3D graphics!
HTML 5's pretty neat ... when your browser supports it
Mathematica hits the Web
Wolfram embraces the cloud, promies private cloud cut of its number-cruncher
Mozilla shutters Labs, tells nobody it's been dead for five months
Staffer's blog reveals all as projects languish on GitHub
'People have forgotten just how late the first iPhone arrived ...'
Plus: 'Google's IDEALISM is an injudicious justification for inappropriate biz practices'
SUSE Linux owner Attachmate gobbled by Micro Focus for $2.3bn
Merger will lead to mainframe and COBOL powerhouse
iOS 8 Healthkit gets a bug SO Apple KILLS it. That's real healthcare!
Not fit for purpose on day of launch, says Cupertino
prev story

Whitepapers

Secure remote control for conventional and virtual desktops
Balancing user privacy and privileged access, in accordance with compliance frameworks and legislation. Evaluating any potential remote control choice.
WIN a very cool portable ZX Spectrum
Win a one-off portable Spectrum built by legendary hardware hacker Ben Heck
Storage capacity and performance optimization at Mizuno USA
Mizuno USA turn to Tegile storage technology to solve both their SAN and backup issues.
High Performance for All
While HPC is not new, it has traditionally been seen as a specialist area – is it now geared up to meet more mainstream requirements?
The next step in data security
With recent increased privacy concerns and computers becoming more powerful, the chance of hackers being able to crack smaller-sized RSA keys increases.