PHP: DOMDocument->loadXML() - Manual

downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

DOMDocument->normalizeDocument() DOMDocument->loadHTMLFile()

Last updated: Sat, 27 Jan 2007

view this page in

# DOMDocument->loadXML()

(no version information, might be only in CVS)

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

## Описание

class DOMDocument {
mixed 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` в случае возникновения ошибки. If called statically, returns a DOMDocument.

## Примеры

Пример 1. Creating a Document

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

Пример 2. Static invocation of `loadXML`

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

## Смотрите также

DOMDocument->load()
DOMDocument->save()
DOMDocument->saveXML()

add a note User Contributed Notes
DOMDocument->loadXML()
Gavin Sinai gsinai at gmx dot net
30-Aug-2006 07: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
<?php
function HandleXmlError($errno, $errstr, $errfile, $errline)
{
if ($errno==E_WARNING && (substr_count($errstr,"DOMDocument::loadXML()")>0))
{
throw new DOMException($errstr);
}
else
return false;
}
function XmlLoader($strXml)
{
set_error_handler('HandleXmlError');
$dom = new DOMDocument();
$dom->loadXml($strXml);
restore_error_handler();
return $dom;
}
?>
Returning false in function HandleXmlError() causes a fallback to the default error handler.`
mp at webfactory dot de
08-Aug-2006 08: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.
See http://xmlsoft.org/encoding.html.`
earth at anonymous dot com
12-Apr-2006 02: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 04: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
09-Aug-2005 01: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
25-May-2005 09: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`
add a note
DOMDocument->normalizeDocument() DOMDocument->loadHTMLFile()

Last updated: Sat, 27 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 09:32:46 2007 PST