Feeds

Harness XML with PHP 5 extensions

A time to query

Securing Web Applications Made Simple and Scalable

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

Bridging the IT gap between rising business demands and ageing tools

Next page: Transform XML

More from The Register

next story
NO MORE ALL CAPS and other pleasures of Visual Studio 14
Unpicking a packed preview that breaks down ASP.NET
Cheer up, Nokia fans. It can start making mobes again in 18 months
The real winner of the Nokia sale is *drumroll* ... Nokia
Put down that Oracle database patch: It could cost $23,000 per CPU
On-by-default INMEMORY tech a boon for developers ... as long as they can afford it
Google shows off new Chrome OS look
Athena springs full-grown from Chromium project's head
Apple: We'll unleash OS X Yosemite beta on the MASSES on 24 July
Starting today, regular fanbois will be guinea pigs, it tells Reg
HIDDEN packet sniffer spy tech in MILLIONS of iPhones, iPads – expert
Don't panic though – Apple's backdoor is not wide open to all, guru tells us
DARPA-derived secure microkernel goes open source tomorrow
Hacker-repelling, drone-protecting code will soon be yours to tweak as you see fit
prev story

Whitepapers

Designing a Defense for Mobile Applications
Learn about the various considerations for defending mobile applications - from the application architecture itself to the myriad testing technologies.
Implementing global e-invoicing with guaranteed legal certainty
Explaining the role local tax compliance plays in successful supply chain management and e-business and how leading global brands are addressing this.
Top 8 considerations to enable and simplify mobility
In this whitepaper learn how to successfully add mobile capabilities simply and cost effectively.
Seven Steps to Software Security
Seven practical steps you can begin to take today to secure your applications and prevent the damages a successful cyber-attack can cause.
Boost IT visibility and business value
How building a great service catalog relieves pressure points and demonstrates the value of IT service management.