void XmlFileWriter::writeInternal(const char *pcszFilename, bool fSafe) { WriteContext context(pcszFilename, fSafe); GlobalLock lock; /* serialize to the stream */ xmlIndentTreeOutput = 1; xmlTreeIndentString = " "; xmlSaveNoEmptyTags = 0; xmlSaveCtxtPtr saveCtxt; if (!(saveCtxt = xmlSaveToIO(WriteCallback, CloseCallback, &context, NULL, XML_SAVE_FORMAT))) throw xml::LogicError(RT_SRC_POS); long rc = xmlSaveDoc(saveCtxt, m->pDoc->m->plibDocument); if (rc == -1) { /* look if there was a forwarded exception from the lower level */ // if (m->trappedErr.get() != NULL) // m->trappedErr->rethrow(); /* there must be an exception from the Output implementation, * otherwise the save operation must always succeed. */ throw xml::LogicError(RT_SRC_POS); } xmlSaveClose(saveCtxt); }
int xmlSaveNode(void *node, void *encoding, int options) { xmlSaveCtxtPtr savectx; const char *c_encoding = (char*)encoding; savectx = xmlSaveToIO( (xmlOutputWriteCallback)xml_write_callback, (xmlOutputCloseCallback)close_callback, NULL, encoding, options ); xmlSaveTree(savectx, (xmlNode*)node); return xmlSaveClose(savectx); }
std::ostream& operator<< (std::ostream &stream, const xml::node &n) { node2doc n2d(n.pimpl_->xmlnode_); xmlDocPtr doc = n2d.get_doc(); int libxml2_options = convert_to_libxml2_save_options(save_op_default); const char * enc = NULL; if (n.pimpl_->xmlnode_->doc) enc = (const char *)(n.pimpl_->xmlnode_->doc->encoding); xmlSaveCtxtPtr ctxt = xmlSaveToIO(save_to_stream_cb, NULL, &stream, enc, libxml2_options); if (ctxt) { xmlSaveDoc(ctxt, doc); xmlSaveClose(ctxt); // xmlSaveFlush() is called in xmlSaveClose() } return stream; }
static gboolean xspf_playlist_save (const gchar * filename, VFSFile * file, const gchar * title, Index * filenames, Index * tuples) { gint entries = index_count (filenames); xmlDocPtr doc; xmlNodePtr rootnode, tracklist; gint count; doc = xmlNewDoc((xmlChar *)"1.0"); doc->charset = XML_CHAR_ENCODING_UTF8; doc->encoding = xmlStrdup((xmlChar *)"UTF-8"); rootnode = xmlNewNode(NULL, (xmlChar *)XSPF_ROOT_NODE_NAME); xmlSetProp(rootnode, (xmlChar *)"version", (xmlChar *)"1"); xmlSetProp(rootnode, (xmlChar *)"xmlns", (xmlChar *)XSPF_XMLNS); /* common */ xmlDocSetRootElement(doc, rootnode); if (title) xspf_add_node (rootnode, TUPLE_STRING, FALSE, "title", title, 0); tracklist = xmlNewNode(NULL, (xmlChar *)"trackList"); xmlAddChild(rootnode, tracklist); for (count = 0; count < entries; count ++) { const gchar * filename = index_get (filenames, count); const Tuple * tuple = index_get (tuples, count); xmlNodePtr track, location; gchar *scratch = NULL; gint scratchi = 0; track = xmlNewNode(NULL, (xmlChar *)"track"); location = xmlNewNode(NULL, (xmlChar *)"location"); xmlAddChild(location, xmlNewText((xmlChar *)filename)); xmlAddChild(track, location); xmlAddChild(tracklist, track); if (tuple != NULL) { gint i; for (i = 0; i < xspf_nentries; i++) { const xspf_entry_t *xs = &xspf_entries[i]; gboolean isOK = (tuple_get_value_type (tuple, xs->tupleField, NULL) == xs->type); switch (xs->type) { case TUPLE_STRING: scratch = tuple_get_str (tuple, xs->tupleField, NULL); if (! scratch) isOK = FALSE; str_unref(scratch); break; case TUPLE_INT: scratchi = tuple_get_int (tuple, xs->tupleField, NULL); break; default: break; } if (isOK) xspf_add_node(track, xs->type, xs->isMeta, xs->xspfName, scratch, scratchi); } } } xmlSaveCtxt * save = xmlSaveToIO (write_cb, close_cb, file, NULL, XML_SAVE_FORMAT); if (! save) goto ERR; if (xmlSaveDoc (save, doc) < 0 || xmlSaveClose (save) < 0) goto ERR; xmlFreeDoc(doc); return TRUE; ERR: xmlFreeDoc (doc); return FALSE; }