changing indentation with DOMDocument

<?php
$xml = new DOMDocument();
$xml->loadXML('
<a>
<b>test</b>
</a>
');

$temp = $xml->createElement('b');
$temp->appendChild($xml->createTextNode('test'));
$xml->firstChild->appendChild($temp);

echo '<pre>';
echo htmlspecialchars($xml->saveXML());
echo '</pre>';

?>

I'd like for this to be able to return this:

<a>
<b>test</b>
<b>test</b>
</a>

Instead, it produces this:

<a>
<b>test</b>
<b>test</b></a>

ie. the indentation convention that I'm using isn't being used by
saveXML(). Setting $xml->preserveWhiteSpace and $xml->formatOutput to
true doesn't seem to help.

Any ideas as to what - if anything - I can do?
yawnmoth [ Fr, 11 Januar 2008 21:34 ] [ ID #1905582 ]

Re: changing indentation with DOMDocument

On Jan 11, 12:34=A0pm, yawnmoth <terra1... [at] yahoo.com> wrote:
> <?php
> $xml =3D new DOMDocument();
> $xml->loadXML('
> <a>
> =A0 <b>test</b>
> </a>
> ');
>
> $temp =3D $xml->createElement('b');
> $temp->appendChild($xml->createTextNode('test'));
> $xml->firstChild->appendChild($temp);
>
> echo '<pre>';
> echo htmlspecialchars($xml->saveXML());
> echo '</pre>';
>
> ?>
>
> I'd like for this to be able to return this:
>
> <a>
> =A0 <b>test</b>
> =A0 <b>test</b>
> </a>
>
> Instead, it produces this:
>
> <a>
> =A0 <b>test</b>
> <b>test</b></a>
>
> ie. the indentation convention that I'm using isn't being used by
> saveXML(). =A0Setting $xml->preserveWhiteSpace and $xml->formatOutput to
> true doesn't seem to help.
>
> Any ideas as to what - if anything - I can do?

I don't think this is going to help your cause, but maybe it is
because you are not including \n. To me, this looks just fine though :)
jonKushner [ Sa, 12 Januar 2008 01:50 ] [ ID #1906243 ]

Re: changing indentation with DOMDocument

yawnmoth wrote:
> <?php
> $xml = new DOMDocument();
> $xml->loadXML('
> <a>
> <b>test</b>
> </a>
> ');
>
> $temp = $xml->createElement('b');
> $temp->appendChild($xml->createTextNode('test'));
> $xml->firstChild->appendChild($temp);
>
> echo '<pre>';
> echo htmlspecialchars($xml->saveXML());
> echo '</pre>';
>
> ?>
>
> I'd like for this to be able to return this:
>
> <a>
> <b>test</b>
> <b>test</b>
> </a>
>
> Instead, it produces this:
>
> <a>
> <b>test</b>
> <b>test</b></a>
>
> ie. the indentation convention that I'm using isn't being used by
> saveXML(). Setting $xml->preserveWhiteSpace and $xml->formatOutput to
> true doesn't seem to help.
>
> Any ideas as to what - if anything - I can do?

You want preserveWhiteSpace = false and formatOutput = true (I know,
it's weird. But that's how it seems to work.) I think these options
also must be set directly after creating the new DOMDocument:

$xml = new DOMDocument;
$xml->preserveWhiteSpace = false;
$xml->formatOutput = true;

// do whatever you do

echo $xml->saveXML(); //output is indented


Jeremy
Jeremy [ Sa, 12 Januar 2008 02:29 ] [ ID #1906244 ]

Re: changing indentation with DOMDocument

Jeremy wrote:
>
> You want preserveWhiteSpace = false and formatOutput = true (I know,
> it's weird.

Maybe not so weird, now that I think about it. preserveWhiteSpace =
true tells it to preserve whitespace text nodes to avoid changing the
meaning of the document. Indenting the document does in fact add
whitespace text nodes, which is a far cry from preserving them.

Jeremy
Jeremy [ Sa, 12 Januar 2008 02:31 ] [ ID #1906245 ]

Re: changing indentation with DOMDocument

On Sat, 12 Jan 2008 02:29:37 +0100, Jeremy <jeremy [at] pinacol.com> wrote:

> yawnmoth wrote:
>> <?php
>> $xml =3D new DOMDocument();
>> $xml->loadXML('
>> <a>
>> <b>test</b>
>> </a>
>> ');
>> $temp =3D $xml->createElement('b');
>> $temp->appendChild($xml->createTextNode('test'));
>> $xml->firstChild->appendChild($temp);
>> echo '<pre>';
>> echo htmlspecialchars($xml->saveXML());
>> echo '</pre>';
>> ?>
>> I'd like for this to be able to return this:
>> <a>
>> <b>test</b>
>> <b>test</b>
>> </a>
>> Instead, it produces this:
>> <a>
>> <b>test</b>
>> <b>test</b></a>
>> ie. the indentation convention that I'm using isn't being used by
>> saveXML(). Setting $xml->preserveWhiteSpace and $xml->formatOutput t=
o
>> true doesn't seem to help.
>> Any ideas as to what - if anything - I can do?
>
> You want preserveWhiteSpace =3D false and formatOutput =3D true (I kno=
w, =

> it's weird. But that's how it seems to work.) I think these options =
=

> also must be set directly after creating the new DOMDocument:

Settings formatOutput before loading indeed solved the problem, or, =

strangely anough, feeding it a string without ANY whitespace outside tag=
s =

(so '<a><b>test</b></a>').
-- =

Rik Wasmus
luiheidsgoeroe [ Sa, 12 Januar 2008 03:19 ] [ ID #1906247 ]
PHP » comp.lang.php » changing indentation with DOMDocument

Vorheriges Thema: Search DB by more than one word
Nächstes Thema: displaying a file not in the webroot