When inserting XML DOM Elements inside existing XML DOM Elements that I loaded from an XML file using the following code, none of my new elements were formatted correctly, they just showed up on one line:
<?php
$dom = DOMDocument::load('file.xml');
$dom->formatOutput = true;
//$dom->add some new elements with child nodes somewhere inside the loaded XML using insertBefore();
$dom->saveXML();
//output: everything looks normal but the new nodes are all on one line.
?>
I found I could pass LIBXML_NOBLANKS to the load method and it would reformat the whole document, including my added stuff:
<?php
$dom = DOMDocument::load('file.xml', LIBXML_NOBLANKS);
$dom->formatOutput = true;
//$dom->add some new elements with child nodes somewhere inside the loaded XML using insertBefore();
$dom->saveXML();
//output: everything looks newly formatted, including new nodes
?>
Hope this helps, took me hours of trial and error to figure this out!
Predefined Constants
The constants below are defined by this extension, and will only be available when the extension has either been compiled into PHP or dynamically loaded at runtime.
-
LIBXML_COMPACT(integer) -
Activate small nodes allocation optimization. This may speed up your
application without needing to change the code.
Note:
Only available in Libxml >= 2.6.21
-
LIBXML_DTDATTR(integer) - Default DTD attributes
-
LIBXML_DTDLOAD(integer) - Load the external subset
-
LIBXML_DTDVALID(integer) - Validate with the DTD
-
LIBXML_NOBLANKS(integer) - Remove blank nodes
-
LIBXML_NOCDATA(integer) - Merge CDATA as text nodes
-
LIBXML_NOEMPTYTAG(integer) -
Expand empty tags (e.g. <br/> to
<br></br>)
Note:
This option is currently just available in the DOMDocument::save and DOMDocument::saveXML functions.
-
LIBXML_NOENT(integer) - Substitute entities
-
LIBXML_NOERROR(integer) - Suppress error reports
-
LIBXML_NONET(integer) - Disable network access when loading documents
-
LIBXML_NOWARNING(integer) - Suppress warning reports
-
LIBXML_NOXMLDECL(integer) -
Drop the XML declaration when saving a document
Note:
Only available in Libxml >= 2.6.21
-
LIBXML_NSCLEAN(integer) - Remove redundant namespaces declarations
-
LIBXML_PARSEHUGE(integer) -
Sets XML_PARSE_HUGE flag, which relaxes any hardcoded limit from the parser. This affects
limits like maximum depth of a document or the entity recursion, as well as limits of the
size of text nodes.
Note:
Only available in Libxml >= 2.7.0 (as of PHP >= 5.3.2 and PHP >= 5.2.12)
-
LIBXML_XINCLUDE(integer) - Implement XInclude substitution
-
LIBXML_ERR_ERROR(integer) - A recoverable error
-
LIBXML_ERR_FATAL(integer) - A fatal error
-
LIBXML_ERR_NONE(integer) - No errors
-
LIBXML_ERR_WARNING(integer) - A simple warning
-
LIBXML_VERSION(integer) - libxml version like 20605 or 20617
-
LIBXML_DOTTED_VERSION(string) - libxml version like 2.6.5 or 2.6.17
@oneseventeen ¶
2 years ago
zachatwork at gmail dot com ¶
3 years ago
Note: The LIBXML_NOXMLDECL constant is defined in this library but is not supported by DOMDocument (yet).
See also: http://bugs.php.net/bug.php?id=47137
<?php
print "PHP_VERSION: ".PHP_VERSION."\n";
print "LIBXML_VERSION: ".LIBXML_VERSION."\n";
print "LIBXML_NOXMLDECL: ".LIBXML_NOXMLDECL."\n";
$dom = new DomDocument();
$dom->loadXML("<foo />");
# This should work but doesn't.
print "DOMDocument doesn't honor LIBXML_NOXMLDECL:\n";
print $dom->saveXML(null,LIBXML_NOXMLDECL);
# This works, and will still work after the above is fixed.
print "Forwards compatible workaround:\n";
$lines = explode("\n", $dom->saveXML(null, LIBXML_NOXMLDECL), 2);
if(!preg_match('/^\<\?xml/', $lines[0]))
print $lines[0];
print $lines[1];
?>
PHP_VERSION: 5.3.1-0.dotdeb.1
LIBXML_VERSION: 20632
LIBXML_NOXMLDECL: 2
DOMDocument doesn't honor LIBXML_NOXMLDECL:
<?xml version="1.0"?>
<foo/>
Forwards compatible workaround:
<foo/>
