(no version information, might be only in CVS)

DOMDocument->loadXML() --  Load XML from a string


class DOMDocument {

bool loadXML ( string source [, int options] )


Loads an XML document from a string.

This method may also be called statically to load and create a DOMDocument object. The static invocation may be used when no DOMDocument properties need to be set prior to loading.



The string containing the XML.


如果成功则返回 TRUE,失败则返回 FALSE


例子 1. Creating a Document

= DOMDocument::loadXML('<root><node/></root>');

$doc = new DOMDocument();

add a note add a note User Contributed Notes
Gavin Sinai gsinai at gmx dot net
30-Aug-2006 10:34
loadXml reports an error instead of throwing an exception when the xml is not well formed. This is annoying if you are trying to to loadXml() in a try...catch statement. Apparently its a feature, not a bug, because this conforms to a spefication.

If you want to catch an exception instead of generating a report, you could do something like

function HandleXmlError($errno, $errstr, $errfile, $errline)
   if (
$errno==E_WARNING && (substr_count($errstr,"DOMDocument::loadXML()")>0))
throw new DOMException($errstr);

$dom = new DOMDocument();


Returning false in function HandleXmlError() causes a fallback to the default error handler.
mp at webfactory dot de
08-Aug-2006 11:26
While loadXML() expects its input to have a leading XML processing instruction to deduce the encoding used, there's no such concept in (non-XML) HTML documents. Thus, the libxml library underlying the DOM functions peeks at the <META> tags to figure out the encoding used.

earth at anonymous dot com
12-Apr-2006 05:28
Note that loadXML crops off beginning and trailing whitespace and linebreaks.

When using loadXML and appendChild to add a chunk of XML to an existing document, you may want to force a linebreak between the end of the XML chunk and the next line (usually a close tag) in the output file:

$childDocument = new DOMDocument;
$childDocument>preserveWhiteSpace = true;
$mNewNode = $mainDOcument->importNode($childDocument->documentElement, true);
$ParentNode->appendChild($mainDocument->createTextNode("\\n  ");

Although it is said that DOM should not be used to make 'pretty' XML output, it is something I struggled with to get something that was readable for testing.  Another solution is to use the createDocumentFragment()->appendXML(..XML-Chunk..) instead, which seems not to trim off linebreaks like DOMDocument->loadXML() does.
georg at howen dot de
06-Mar-2006 08:04
Just in case you try to do the same as I did and thought you found a bug:

loadXML() is not a true static function. It is just allowed to be called statically. When called statically within a method of an instantiated DOMDocument object it acts as if the method had been called directly from the object itself rather than statically.

This means that if you call DOMDocument::loadXML() from within an instantiated DOMDocument object, it will not return a new DOMDocument but true/false.

primaryspace at hotmail dot com
10-Aug-2005 04:42
This method replaces any existing document tree already in the object with the document tree contained in the source argument.
blaine at blainegarrett dot com
26-May-2005 12:04
It seems this method does not throw a DOMException if it fails, but rather a regular warning. Thus it makes it hard to fine tune strings to try to get them to be valid XML.

Ideally, there would be a way to figure out why loadXML failed (by way of the exception), but rather only an warning is displayed.  This is a heads up for anyone trying to write their own string_to_xml() functions.

$string = '$$$"$$"&&&&lesund';

try {
   $dom = DOMDocument::loadXML($test_string);
catch(DOMException $e) {
 echo '<pre>';
 echo '</pre>';