DOMDocument Extension: Output current nodelist

From: Date: Tue, 29 Jul 2008 22:23:53 +0000
Subject: DOMDocument Extension: Output current nodelist
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-39444@lists.php.net to get a copy of this message
Hello, this is my first posting and first patch proposal, so please be kind :) I recognized at work today that DOMDocument has two function saveHTML and saveHTMLFile, but only a save function for the "unfiltered" xml node structure to save to a file although libxml provides the functionality to export the data to string. This is bad because saveHTML(File) does lots of filtering on the output and if the document still has internal placeholders inside because its not finished they won't be recognized again. save() circumvents this by just printing the original inputted data, but needs to be used by saving the data into a tmp file and extracting it back by using file_get_contents for example. I created a patch that adds a new method "DOMDocument::ouput" that does what save() does just returns the output as a string from the function. I have attached the two diffs, they go in "ext/dom/" I tested this with 5.2.6. They are essentially copy paste and merge of the DOMDocument::save and DOMDocument::saveHtml functions. I changed the PHP.net manual snippet as a demo: <?php $doc = new DOMDocument('1.0'); $root = $doc->createElement('html'); $root = $doc->appendChild($root); $head = $doc->createElement('head'); $head = $root->appendChild($head); $title = $doc->createElement('title'); $title = $head->appendChild($title); $text = $doc->createTextNode('This is the title'); $text = $title->appendChild($text); echo $doc->output(); ?>

148a149,152 > ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_document_output, 0, 0, 0) > ZEND_END_ARG_INFO(); > > static 246a251 > PHP_FALIAS(output, dom_document_output, arginfo_dom_document_output) 1826a1832,1859 > /* {{{ proto string dom_document_save( void ) ; > return string representation of the dom > */ > PHP_FUNCTION(dom_document_output) > { > zval *id; > xmlDoc *docp; > dom_object *intern; > xmlChar *mem; > int size; > > if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &id, dom_document_class_entry) == FAILURE) { > return; > } > > DOM_GET_OBJ(docp, id, xmlDocPtr, intern); > > xmlDocDumpMemory(docp, &mem, &size); > if (!size) { > if (mem) > xmlFree(mem); > RETURN_FALSE; > } > RETVAL_STRINGL(mem, size, 1); > xmlFree(mem); > } > /* }}} end dom_document_output */ > 128a129 > PHP_FUNCTION(dom_document_output);
« previous php.internals (#39444) next »