Writing XML Files with DOMIT! | Print |  E-mail
Creating XML files in Mambo is easy with the DOMIT! library.

The first steps are to include the library in your code and create an XML document to work with.
require_once( $mosConfig_absolute_path
    . '/includes/domit/xml_domit_lite_include.php' );
$xmlDoc =& new DOMIT_Lite_Document();
We use the DOMIT_Lite_Document class because it has most of the basic features we need.  If you needed advanced features, like namespace support, then you would use the DOMIT_Document class.

The next thing we need to do is to set the root tag for the xml object.  Let's imagine we are writing our own installer xml file.  The root tag is called mosinstall and it will have an attribute called version that we will assign the current Mambo version number to.
global $_VERSION;
$version = $_VERSION->RELEASE . '.' . $_VERSION->DEV_LEVEL;
 
$elem = new DOMIT_Lite_Element( 'mosinstall' );
$elem->setAttribute( 'version', $version );
 
$xmlDoc->setDocumentElement( $elem );
We make each new tag by creating a DOMIT_Lite_Element object ($elem) and pass it the name of the tag.  To set the version attribute we use the setAttribute method and pass it the name and then value of the attribute.  Finally, we pass our new object ($elem) to the setDocumentElement method of our xml object ($xmlDoc).

One thing you must never do is try and dump the structure of the xml object using a function like print_r.  There are circular references within the object that will cause PHP to spin in a loop.  To see what the xml object looks like we can used either the toString method or the toNormalizedString method.  The second method formats the output neatly but is otherwise the same as the first.  Passing either method an argument of true will make the output html-safe.
echo $xmlDoc->toNormalizedString( true );
Calling this function should output:

<mosintall version="4.5.2" />

Let's move on to add some child elements to the root tag.  First we create a shortcut variable ($root) to the documentElement property of the xml object.  We can create new elements using the createElement method in the xml object.
$root =& $xmlDoc->documentElement;
 
$name =& $xmlDoc->createElement( 'name' );
$name->setText( 'Mambo Man' );
$root->appendChild( $name );
You will see that have created a new name tag and placed it in a variable called $name.  This tag has no text but does contain some character data (plain text).  To set the data within the tag we call the setText method and pass it the desired text.  Following that we use the appendChild method to add the new name tag to the end of the child tags already defined.  Of course don't have any yet so it is simply added.

If we now output the contents of the object with the toNormalizedString method it would look like this:

<mosinstall version="4.5.2">
  <name>Mambo Man</name>
</mosinstall>


All we have to do now is save the xml file.  Set the declaration for the xml file using the setXMLDeclaration method and then use the saveXML method, pass it the file name.  The second argument is set to true to provided a neatly indented file.
$xmlDoc->setXMLDeclaration(
    '<?xml version="1.0" encoding="iso-8859-1"?>' );
$xmlDoc->saveXML( 'installer.xml', true );

Last Updated ( Wednesday, 16 February 2005 )