/** * Serialises an xmlNode to a string * * @param log Log context * @param node Input XML node to be serialised * * @return Returns a pointer to a new buffer containing the serialised data. This buffer must be freed * after usage */ char *xmlNodeToString(LogContext *log, xmlNode *node) { xmlBuffer *buf = NULL; xmlSaveCtxt *serctx = NULL; char *ret = NULL; if( node == NULL ) { writelog(log, LOG_ALERT, "xmlNodeToString: Input data is NULL"); return NULL; } buf = xmlBufferCreate(); assert( buf != NULL ); serctx = xmlSaveToBuffer(buf, "UTF-8", XML_SAVE_NO_EMPTY|XML_SAVE_NO_DECL); assert( serctx != NULL ); if( xmlSaveTree(serctx, node) < 0 ) { writelog(log, LOG_ALERT, "xmlNodeToString: Failed to serialise xmlNode"); return NULL; } xmlSaveClose(serctx); ret = strdup_nullsafe((char *) xmlBufferContent(buf)); xmlBufferFree(buf); return ret; }
int main(int argc, char * argv[]) { xmlNodePtr sentence; gchar ** tokens; GString * linebuffer = g_string_new(""); GIOChannel * in = g_io_channel_unix_new(fileno(stdin)); //首先读取一行文字 g_io_channel_read_line_string(in,linebuffer,NULL,NULL); //分割 tokens = g_strsplit_set(linebuffer->str," ",1024); //为此行文字打标记 init_yylex(tokens); //打好词性标记后开始解析语法结构 xmlInitGlobals(); xmlInitMemory(); yyparse(&sentence); xmlCleanupGlobals(); xmlCleanupMemory(); xmlSaveCtxtPtr saver = xmlSaveToFd(2,"UTF-8",0); xmlSaveTree(saver,sentence); xmlSaveFlush(saver); xmlSaveClose(saver); write(2,"\n",1); //输出语法树, 以 XML 形式 return 0; }
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); }
// ------------------------------------------------------------- const char* Webpage::getNodeAsString(string exp) { xmlXPathObjectPtr obj = xpath(exp); xmlNodeSetPtr nodeset = obj->nodesetval; if(nodeset && nodeset->nodeNr>0) { xmlBufferPtr buf = xmlBufferCreate(); xmlSaveCtxtPtr savectx = xmlSaveToBuffer(buf, 0, XML_SAVE_FORMAT); xmlNodePtr node = nodeset->nodeTab[0]; if (savectx) { xmlSaveTree(savectx, node); xmlSaveClose(savectx); } return (const char*)xmlBufferContent(buf); } else { cout << "no node found" << endl; return ""; } }