Jump to content

Recommended Posts

While working on project, we needed good and simple solutions for creating XML data. SimpleXML have meny limitations, and doesn't suited our needs, DOM needs lots of code for simpliest things and is uncomfortable.

Using magic methods and ArrayAccess interface we created class with SimpleXML-like API, extending it with new functions and DOM methods and fields. EasyXML dosen't use lots of memory because it's created on-fly (only where some node is requested). In theory size of XML data is only limited by PHP and DOM capabilities.

Download:
http://shaxbee.superhost.pl/code/2007/01/EasyXML.class.zip
http://shaxbee.superhost.pl/code/2007/01/EasyXML-doc/class_e asy_x_m_l.html

Some usage examples:
[code]
$xml = new EasyXML('<root><check>someValue</check></root>'); // creates object in SimpleXML manner
// lub
$xml = EasyXML::loadXMLFile('file.xml'); // loads a file and returns EasyXML object

$xml->root->check = 'someOtherValue'; // sets contents of given tag (creates DOM nodes)
$xml->root['attribute'] = 'attributeValue'; // sets attribute value

unset($xml->root['attribute']); // removes attribute

$domain = $xml->root->check->appendChild('domain');
$otherDomain = $xml->root->check->appendChild('domain', 'value'); // creates another child

$domain['name'] = 'aqq.pl';

unset($xml->root->check->domain); // deletes every check childs with name 'domain'

$xml->root->firstChild; // access for DOM fields ...
$xml->root->hasAttribute('test'); // ... and methods

$xml->root->check = '<domain>aqq</domain><domain>bqq</domain>'; // you can simple set node content (DOM nodes)

$data = serialize($xml); // working (SimpleXML :/) serialization
$xml->getArray(); // returns node as array, preserving structure of nodes

$xml->xpath('//check'); // xpath is also working

echo $xml; // (string) $xml returns XML data
[/code]

Feel free to play with that class, i'm waiting for feedback :)
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.