Ejemplo n.º 1
0
/**
 * 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;
}
Ejemplo n.º 2
0
/***********************************************
 *  生成XML的字符串                            *
 *  成功返回XML的字符串                        *
 *  失败则返回NULL                             *
 *  doc用完已自动清空                          *
 ***********************************************/
byte * generateXmlBuffer(xmlDocPtr doc)
{
	static byte szBuf[2048];
	byte * buffer;
	xmlBufferPtr buf = NULL;
    	xmlSaveCtxtPtr ctxt = NULL;
	LIBXML_TEST_VERSION;
	if(doc == NULL) {
		ErrorLog(ERROR, "doc is null");
		printf("doc is null");
		return NULL;
	}

	buf = xmlBufferCreate();
    	ctxt = xmlSaveToBuffer(buf, "UTF-8", 1);

    	xmlSaveDoc(ctxt, doc);
    	xmlSaveClose(ctxt);
	
	memset(szBuf, 0x00, sizeof(szBuf));
	buffer = u2g(buf->content);
	sprintf(szBuf, "%s",buffer );
	
	buf->content = NULL;
    	xmlBufferFree(buf);

    	xmlFreeDoc(doc);
    	xmlCleanupParser();
	
	free(buffer);
	return szBuf;
}
Ejemplo n.º 3
0
int lpc2xml_convert_string(lpc2xml_context* context, char **content) {
	int ret = -1;
	xmlBufferPtr buffer = xmlBufferCreate();
	xmlSaveCtxtPtr save_ctx;
	lpc2xml_context_clear_logs(context);
	xmlSetGenericErrorFunc(context, lpc2xml_genericxml_error);
	save_ctx = xmlSaveToBuffer(buffer, "UTF-8", XML_SAVE_FORMAT);
	if(save_ctx != NULL) {
		ret = internal_convert_lpc2xml(context);
		if(ret == 0) {
			ret = xmlSaveDoc(save_ctx, context->doc);
			if(ret != 0) {
				lpc2xml_log(context, LPC2XML_ERROR, "Can't save document");
				lpc2xml_log(context, LPC2XML_ERROR, "%s", context->errorBuffer);
			}
		}
		xmlSaveClose(save_ctx);
	} else {
		lpc2xml_log(context, LPC2XML_ERROR, "Can't initialize internal buffer");
		lpc2xml_log(context, LPC2XML_ERROR, "%s", context->errorBuffer);
	}
	if(ret == 0) {
#if LIBXML_VERSION >= 20800
		*content = (char *)xmlBufferDetach(buffer);
#else
		*content = strdup((const char *)xmlBufferContent(buffer));
#endif
	}
	xmlBufferFree(buffer);
	return ret;
}
Ejemplo n.º 4
0
const std::string queryResult::to_xml()
{
	xmlDocPtr doc;
	xmlNodePtr root;
	int rc;
	std::string returnValue;

	doc = xmlNewDoc(BAD_CAST "1.0");
	root = xmlNewNode(NULL, BAD_CAST "result");

	std::stringstream temp;
	temp << size();
	xmlNewProp(root, BAD_CAST "count", BAD_CAST temp.str().c_str());

	if(__code != 0) {
		temp.str("");
		temp << __code;
		xmlNewProp(root, BAD_CAST "ocicode", BAD_CAST temp.str().c_str());
	}

	if(__info.length()) {
		xmlNewProp(root, BAD_CAST "ociinfo", BAD_CAST __info.c_str());
	}

	xmlDocSetRootElement(doc, root);


	for(int i=0; i<size(); ++i) {
			xmlNodePtr branch = xmlNewChild(root, NULL, BAD_CAST "branch", NULL);
	
			std::multimap<std::string, std::string>::iterator it, iend = at(i).end();
			for(it=at(i).begin(); it!=iend; ++it) {
						xmlNodePtr child = xmlNewChild(branch, NULL, BAD_CAST it->first.c_str(), BAD_CAST it->second.c_str());
					}
		}

	xmlNodePtr bind = xmlNewChild(root, NULL, BAD_CAST "bind", NULL);
	std::map<std::string, std::pair<std::string, bool> >::iterator j, jend = __bindouts.end();
	for(j = __bindouts.begin(); j != jend; ++j) {
		if(j->second.second == true)
			continue;
		xmlNodePtr child = xmlNewChild(bind, NULL, BAD_CAST j->first.c_str(), BAD_CAST j->second.first.c_str());
	}

	xmlBufferPtr buf = xmlBufferCreate();
	xmlSaveCtxtPtr sav = xmlSaveToBuffer(buf, "UTF-8", XML_SAVE_FORMAT | XML_SAVE_NO_EMPTY);

	xmlSaveDoc(sav, doc);
	xmlSaveClose(sav);

	returnValue = (char*)buf->content;

	xmlBufferFree(buf);
	xmlFreeDoc(doc);

	return returnValue;
}
Ejemplo n.º 5
0
bool CXmlTree::WriteXmlFileToString(cvs::string& string) const
{
	xmlBufferPtr buffer = xmlBufferCreate();
	if(!buffer)
		return false;
	xmlSaveCtxtPtr save = xmlSaveToBuffer(buffer, NULL, 0);
	if(!save)
	{
		xmlBufferFree(buffer);
		return false;
	}
	xmlSaveDoc(save, m_doc);
	xmlSaveFlush(save);
	xmlSaveClose(save);
	string = (const char*)xmlBufferContent(buffer);
	xmlBufferFree(buffer);
	return true;
}
Ejemplo n.º 6
0
const std::string XMLDocument::dumpHTML(bool indent) const
{
    xmlBuffer * buffer = xmlBufferCreate();
    int ret;
    int options = XML_SAVE_AS_HTML;
    if (indent)
    {
        options |= XML_SAVE_FORMAT;
    }

    xmlThrDefIndentTreeOutput(1);
    xmlSaveCtxtPtr ctxt = xmlSaveToBuffer(buffer, 0, options);
    ret = xmlSaveDoc(ctxt, document);
    xmlSaveFlush(ctxt);
    xmlSaveClose(ctxt);

    std::string str((const char *)xmlBufferDetach(buffer));
    xmlBufferFree(buffer);

    return str;
}
Ejemplo n.º 7
0
// -------------------------------------------------------------
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 "";
	}
}