|
26 | 26 | #include "php.h"
|
27 | 27 | #if HAVE_LIBXML && HAVE_DOM
|
28 | 28 | #include "php_dom.h"
|
| 29 | +#include <libxml/xmlsave.h> |
29 | 30 | #include <libxml/SAX.h>
|
30 | 31 | #ifdef LIBXML_SCHEMAS_ENABLED
|
31 | 32 | #include <libxml/relaxng.h>
|
@@ -1616,59 +1617,51 @@ PHP_FUNCTION(dom_document_savexml)
|
1616 | 1617 | dom_doc_propsptr doc_props;
|
1617 | 1618 | int size, format, saveempty = 0;
|
1618 | 1619 | zend_long options = 0;
|
| 1620 | + xmlSaveCtxtPtr xscp; |
1619 | 1621 |
|
1620 | 1622 | if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O|O!l", &id, dom_document_class_entry, &nodep, dom_node_class_entry, &options) == FAILURE) {
|
1621 | 1623 | return;
|
1622 | 1624 | }
|
| 1625 | + options = options | XML_SAVE_AS_XML; |
1623 | 1626 |
|
1624 | 1627 | DOM_GET_OBJ(docp, id, xmlDocPtr, intern);
|
1625 | 1628 |
|
1626 | 1629 | doc_props = dom_get_doc_props(intern->document);
|
1627 | 1630 | format = doc_props->formatoutput;
|
1628 | 1631 |
|
| 1632 | + buf = xmlBufferCreate(); |
| 1633 | + if (!buf) { |
| 1634 | + php_error_docref(NULL, E_WARNING, "Could not fetch buffer"); |
| 1635 | + RETURN_FALSE; |
| 1636 | + } |
| 1637 | + xscp = xmlSaveToBuffer(buf, docp->encoding, options); |
| 1638 | + |
1629 | 1639 | if (nodep != NULL) {
|
1630 | 1640 | /* Dump contents of Node */
|
1631 | 1641 | DOM_GET_OBJ(node, nodep, xmlNodePtr, nodeobj);
|
1632 | 1642 | if (node->doc != docp) {
|
1633 | 1643 | php_dom_throw_error(WRONG_DOCUMENT_ERR, dom_get_strict_error(intern->document));
|
| 1644 | + xmlBufferFree(buf); |
1634 | 1645 | RETURN_FALSE;
|
1635 | 1646 | }
|
1636 |
| - buf = xmlBufferCreate(); |
1637 |
| - if (!buf) { |
1638 |
| - php_error_docref(NULL, E_WARNING, "Could not fetch buffer"); |
1639 |
| - RETURN_FALSE; |
1640 |
| - } |
1641 |
| - if (options & LIBXML_SAVE_NOEMPTYTAG) { |
1642 |
| - saveempty = xmlSaveNoEmptyTags; |
1643 |
| - xmlSaveNoEmptyTags = 1; |
1644 |
| - } |
1645 |
| - xmlNodeDump(buf, docp, node, 0, format); |
1646 |
| - if (options & LIBXML_SAVE_NOEMPTYTAG) { |
1647 |
| - xmlSaveNoEmptyTags = saveempty; |
1648 |
| - } |
1649 |
| - mem = (xmlChar*) xmlBufferContent(buf); |
1650 |
| - if (!mem) { |
| 1647 | + if(xmlSaveTree(xscp, node) < 0) { |
1651 | 1648 | xmlBufferFree(buf);
|
1652 | 1649 | RETURN_FALSE;
|
1653 | 1650 | }
|
1654 |
| - RETVAL_STRING((char *) mem); |
1655 |
| - xmlBufferFree(buf); |
1656 | 1651 | } else {
|
1657 |
| - if (options & LIBXML_SAVE_NOEMPTYTAG) { |
1658 |
| - saveempty = xmlSaveNoEmptyTags; |
1659 |
| - xmlSaveNoEmptyTags = 1; |
1660 |
| - } |
1661 |
| - /* Encoding is handled from the encoding property set on the document */ |
1662 |
| - xmlDocDumpFormatMemory(docp, &mem, &size, format); |
1663 |
| - if (options & LIBXML_SAVE_NOEMPTYTAG) { |
1664 |
| - xmlSaveNoEmptyTags = saveempty; |
1665 |
| - } |
1666 |
| - if (!size || !mem) { |
| 1652 | + if(xmlSaveDoc(xscp, docp) < 0) { |
| 1653 | + xmlBufferFree(buf); |
1667 | 1654 | RETURN_FALSE;
|
1668 | 1655 | }
|
1669 |
| - RETVAL_STRINGL((char *) mem, size); |
1670 |
| - xmlFree(mem); |
1671 | 1656 | }
|
| 1657 | + xmlSaveClose(xscp); |
| 1658 | + mem = (xmlChar*) xmlBufferContent(buf); |
| 1659 | + if (!mem) { |
| 1660 | + xmlBufferFree(buf); |
| 1661 | + RETURN_FALSE; |
| 1662 | + } |
| 1663 | + RETVAL_STRING((char *) mem); |
| 1664 | + xmlBufferFree(buf); |
1672 | 1665 | }
|
1673 | 1666 | /* }}} end dom_document_savexml */
|
1674 | 1667 |
|
|
0 commit comments