DOM XML (PHP 4) 函数
在线手册:中文 英文
PHP手册

DomDocument::dump_mem

(PHP 4 >= 4.1.0)

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.

Example #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>";
?>

Note:

The first parameter was added in PHP 4.3.0.

See also domdocument_dump_file(), and domdocument_html_dump_mem().


DOM XML (PHP 4) 函数
在线手册:中文 英文
PHP手册
PHP手册 - N: Dumps the internal XML tree back into a string

用户评论:

jonathan at fluent dot ltd dot uk (01-Dec-2003 12: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 (27-Sep-2003 06: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 10: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 10: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"?>