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!
Constantes pré-définies
Ces constantes sont définies par cette extension, et ne sont disponibles que si cette extension a été compilée avec PHP, ou bien chargée au moment de l'exécution.
- LIBXML_COMPACT (integer)
-
Active l'optimisation de l'allocation de petits noeuds. Ceci pourrait
augmenter la rapidité de votre application sans avoir besoin de changer
votre code.
Note:
Seulement disponible dans Libxml >= 2.6.21
- LIBXML_DTDATTR (integer)
- Attribut de DTD par défaut
- LIBXML_DTDLOAD (integer)
- Charge le sous-ensemble externe
- LIBXML_DTDVALID (integer)
- Valide avec la DTD
- LIBXML_NOBLANKS (integer)
- Suppression des noeuds vides
- LIBXML_NOCDATA (integer)
- Fusion des CDATA en noeuds de texte
- LIBXML_NOEMPTYTAG (integer)
-
Agrandi les balises vides (par exemple, <br/> en
<br></br>)
Note:
Cette option est actuellement disponible uniquement avec les fonctions DOMDocument::save et DOMDocument::saveXML.
- LIBXML_NOENT (integer)
- Substitution des entités
- LIBXML_NOERROR (integer)
- Suppression du rapport d'erreur
- LIBXML_NONET (integer)
- Désactivation du réseau lors du chargement de document
- LIBXML_NOWARNING (integer)
- Suppression des rapports d'alerte
- LIBXML_NOXMLDECL (integer)
-
Annule la déclaration XML lors de la sauvegarde du document
Note:
Seulement disponible dans Libxml >= 2.6.21
- LIBXML_NSCLEAN (integer)
- Suppression des espaces de noms redondants
- LIBXML_PARSEHUGE (integer)
-
Affecte le drapeau XML_PARSE_HUGE. Désactive toute limite du
parseur codée en dur. Ceci affecte les limites comme la profondeur
maximale d'un document ou l'entité récursion, mais aussi
les limites de la taille du texte des noeuds.
Note:
Seulement disponible depuis Libxml >= 2.7.0 (depuis PHP >= 5.3.2 et PHP >= 5.2.12)
- LIBXML_XINCLUDE (integer)
- Implémentation de la substitution XInclude
- LIBXML_ERR_ERROR (integer)
- Erreur non-fatale
- LIBXML_ERR_FATAL (integer)
- Erreur fatale
- LIBXML_ERR_NONE (integer)
- Aucune erreur
- LIBXML_ERR_WARNING (integer)
- Une alerte simple
- LIBXML_VERSION (integer)
- libxml version sous la forme 20605 ou 20617
- LIBXML_DOTTED_VERSION (string)
- libxml version sous la forme 2.6.5 ou 2.6.17
@oneseventeen
06-Feb-2011 12:51
zachatwork at gmail dot com
10-Feb-2010 12:30
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/>
