Skip to content
World Wide Web Server edited this page Jul 4, 2012 · 29 revisions

Category:Libraries

[h3]DOMLib[/h3]

This library was built to ease the use of the PHP DOM object. I found it too tedious to always be writing this: [CODE] $this->doc = new DOMDocument ('1.0', 'iso-8859-1'); $this->doc->preserveWhiteSpace = false; $this->doc->formatOutput = true; $root = $this->doc->createElement('root'); $this->doc->appendChild($root); [/CODE]

An example of the above using this library would be: [CODE] // Must load the library properly, example: $this->load->library('domlib'); // In controller $doc = $this->domlib->domNew(false); // Creates new DOMDocument object. $this->domlib->domRoot('root', $doc); // Creates root element and appends to the document. [/CODE]

The XML version, encoding, and formatOutput are set as constants.

-- XML_VERSION = '1.0'; -- XML_ENCODING = 'iso-8859-1'; -- FORMAT_OUTPUT = 'TRUE';

This library includes pretty much all of the most common functions - I may include full DOM functionality in the near future.

You can create these types of nodes with the library:

-- Standard XML elements -- Element text (must be appended [b]TO[/b] an element) -- Element attributes (must be appended [b]TO[/b] an element) -- XML comments <!-- comment --> -- CDATA sections [[CDATA[ -- Transforms with XSLT and optional parameters -- Output DOMDocument tree as an XML string to browser -- Output DOMDocument tree as a file -- Load an XML document for processing

[h3]Documentation[/h3]

The functions are easy to use. First you must create a new DOMDocument: [CODE $doc = $this->domlib->domNew(false); //Assigning to a var makes it easier for appending elements [/CODE] The parameter sets the boolean value for the 'preserveWhitespace' function when Document output occurs.

For root elements (to be valid XML you must have a root element!): [CODE] $root = $this->domlib->domRoot('root', $doc); [/CODE]

The first parameter sets the name of the element, and the second parameter appends the root element to the DOMDocument.

For regular elements you would do this: [CODE] $this->domlib->domElement('somelement', $root, 'This is an element value'); [/CODE]

The first parameter sets the name of your element, second parameter appends it to any parent elements you choose (at the moment, the only one we have is the root element) and so far the document on output would look like this: [CODE] <?xml version="1.0" encoding="ISO-8859-1"?> This is an element value! [/CODE] You can download it here: [url=http://www.orchadea.com/dl/domlib.php.zip]domlib.php.zip[/url] [i]Note: you should save it by right-clicking and selecting 'Save link as'[/i]

The forum topic is here: [url=http://codeigniter.com/forums/viewthread/50082/]DOMLib thread[/url]

Clone this wiki locally