changing indentation with DOMDocument

changing indentation with DOMDocument

am 11.01.2008 21:34:53 von yawnmoth

$xml = new DOMDocument();
$xml->loadXML('

test

');

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

echo '

';
echo htmlspecialchars($xml->saveXML());
echo '
';

?>

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


test
test


Instead, it produces this:


test
test


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?

Re: changing indentation with DOMDocument

am 12.01.2008 01:50:27 von jonKushner

On Jan 11, 12:34=A0pm, yawnmoth wrote:
> > $xml =3D new DOMDocument();
> $xml->loadXML('
>
> =A0 test
>

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

';
> echo htmlspecialchars($xml->saveXML());
> echo '
';
>
> ?>
>
> I'd like for this to be able to return this:
>
>
> =A0 test
> =A0 test
>

>
> Instead, it produces this:
>
>
> =A0 test
> test

>
> 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 :)

Re: changing indentation with DOMDocument

am 12.01.2008 02:29:37 von Jeremy

yawnmoth wrote:
> > $xml = new DOMDocument();
> $xml->loadXML('
>
> test
>

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

';
> echo htmlspecialchars($xml->saveXML());
> echo '
';
>
> ?>
>
> I'd like for this to be able to return this:
>
>
> test
> test
>

>
> Instead, it produces this:
>
>
> test
> test

>
> 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

Re: changing indentation with DOMDocument

am 12.01.2008 02:31:55 von Jeremy

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

Re: changing indentation with DOMDocument

am 12.01.2008 03:19:28 von luiheidsgoeroe

On Sat, 12 Jan 2008 02:29:37 +0100, Jeremy wrote:

> yawnmoth wrote:
>> >> $xml =3D new DOMDocument();
>> $xml->loadXML('
>>
>> test
>>

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

';
>> echo htmlspecialchars($xml->saveXML());
>> echo '
';
>> ?>
>> I'd like for this to be able to return this:
>>
>> test
>> test
>>

>> Instead, it produces this:
>>
>> test
>> test

>> 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 'test').
-- =

Rik Wasmus