This is a very easy to use XML writer library I developed to write XLM for communication with a Flash app. But it can be used to generate any well-formed XML document. Thought I’d share. Any comments welcome, of course 😊
EDIT: Most recent version is on Github: https://github.com/accent-interactive/xml_writer
It can be used to create any XML, not matter how deep. Every branche and node can have one or more attributes, if needed. Need CDATA? Just set a parameter to true. The library prints XML directly to screen, setting XML headers first, or it can return the XML as a string. Just place the library in applications/library and you’re good to go.
Basically, you can add two types of data:
* branches - you need to start a new branch and end it before adding the next branch
* nodes - nodes are children to a branch. You do not have to start and end a node, just add it.
Example use: place this in your controller (of course you would normally collect the data from a database, making the code much cleaner than it is in this example)
CONTROLLER
function write_xml() {
// Load XML writer library
$this->load->library('MY_Xml_writer');
// Initiate class
$xml = new MY_Xml_writer;
$xml->setRootName('my_store');
$xml->initiate();
// Start branch 1
$xml->startBranch('cars');
// Set branch 1-1 and its nodes
$xml->startBranch('car', array('country' => 'usa')); // start branch 1-1
$xml->addNode('make', 'Ford');
$xml->addNode('model', 'T-Ford', array(), true);
$xml->endBranch();
// Set branch 1-2 and its nodes
$xml->startBranch('car', array('country' => 'Japan')); // start branch
$xml->addNode('make', 'Toyota');
$xml->addNode('model', 'Corolla', array(), true);
$xml->endBranch();
// End branch 1
$xml->endBranch();
// Start branch 2
$xml->startBranch('bikes'); // start branch
// Set branch 2-1 and its nodes
$xml->startBranch('bike', array('country' => 'usa')); // start branch
$xml->addNode('make', 'Harley-Davidson');
$xml->addNode('model', 'Soft tail', array(), true);
$xml->endBranch();
// End branch 2
$xml->endBranch();
// Print the XML to screen
$xml->getXml(true);
}This will produce the following XML:
<?xml version="1.0" encoding="UTF-8"?>
<my_store>
<cars>
<car country="usa">
<make>Ford</make>
<model><![CDATA[T-Ford]]></model>
</car>
<car country="Japan">
<make>Toyota</make>
<model><![CDATA[Corolla]]></model>
</car>
</cars>
<bikes>
<bike country="usa">
<make>Harley-Davidson</make>
<model><![CDATA[Soft tail]]></model>
</bike>
</bikes>
</my_store>EXTRA OPTIONS
Before you call initiate() you can set some XML settings if you wish:
$xml->setRootName($string); // Set the value of the root tag for this XML. Defaults to 'root'
$xml->setXmlVersion($string); // Set the value of the XMl version for this XML. Defaults to '1.0'
$xml->setCharSet($string); // Set the character set for this XML. Defaults to 'UTF-8'
$xml->setIndentStr($string); // Set indenting for every new node in this XML. Defaults to ' '.
$xml->setXsltFilePath($string); // Set the XSLT filepath for this XML. This should be an absolute URL. Defaults to '' (no XSL)USE A VIEW
If you do not wish to print, but rather send the XML string to a view file:
$data['xml'] = $xml->getXml();
$this->load->view('xml_template', $data);LIBRARY SOURCE CODE
The library code will be in the following post below.