PHP: DomDocument->dump_mem - Manual

downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

DomDocument->get_element_by_id DomDocument->dump_file

Last updated: Sat, 27 Jan 2007

view this page in

# DomDocument->dump_mem

(no version information, might be only in CVS)

DomDocument->dump_mem -- Dumps the internal XML tree back into a string

## Описание

string DomDocument->dump_mem ( [bool format [, string encoding]] )

Creates an XML document from the dom representation. This function usually is called after building a new dom document from scratch as in the example below. The `format` specifies whether the output should be neatly formatted, or not.

Пример 1. Creating a simple HTML document header

`<?php
$doc = domxml_new_doc("1.0");
$root = $doc->create_element("HTML");
$root = $doc->append_child($root);
$head = $doc->create_element("HEAD");
$head = $root->append_child($head);
$title = $doc->create_element("TITLE");
$title = $head->append_child($title);
$text = $doc->create_text_node("This is the title");
$text = $title->append_child($text);
echo "<PRE>";
echo htmlentities($doc->dump_mem(true));
echo "</PRE>";
?>`

> Замечание: The first parameter was added in PHP 4.3.0.

See also domdocument_dump_file(), and domdocument_html_dump_mem().

add a note User Contributed Notes
DomDocument->dump_mem
jonathan at fluent dot ltd dot uk
30-Nov-2003 04:11
`It seems that if you use $xslt->dump_mem(TRUE) IE: breaks and formats all the HTML Badly. Perhaps its a problem with CR's...`
ross at dev-null dot accretivetg dot com
26-Sep-2003 10:11
`Given the missing description of the encoding parameter, I had hoped it would automatically encode the document if I set the encoding type; this is not the case.
At least in PHP v4.3.3, it seems that setting the encoding type does NOTHING other than set the name displayed in the string:
<?xml version="1.0" encoding="UTF-8"?>
Thus if you want your values to be encoded, you need to manually encode them when you create your DOM object. E.g. assume you have a simple setup like this:
<?php
$text = '�Spanish exclamation!' ;
$xml = domxml_new_doc( '1.0' );
$node = $xml->create_element( 'example' );
$cdata = $xml->create_cdata_section( $text );
$node->append_child( $cdata );
$xml->append_child( $node );
echo $xml->dump_mem( true, 'UTF-8' ) ;
?>
This will output the following:
<?xml version="1.0" encoding="UTF-8"?>
<example>
<![CDATA[�Spanish exclamation!]]>
</example>
However, that is not what was intended, as the exclamation is not UTF-8-encoded. To make $text be encoded correctly, you have to explicitly do so, as in:
<?php
$cdata = $xml->create_cdata_section( utf8_encode( $text ) );
?>
This results in the output being correctly UTF-8-encoded, as follows:
<![CDATA[¡Spanish exclamation!]]>
Apparently MSIE's MSXML2.DOMDocument ActiveX control requires UTF-8 encoding for special characters, and it will invalidate the entire document if those chars are not appropriately encoded, so beware.`
amaslov at pegasus dot rutgers dot edu
23-Oct-2002 02:29
`<pre>
for xml file that goes like this:
<item>
<name>foo</name>
<desc>bar</desc>
</item>
Format ID's influence output in this manner:
case 0 :{
<item><name>foo</name><desc>bar</desc></item>
}
case 1 :{
<item>
<name>foo</name>
<desc>bar</desc>
</item>
}
case 2:{
<item>
<name>
foo
</name>
<desc>
bar
</desc>
</item>
If you use PHP SAX(Expat) XML engine, you should always stick to format "0". It reads any empty spaces as XML data.
</pre>`
bruno dot rodrigues at litux dot org
15-Aug-2002 02:32
`string dump_mem(int format, string encoding);
format=0,1 = <tag>text</tag>
format=2 =
<tag>
text
</tag>
encoding set's the encoding attribute in line
<?xml version="1.0" encoding="iso-8859-1"?>`
add a note
DomDocument->get_element_by_id DomDocument->dump_file

Last updated: Sat, 27 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 09:32:46 2007 PST