Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : DOM Functions : DOMDocument->loadXML()

DOMDocument->loadXML()

Load XML from a string ()

DOMDocument{
mixedloadXML(stringsource,
intoptions);

}

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.

Parameters

source

The string containing the XML.

Return Values

Returns TRUE on success or FALSE on failure. If called statically, returns a DOMDocument.

Errors/Exceptions

If an empty string is passed as the source, a warning will be generated. This warning is not generated by libxml and cannot be handled using libxml's error handling functions.

Examples

Example524.Creating a Document

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


Example525.Static invocation of loadXML

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


Related Examples ( Source code ) » dom domdocument loadxml



Code Examples / Notes » dom domdocument loadxml

mp

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.


jazzslider

When using loadXML() to parse a string that contains entity references (e.g., &nbsp;), be sure that those entity references are properly declared through the use of a DOCTYPE declaration; otherwise, loadXML() will not be able to interpret the string.
Example:
<?php
$str = <<<XML
<?xml version="1.0" encoding="iso-8859-1"?>
<div>This&nbsp;is a non-breaking space.</div>
XML;
$dd1 = new DOMDocument();
$dd1->loadXML($str);
echo $dd1->saveXML();
?>
Given the above code, PHP will issue a Warning about the entity 'nbsp' not being properly declared.  Also, the call to saveXML() will return nothing but a trimmed-down version of the original processing instruction...everything else is gone, and all because of the undeclared entity.
Instead, explicitly declare the entity first:
<?php
$str = <<<XML
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE root [
<!ENTITY nbsp "&#160;">
]>
<div>This&nbsp;is a non-breaking space.</div>
XML;
$dd2 = new DOMDocument();
$dd2->loadXML($str);
echo $dd2->saveXML();
?>
Since the 'nbsp' entity is defined in the DOCTYPE, PHP no longer issues that Warning; the string is now well-formed, and loadXML() understands it perfectly.
You can also use references to external DTDs in the same way (e.g., <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">), which is particularly important if you need to do this for many different documents with many different possible entities.
Also, as a sidenote...entity references created by createEntityReference() do not need this kind of explicit declaration.


primaryspace

This method replaces any existing document tree already in the object with the document tree contained in the source argument.

marc liyanage

The documentation states that loadXML can be called statically, but this is misleading. This feature seems to be a special case hack and its use seems to be discouraged according to http://bugs.php.net/bug.php?id=41398.
Calling the method statically will fail with an error if the code runs with E_STRICT error reporting enabled.
The documentation should be changed to make it clear that a static call is against recommended practice and won't work with E_STRICT.


stuart grimshaw

Possible values for the options parameter can be found here:
http://us3.php.net/manual/en/ref.libxml.php#libxml.constants


earth

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.


gavin sinai gsinai

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.


gabriel dot birke

If you want to preserve greater-than and lower-than signs inside CDATA sections with XSL processing you have to use the LIBXML_NOCDATA option:
Given this XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output omit-xml-declaration="yes" />
<xsl:template match="/">
<script type="text/javascript">
<xsl:text disable-output-escaping="yes">
// Decorative Brackets:
<![CDATA[// <<<<>>>> ]]>
</xsl:text>
</script>
</xsl:template>
</xsl:stylesheet>
and this PHP:
<?php
$xml = new DOMDocument;
$xml->load('dummy.xml');
$xsl1 = new DOMDocument;
$xsl1->load('scripttest.xsl');
$xsl2 = new DOMDocument;
$xsl2->load('scripttest.xsl', LIBXML_NOCDATA);
$proc = new XSLTProcessor;
echo "Normal Load:\n";
$proc->importStyleSheet($xsl1);
echo $proc->transformToXML($xml);
echo "LIBXML_NOCDATA Load:\n";
$proc->importStyleSheet($xsl2);
echo $proc->transformToXML($xml);
?>
You get the following output:
Normal Load:
<script type="text/javascript">
// Decorative Brackets:
// &lt;&lt;&lt;&lt;&gt;&gt;&gt;&gt;
</script>
LIBXML_NOCDATA Load:
<script type="text/javascript">
// Decorative Brackets:
// <<<<>>>>
</script>


shaoyu73

earth at anonymous dot com,
preserveWhiteSpace property needs to be set to false for formatOutput to work properly, for some reason.
$dom = new DOMDocument;
$dom->preserveWhiteSpace = false;
$dom->loadXML($xmlStr);
...
$element->appendChild(...);
...
$dom->formatOutput = true;
$xmlStr = $dom->saveXML();
echo $xmlStr;
This would format the output nicely.


Change Language


Follow Navioo On Twitter
DOMAttr->__construct()
DOMAttr->isId()
DOMCharacterData->appendData()
DOMCharacterData->deleteData()
DOMCharacterData->insertData()
DOMCharacterData->replaceData()
DOMCharacterData->substringData()
DOMComment->__construct()
DOMDocument->__construct()
DOMDocument->createAttribute()
DOMDocument->createAttributeNS()
DOMDocument->createCDATASection()
DOMDocument->createComment()
DOMDocument->createDocumentFragment()
DOMDocument->createElement()
DOMDocument->createElementNS()
DOMDocument->createEntityReference()
DOMDocument->createProcessingInstruction()
DOMDocument->createTextNode()
DOMDocument->getElementById()
DOMDocument->getElementsByTagName()
DOMDocument->getElementsByTagNameNS()
DOMDocument->importNode()
DOMDocument->load()
DOMDocument->loadHTML()
DOMDocument->loadHTMLFile()
DOMDocument->loadXML()
DOMDocument->normalizeDocument()
DOMDocument->registerNodeClass()
DOMDocument->relaxNGValidate()
DOMDocument->relaxNGValidateSource()
DOMDocument->save()
DOMDocument->saveHTML()
DOMDocument->saveHTMLFile()
DOMDocument->saveXML()
DOMDocument->schemaValidate()
DOMDocument->schemaValidateSource()
DOMDocument->validate()
DOMDocument->xinclude()
DOMDocumentFragment->appendXML()
DOMElement->__construct()
DOMElement->getAttribute()
DOMElement->getAttributeNode()
DOMElement->getAttributeNodeNS()
DOMElement->getAttributeNS()
DOMElement->getElementsByTagName()
DOMElement->getElementsByTagNameNS()
DOMElement->hasAttribute()
DOMElement->hasAttributeNS()
DOMElement->removeAttribute()
DOMElement->removeAttributeNode()
DOMElement->removeAttributeNS()
DOMElement->setAttribute()
DOMElement->setAttributeNode()
DOMElement->setAttributeNodeNS()
DOMElement->setAttributeNS()
DOMElement->setIdAttribute()
DOMElement->setIdAttributeNode()
DOMElement->setIdAttributeNS()
DOMEntityReference->__construct()
DOMImplementation->__construct()
DOMImplementation->createDocument()
DOMImplementation->createDocumentType()
DOMImplementation->hasFeature()
DOMNamedNodeMap->getNamedItem()
DOMNamedNodeMap->getNamedItemNS()
DOMNamedNodeMap->item()
DOMNode->appendChild()
DOMNode->cloneNode()
DOMNode->hasAttributes()
DOMNode->hasChildNodes()
DOMNode->insertBefore()
DOMNode->isDefaultNamespace()
DOMNode->isSameNode()
DOMNode->isSupported()
DOMNode->lookupNamespaceURI()
DOMNode->lookupPrefix()
DOMNode->normalize()
DOMNode->removeChild()
DOMNode->replaceChild()
DOMNodelist->item()
DOMProcessingInstruction->__construct()
DOMText->__construct()
DOMText->isWhitespaceInElementContent()
DOMText->splitText()
DOMXPath->__construct()
DOMXPath->evaluate()
DOMXPath->query()
DOMXPath->registerNamespace()
dom_import_simplexml
eXTReMe Tracker