This is a convenience attribute that allows direct access to the
child node that is the document element of the document.

documentURI

The location of the document or NULL if undefined.

encoding

Encoding of the document, as specified by the XML declaration. This
attribute is not present in the final DOM Level 3 specification, but
is the only way of manipulating XML document encoding in this
implementation.

User Contributed Notes 13 notes

Showing a quick example of how to use this class, just so that new users can get a quick start without having to figure it all out by themself. ( At the day of posting, this documentation just got added and is lacking examples. )

<?php

// Set the content type to be XML, so that the browser will recognise it as XML.header( "content-type: application/xml; charset=ISO-8859-15" );

If you want your PHP->DOM code to run under the .xml extension, you should set your webserver up to run the .xml extension with PHP ( Refer to the installation/configuration configuration for PHP on how to do this ).

is NOT the same as this:<?php// Will NOT work.$xml = new DOMDocument( "1.0", "ISO-8859-15" );$xml_album = new DOMElement( "Album" );$xml_track = new DOMElement( "Track" );$xml_album->appendChild( $xml_track );$xml->appendChild( $xml_album );?>

//Loop through each <a> tag in the dom and add it to the link array
foreach($xml->getElementsByTagName('a') as $link) {
$links[] = array('url' => $link->getAttribute('href'), 'text' => $link->nodeValue);
}

//Loop through each <a> tag in the dom and add it to the link array foreach ($xml->getElementsByTagName('a') as $link) { $url = $link->getAttribute('href'); if (!empty($url)) { $links[] = $link->getAttribute('href'); } }

After seeing many complaints about certain DOMDocument shortcomings, such as bad handling of encodings and always saving HTML fragments with <html>, <head>, and DOCTYPE, I decided that a better solution is needed.

- SmartDOMDocument inherits from DOMDocument, so it's very easy to use - just declare an object of type SmartDOMDocument instead of DOMDocument and enjoy the new behavior on top of all existing functionality (see example below).

- saveHTMLExact() - DOMDocument has an extremely badly designed "feature" where if the HTML code you are loading does not contain <html> and <body> tags, it adds them automatically (yup, there are no flags to turn this behavior off).Thus, when you call $doc->saveHTML(), your newly saved content now has <html><body> and DOCTYPE in it. Not very handy when trying to work with code fragments (XML has a similar problem).SmartDOMDocument contains a new function called saveHTMLExact() which does exactly what you would want - it saves HTML without adding that extra garbage that DOMDocument does.

- encoding fix - DOMDocument notoriously doesn't handle encoding (at least UTF-8) correctly and garbles the output.SmartDOMDocument tries to work around this problem by enhancing loadHTML() to deal with encoding correctly. This behavior is transparent to you - just use loadHTML() as you would normally.

- SmartDOMDocument Object As String - you can use a SmartDOMDocument object as a string which will print out its contents.For example:<?phpecho "Here is the HTML: $smart_dom_doc";?>