Home arrow PHP arrow Page 4 - 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 Example 2
(Page 4 of 6 )

In example one, the elements in our XML document didn't contain any attributes. Let's parse some XML that contains attributes:

<?php

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

$theXML .= " <people>";

$theXML .= " <person id='13' dob='13/07/82'>";

$theXML .= " Mitchell Harper";

$theXML .= " </person>";

$theXML .= " <person id='14' dob='20/08/1960'>";

$theXML .= " Bodger Dodger";

$theXML .= " </person>";

$theXML .= " </people>";



$xmlDoc = xmldoc($theXML);

$root = $xmlDoc->root();

$people = $root->children();



while($person = array_shift($people))

{

if($person->name == "person")

{

echo "<b>Person Found:</b><br> ";

echo "Name: " . $person->content . "<br>";

echo "Id: " . $person->getattr("id") . "<br>";

echo "DOB: " . $person->getattr("dob") . "<br><br>";

}

}

?>


If you copy-paste this code into a file named "example2.php" and run it through your browser, you'll get the following output:

Retrieving attributes is easy with DOMXML

As you can see, our XML string contains the details of two people, each persons details were stored in a <person> element.

Parsing the XML is a little different this time. We load and parse the XML string as normal, however we assign each child node of the documents root element, <people>, to an associative array in the $people variable:

$xmlDoc = xmldoc($theXML);

$root = $xmlDoc->root();

$people = $root->children();


We use the $people associative array to loop through each of the root elements classes that it contains. The "name" key/value pair returns the name of the current node that we are working with. We only want to work with <person> nodes, so we use a simple "if" block to take care of that:

if($person->name == "person")

Each <person> node contains two attributes and a child text node, which contains the person's name. To extract the person's id and date of birth attributes, we use the getattr() function, passing it the name of the attribute whose value we're after:

echo "Id: " . $person->getattr("id") . "<br>";

echo "DOB: " . $person->getattr("dob") . "<br><br>";


One import thing to note about the DOMXML parser is that it's very fussy with spaces. For example, if we had this XML string:

<blah> <node/> </blah>

Then each space between the nodes would be treated as a text node, thus our XML document would contain five elements instead of three. To correct this, just remove the spaces between each node:

<blah><node/></blah>

As you can see from the two examples described above, XML nodes are represented as a set of associative arrays and classes through the DOMXML library. Before we move onto the next section however, I strongly suggest that you take a look at this link. It's is part of PHP's manual and contains a tonne of information about using DOMXML from within PHP, including how to create and append nodes, how to access class attributes and how to instantiate new DOMXML classes programmatically.
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