Feeds

Harness XML with PHP 5 extensions

A time to query

Providing a secure and efficient Helpdesk

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.

Internet Security Threat Report 2014

Next page: Parse the XML

More from The Register

next story
Google+ goes TITSUP. But WHO knew? How long? Anyone ... Hello ...
Wobbly Gmail, Contacts, Calendar on the other hand ...
UNIX greybeards threaten Debian fork over systemd plan
'Veteran Unix Admins' fear desktop emphasis is betraying open source
Preview redux: Microsoft ships new Windows 10 build with 7,000 changes
Latest bleeding-edge bits borrow Action Center from Windows Phone
Microsoft promises Windows 10 will mean two-factor auth for all
Sneak peek at security features Redmond's baking into new OS
Netscape Navigator - the browser that started it all - turns 20
It was 20 years ago today, Marc Andreeesen taught the band to play
DEATH by PowerPoint: Microsoft warns of 0-day attack hidden in slides
Might put out patch in update, might chuck it out sooner
Redmond top man Satya Nadella: 'Microsoft LOVES Linux'
Open-source 'love' fairly runneth over at cloud event
prev story

Whitepapers

Forging a new future with identity relationship management
Learn about ForgeRock's next generation IRM platform and how it is designed to empower CEOS's and enterprises to engage with consumers.
Why and how to choose the right cloud vendor
The benefits of cloud-based storage in your processes. Eliminate onsite, disk-based backup and archiving in favor of cloud-based data protection.
Three 1TB solid state scorchers up for grabs
Big SSDs can be expensive but think big and think free because you could be the lucky winner of one of three 1TB Samsung SSD 840 EVO drives that we’re giving away worth over £300 apiece.
Reg Reader Research: SaaS based Email and Office Productivity Tools
Read this Reg reader report which provides advice and guidance for SMBs towards the use of SaaS based email and Office productivity tools.
Security for virtualized datacentres
Legacy security solutions are inefficient due to the architectural differences between physical and virtual environments.