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.

Boost IT visibility and business value

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
KDE releases ice-cream coloured Plasma 5 just in time for summer
Melty but refreshing - popular rival to Mint's Cinnamon's still a work in progress
Leaked Windows Phone 8.1 Update specs tease details of Nokia's next mobes
New screen sizes, dual SIMs, voice over LTE, and more
Secure microkernel that uses maths to be 'bug free' goes open source
Hacker-repelling, drone-protecting code will soon be yours to tweak as you see fit
Mozilla keeps its Beard, hopes anti-gay marriage troubles are now over
Plenty on new CEO's todo list – starting with Firefox's slipping grasp
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
prev story

Whitepapers

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.
Consolidation: The Foundation for IT Business Transformation
In this whitepaper learn how effective consolidation of IT and business resources can enable multiple, meaningful business benefits.
Backing up Big Data
Solving backup challenges and “protect everything from everywhere,” as we move into the era of big data management and the adoption of BYOD.
Boost IT visibility and business value
How building a great service catalog relieves pressure points and demonstrates the value of IT service management.
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.