Feeds

Harness XML with PHP 5 extensions

A time to query

Combat fraud and increase customer satisfaction

Parse the XML

To parse the XML document catalog.xml create a PHP file, parseXML.php. Create a DOMDocument object. Load the example XML document using load(String xmlFile). Another function loadXML(string xmlDocument) may be used to load an XML document from a String.

$domDocument->load("file://C:/PHP/catalog.xml");

As an example, obtain all the title nodes in the XML document using getElementsByTagName(string tagName). Iterate over the node list and output the title elements' values.

$titleNodeList=$domDocument->getElementsByTagName("title");
   for($i=0; $I<$titleNodeList->length;$i++)
   {
     echo $titleNodeList->item($i)->nodeValue;
     print "<br/>\n";
     
    }

The output from the PHP script is shown below.

The Java XPath API
JAXP validation

Navigate XML with XPath

To navigate the XML document using XPath create a PHP script, xpath.php. Create a DOMDocument object and load the example XML document with load(string filename).

The DOMXPath class is used to evaluate an XPath expression in the context of an XML document node. Create a DOMXPath object.

$domXPath=new DOMXPath($domDocument);

XPath expressions may be evaluated with query(). Parameter of type DOMNode is optional in query(). By default the context node is the root element. As an example, retrieve the values of all the title elements.

$titles=$domXPath->query("/catalog/journal/article/title");

The query() returns a DOMNodeList. Iterate over the DOMNodeList to output the values of the title elements.

foreach ($titles as $title) {

    echo 'Title: ', $title->firstChild->nodeValue;
    print "<br/>\n";
}

The output from the xpath.php script is shown below.

Title: The Java XPath API
Title: JAXP validation

Validate your XML

In this section we validate the example document with an XML schema, catalog.xsd. The schema validation functions, schemaValidate(string filename) and schemaValidateSource(string schema), return a boolean value indicating if an XML document is valid. Limitations of the schema validation functions are that only one schema may be specified for validation and that the detail of the validation error is not output.

High performance access to file storage

More from The Register

next story
This time it's 'Personal': new Office 365 sub covers just two devices
Redmond also brings Office into Google's back yard
Batten down the hatches, Ubuntu 14.04 LTS due in TWO DAYS
Admins dab straining server brows in advance of Trusty Tahr's long-term support landing
Microsoft lobs pre-release Windows Phone 8.1 at devs who dare
App makers can load it before anyone else, but if they do they're stuck with it
Half of Twitter's 'active users' are SILENT STALKERS
Nearly 50% have NEVER tweeted a word
Internet-of-stuff startup dumps NoSQL for ... SQL?
NoSQL taste great at first but lacks proper nutrients, says startup cloud whiz
Windows 8.1, which you probably haven't upgraded to yet, ALREADY OBSOLETE
Pre-Update versions of new Windows version will no longer support patches
Microsoft TIER SMEAR changes app prices whether devs ask or not
Some go up, some go down, Redmond goes silent
Red Hat to ship RHEL 7 release candidate with a taste of container tech
Grab 'near-final' version of next Enterprise Linux next week
Ditch the sync, paddle in the Streem: Upstart offers syncless sharing
Upload, delete and carry on sharing afterwards?
prev story

Whitepapers

Designing a defence for mobile apps
In this whitepaper learn the various considerations for defending mobile applications; from the mobile application architecture itself to the myriad testing technologies needed to properly assess mobile applications risk.
3 Big data security analytics techniques
Applying these Big Data security analytics techniques can help you make your business safer by detecting attacks early, before significant damage is done.
Five 3D headsets to be won!
We were so impressed by the Durovis Dive headset we’ve asked the company to give some away to Reg readers.
The benefits of software based PBX
Why you should break free from your proprietary PBX and how to leverage your existing server hardware.
Securing web applications made simple and scalable
In this whitepaper learn how automated security testing can provide a simple and scalable way to protect your web applications.