DOMDocument->loadXML()

(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.

参数

source

The string containing the XML.

返回值

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

范例

例子 1. Creating a Document

<?php
$doc
= DOMDocument::loadXML('<root><node/></root>');
echo
$doc->saveXML();

$doc = new DOMDocument();
$doc->loadXML('<root><node/></root>');
echo
$doc->saveXML();
?>


add a note add a note User Contributed Notes
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;
$childDocument->loadXML(..XML-Chunk..);
$mNewNode = $mainDOcument->importNode($childDocument->documentElement, true);
$ParentNode->appendChild($mNewNode);
$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.

Georg
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>';
 print_r($e);
 echo '</pre>';
}

Blaine
http://blainegarrett.com