-
Notifications
You must be signed in to change notification settings - Fork 0
DOM library
[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]Installation[/h3]
This is easy - put it in your /application/libraries/ folder.
[h3]Documentation[/h3]
The functions are easy to use. First you must create a new DOMDocument:
[CODE] // First things first, load it! $this->load->library('domlib'); // Assigning to a var makes it easier for appending elements $doc = $this->domlib->domNew(false); [/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] // Assigning to a var makes it easier for appending elements $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; the third parameter sets the [b]value[/b] of the element.
For regular elements you would do this:
[CODE] // Assigning to a var makes it easier for appending elements $somelement = $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)
Now say you wanted an attribute on your element, you would do this:
[CODE] // No need to assign to a variable, as we won't be appending elements to an attribute $this->domlib->domAttribute('id', $somelement, 'uniqueId'); [/CODE]
Now to output all of this as an XML file we would do this:
[CODE] $this->domlib->save('example.xml'); [/CODE]
The parameter is simply a string for the name (including extension) you wish to save the Document as or to. If it does not exist it will create it, if it does exist it will overwrite it (if it has the permissions to do so).
This, so far, is what our document on output would look like if you opened up the XML file: [CODE]
<?xml version="1.0" encoding="ISO-8859-1"?> This is an element value!
[/CODE]
[h3]Download and further information[/h3] 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]