Difference between revisions of "Generating XML output"

From Joomla! Documentation

 
Line 11: Line 11:
  
 
// Output root element.
 
// Output root element.
echo '<root>' . "\n";
+
echo '<root>'."\n";
  
 
// Output the data.
 
// Output the data.
echo "\t" . '<items>' . "\n";
+
echo "\t".'<items>'."\n";
if (!empty( $data )) {
+
if(!empty($data)){
foreach ($data as $datum) {
+
    foreach($data as $datum){
echo "\t\t" . '<item>' . "\n";
+
          echo "\t\t".'<item>'."\n";
foreach ($datum as $key => $value) {
+
          foreach ($datum as $key => $value) {
echo "\t\t\t".'<'.$key.'>'.htmlspecialchars($value).'</'.$key.'>'."\n";
+
              echo "\t\t\t".'<'.$key.'>'.htmlspecialchars($value).'</'.$key.'>'."\n";
}
+
          }
echo "\t\t" . '</item>' . "\n";
+
          echo "\t\t".'</item>'."\n";
}
+
    }
 
}
 
}
echo "\t" . '</items>' . "\n";
+
echo "\t".'</items>'."\n";
  
 
// Terminate root element.
 
// Terminate root element.
echo '</root>' . "\n";
+
echo '</root>'."\n";
 
</source>
 
</source>
 
Note that the data should be passed through [[php:htmlspecialchars|htmlspecialchars]] to ensure that HTML characters are properly escaped.
 
Note that the data should be passed through [[php:htmlspecialchars|htmlspecialchars]] to ensure that HTML characters are properly escaped.

Latest revision as of 18:37, 5 November 2011

Joomla supports a simple and quite efficient class, JSimpleXML, which can be used to generate XML output in, for example, an Ajax implementation. However, using an object-based XML generator suffers from the drawback that it tends to be slow and memory-intensive, even though JSimpleXML is a lightweight implementation. Unless your requirements are particularly complex, directly outputting XML in string form will result in faster response times and a lower server footprint.

For example, the following code will output an XML document consisting of a root element, called <root>, containing an <items> element which itself contains one or more <item> elements with the actual data. You will need to adjust the code to cope with your particular data requirements.

<?php
$document =& JFactory::getDocument();
$document->setMimeEncoding( 'text/xml' );

// Output XML header.
echo '<?xml version="1.0" encoding="UTF-8" ?>' . "\n";

// Output root element.
echo '<root>'."\n";

// Output the data.
echo "\t".'<items>'."\n";
if(!empty($data)){
     foreach($data as $datum){
          echo "\t\t".'<item>'."\n";
          foreach ($datum as $key => $value) {
               echo "\t\t\t".'<'.$key.'>'.htmlspecialchars($value).'</'.$key.'>'."\n";
          }
          echo "\t\t".'</item>'."\n";
     }
}
echo "\t".'</items>'."\n";

// Terminate root element.
echo '</root>'."\n";

Note that the data should be passed through htmlspecialchars to ensure that HTML characters are properly escaped.

Note also that in this example some attempt has been made to "pretty print" the XML output with tabs and carriage returns. This is not strictly necessary and these extra characters can be removed if you prefer.