Using the JSimpleXML Parser Utility

  • PDF

Using the JSimpleXML Object in Joomla

How to use the JSimpleXML and JSimpleXMLElement for parsing XML files in Joomla

For simple XML parsing the JSimpleXML and JSimpleXMLElement objects are a great way to quickly read in XML files and start working with them within your code.

To access an XML File you will need to do the following:

Create an XML parser instance, by calling the JFactory Class, getXMLParser method:

$xml = JFactory::getXMLParser('Simple');
 

// The parameter 'Simple' is used to create an object of the JSimpleXML class;other parameters include 'RSS' to create a SimplePie RSS parser and

// 'DOM' will return the Domit parser.

Next set define the file location and read the file into an XML object:

$xmlfile= JPATH_SITE .DS.'modules'.DS.'mod_readxml'.DS.'file'.DS.'book_sample.xml';
$xml->loadFile($xmlfile)

We use JPATH_SITE here because we are looking for a file.  If we were looking for a web based resource we would use JURI::base() instead. 

Now you have the file loaded into you code what can you do with it now?

Let's say we have some simple XML like this:

<?xml version="1.0"?>
<catalog>
 <book id="bk101">
   <author>Gambardella, Matthew</author>
   <title>XML Developer's Guide</title>
 </book>
 <book id="bk102">
   <author>Ralls, Kim</author>
   <title>Midnight Rain</title>
 </book>
</catalog>
 

We can write out each book's information using code like this:

// Create a JSimpleXML object and then parse the file
$xml = JFactory::getXMLParser('Simple');
if ($xml->loadFile($xmlfile)) {
// We can now step through each element of the file 
foreach( $xml->document->book as $book ) {
   $author = $book->getElementByPath('author');
   $title = $book->getElementByPath('title');
   echo "Author: {$author->data()}<br/>";
   echo "Title: {$title->data()}<br/><br/>";
   }
 }
 else {
   $error_msg = "File Open Error: file " . $xmlfile;
   echo $error_msg;
}

So this steps through our parsed XML and writes out some basic information.  How would we manipulate this XML further, how can we add and save the content?

To add new information to our XML we can use the addChild method by inserting the following to our code:

  $book->addChild('publisher');
  $publisher = $book->getElementByPath('publisher');
  $publisher->setData('acmePub');

This code works by first adding a child element of type publisher to our XML; we then retrieve the JSimpleXMLElement 'publisher' from the XML and set the data using the JSimpleXMLElement setData method.  The important thing to remember is that when working with parsed XML data most of your methods are from the JSimpleXMLElement class.

Our final step is writing out the modified XML - we do this using the toString method of JSimpleXMLElement, which will output a well formatted XML string.

Our complete working code then looks like this:

// no direct access
defined('_JEXEC') or die;
 
$xmlfile = JPATH_SITE . DS . 'modules' . DS . 'mod_read_xml_ds' . DS. 'file' . DS .'booksample.xml';
 
echo $xmlfile;  // For debugging only
 
$xml = JFactory::getXMLParser('simple');
 
if ( $xml->loadFile($xmlfile)){
 foreach($xml->document->book as $book){
   $author = $book->getElementByPath('author');
   $title = $book->getElementByPath('title');
   $book->addChild('publisher');
   $publisher = $book->getElementByPath('publisher');
   $publisher->setData('acmePub');
   echo $book->toString();
   }
 }
 else{
   $error_msg = "File Open Error: file " . $xmlfile;
   echo $error_msg;    
 }

Links to JSimpleXML Resources

http://docs.joomla.org/JFactory/getXMLParser

JSimpleXMLElement