Feeds

Harness XML with PHP 5 extensions

A time to query

Boost IT visibility and business value

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.

5 things you didn’t know about cloud backup

More from The Register

next story
Why has the web gone to hell? Market chaos and HUMAN NATURE
Tim Berners-Lee isn't happy, but we should be
Apple promises to lift Curse of the Drained iPhone 5 Battery
Have you tried turning it off and...? Never mind, here's a replacement
'Stop dissing Google or quit': OK, I quit, says Code Club co-founder
And now a message from our sponsors: 'STFU or else'
Microsoft boots 1,500 dodgy apps from the Windows Store
DEVELOPERS! DEVELOPERS! DEVELOPERS! Naughty, misleading developers!
Linux turns 23 and Linus Torvalds celebrates as only he can
No, not with swearing, but by controlling the release cycle
Scratched PC-dispatch patch patched, hatched in batch rematch
Windows security update fixed after triggering blue screens (and screams) of death
This is how I set about making a fortune with my own startup
Would you leave your well-paid job to chase your dream?
prev story

Whitepapers

Best practices for enterprise data
Discussing how technology providers have innovated in order to solve new challenges, creating a new framework for enterprise data.
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.
Advanced data protection for your virtualized environments
Find a natural fit for optimizing protection for the often resource-constrained data protection process found in virtual environments.
How modern custom applications can spur business growth
Learn how to create, deploy and manage custom applications without consuming or expanding the need for scarce, expensive IT resources.
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?