Home arrow PHP arrow Page 3 - Parsing XML With DOMXML And PHP
PHP

Parsing XML With DOMXML And PHP


Extensible Mark-up Language (XML) has just about made its impression everywhere: B2B, B2C, as a replacement for .INI files, to aid with data abstraction and transportation, etc. In this article Mitchell shows us how to use the DOMXML library to parse and extract data from both local and remote XML files with PHP.

Author Info:
By: Mitchell Harper
Rating: 4 stars4 stars4 stars4 stars4 stars / 27
January 10, 2002
TABLE OF CONTENTS:
  1. · Parsing XML With DOMXML And PHP
  2. · What is the DOMXML parsing method?
  3. · Parsing a simple XML document
  4. · Parsing Example 2
  5. · Putting our DOMXML knowledge to good use
  6. · Conclusion

print this article
SEARCH DEVARTICLES

Parsing XML With DOMXML And PHP - Parsing a simple XML document
(Page 3 of 6 )

Example One

Consider the following XML document:

<?xml version="1.0"?>

<website>

<url>http://www.devarticles.com</url>

</website>


To extract the contents of the <url> node, we would use the following PHP code:

<?php

$theXML = "<?xml version='1.0'?>";

$theXML .= " <website><url>";

$theXML .= " http://www.devarticles.com";

$theXML .= " </url></website>";

$xmlDoc = xmldoc($theXML);

$root = $xmlDoc->root();

$node = array_shift($root->children());

echo $node->content;

?>


We start by creating a string that contains our XML. The xmldoc() function creates an object that contains our hierarchically structured and validated XML data. If our XML was not valid, then the xmldoc() function would spit an error, like this:

The xmldoc() function spits an error message

One of the great things about using the DOMXML parser is that you can just as easily load a local or remote file, using the xmldocfile() function instead:

$xmlDoc = xmldocfile("/htdocs/xml/sample.xml");

OR

$xmlDoc = xmldocfile("http://www.mysite.com/sample.xml");

All XML documents have a single root element, which is the starting point of the documents content. In our example, it's the <website> element. We retrieve the root element with the root() function, like this:

$root = $xmlDoc->root();

Once we have access to the root element, we use its children() function to return a list of its child nodes as an associative array. The array_shift() function pops the first item off the array and returns it as an associative array. Its "content" key/value pair will contain the URL that we're after, and we use the echo command to write it to the browser window:

$node = array_shift($root->children());

echo $node->content;

blog comments powered by Disqus
PHP ARTICLES

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials