Feeds

Harness XML with PHP 5 extensions

A time to query

Internet Security Threat Report 2014

Create a PHP script, validateXML.php. Create a DOMDocument object and load the XML document. Validate the XML document with the XML schema using schemaValidate().

$isValid=$domDocument->schemaValidate("file://C:/PHP/catalog.xsd");

The output from the PHP script is as follows.

XML Document is valid

Query XML with SimpleXML

PHP 5 provides the SimpleXML extension to select XML document nodes and node values with property selectors and array iterators. To select nodes and output node values in the XML document using the SimpleXML extension create a PHP script, simpleXML.php. The SimpleXML extension provides functions simplexml_load_file(string filename) and simplexml_load_string(string xmlDocument) to load an XML document from a file or a string.

$simplexml=simplexml_load_file("file://C:/PHP/catalog.xml");

The simplexml_load_file() converts the XML document to an object of type SimpleXMLElement, and returns the root element in the XML document. As an example, output the value of the title element of the article in the first journal element.

print $simplexml->journal->article->title;

As the $simplexml object represents the root element in the XML document parsed, the value of the title attribute in the root element, catalog, is obtained as shown below.

print   $simplexml['title'];

The attributes of an element may be output using attributes(). For example, obtain the attributes in the root element.

$attributes=$simplexml->attributes();

Output the publisher attribute.

print $attributes['publisher'];

The output is shown below.

The Java XPath API
XML Zone
IBM developerWorks

Choosing a cloud hosting partner with confidence

Next page: Transform XML

More from The Register

next story
Euro Parliament VOTES to BREAK UP GOOGLE. Er, OK then
It CANNA do it, captain.They DON'T have the POWER!
Download alert: Nearly ALL top 100 Android, iOS paid apps hacked
Attack of the Clones? Yeah, but much, much scarier – report
NSA SOURCE CODE LEAK: Information slurp tools to appear online
Now you can run your own intelligence agency
Post-Microsoft, post-PC programming: The portable REVOLUTION
Code jockeys: count up and grab your fabulous tablets
Twitter App Graph exposes smartphone spyware feature
You don't want everyone to compile app lists from your fondleware? BAD LUCK
Microsoft adds video offering to Office 365. Oh NOES, you'll need Adobe Flash
Lovely presentations... but not on your Flash-hating mobe
prev story

Whitepapers

10 ways wire data helps conquer IT complexity
IT teams can automatically detect problems across the IT environment, spot data theft, select unique pieces of transaction payloads to send to a data source, and more.
Getting started with customer-focused identity management
Learn why identity is a fundamental requirement to digital growth, and how without it there is no way to identify and engage customers in a meaningful way.
How to determine if cloud backup is right for your servers
Two key factors, technical feasibility and TCO economics, that backup and IT operations managers should consider when assessing cloud backup.
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 and trust: The backbone of doing business over the internet
Explores the current state of website security and the contributions Symantec is making to help organizations protect critical data and build trust with customers.