/* wrapper for stats_event, this takes a charset to convert from */ void stats_event_conv(const char *mount, const char *name, const char *value, const char *charset) { const char *metadata = value; xmlBufferPtr conv = xmlBufferCreate (); if (charset && value) { xmlCharEncodingHandlerPtr handle = xmlFindCharEncodingHandler (charset); if (handle) { xmlBufferPtr raw = xmlBufferCreate (); xmlBufferAdd (raw, (const xmlChar *)value, strlen (value)); if (xmlCharEncInFunc (handle, conv, raw) > 0) metadata = (char *)xmlBufferContent (conv); xmlBufferFree (raw); xmlCharEncCloseFunc (handle); } else WARN1 ("No charset found for \"%s\"", charset); } stats_event (mount, name, metadata); xmlBufferFree (conv); }
void closeFile() { mXPath.clear(); // This section writes to file std::ofstream fileStream; if(!mStream) { fileStream.open(mXMLFilePath.c_str()); mStream = &fileStream; } xmlBufferPtr buffer = xmlBufferCreate(); xmlOutputBuffer * outputBuffer = xmlOutputBufferCreateBuffer(buffer, NULL); xmlSaveFormatFileTo(outputBuffer, mXMLDocument, "utf-8", 1); *mStream << buffer->content; xmlBufferFree(buffer); if(fileStream.is_open()) { fileStream.close(); mStream = NULL; } xmlFreeDoc(mXMLDocument); xmlCleanupParser(); if(mHeavyDataWriter->getMode() == XdmfHeavyDataWriter::Default) { mHeavyDataWriter->closeFile(); } };
/** Writes report header to xml file.. Opens the xml file for output. Allocate the memory for xml report. Write the Dtd/Xslt info. Write the root element. @internalComponent @released */ void XmlWriter::StartReport(void) { iXmlFile.open(iXmlFileName.c_str()); if(!(iXmlFile.is_open())) { throw ExceptionReporter(FILEOPENFAIL, __FILE__, __LINE__, (char*)iXmlFileName.c_str()); } if(!(CreateXslFile())) { ExceptionReporter(XSLCREATIONFAILED, __FILE__, __LINE__, (char*)KXslFileName.c_str()).Report(); } iXmlBufPtr = xmlBufferCreate(); // xml writer pointer to buffer ( with no compression ) iXmlTextWriter = xmlNewTextWriterMemory(iXmlBufPtr,0); if (!iXmlBufPtr || !iXmlTextWriter) { throw ExceptionReporter(NOMEMORY,__FILE__,__LINE__); } xmlTextWriterStartDocument(iXmlTextWriter, KXmlVersion.c_str(), KXmlEncoding.c_str(), KNull); xmlTextWriterWriteRaw(iXmlTextWriter,(unsigned char*)KDtdXslInfo.c_str()); xmlTextWriterStartElement(iXmlTextWriter, BAD_CAST KXmlRootElement.c_str()); xmlTextWriterStartElement(iXmlTextWriter,BAD_CAST KXmlcomment.c_str()); xmlTextWriterWriteAttribute(iXmlTextWriter, BAD_CAST KXmlcomment.c_str(), BAD_CAST iInputCommnd.c_str()); }
xmlParserCtxtPtr RS_XML_xmlCreateConnectionParserCtxt(USER_OBJECT_ con) { xmlParserInputBufferPtr buf; xmlParserCtxtPtr ctx = NULL; #ifdef LIBXML2 ctx = xmlNewParserCtxt(); #ifndef LIBXML2_NEW_BUFFER // < 2.9.1 ctx->_private = (USER_OBJECT_) con; /* R_chk_calloc */ buf = (xmlParserInputBufferPtr) calloc(1, sizeof(xmlParserInputBuffer)); buf->readcallback = RS_XML_readConnectionInput; buf->context = (void*) ctx; buf->raw = NULL; /* buf->buffer; */ xmlBufferPtr tmp = xmlBufferCreate(); buf->buffer = tmp; #else RFunCtxData *userData = (RFunCtxData *) R_alloc(sizeof(RFunCtxData), 1); userData->fun = con; userData->ctx = ctx; buf = xmlParserInputBufferCreateIO(RS_XML_readConnectionInput, NULL, userData, XML_CHAR_ENCODING_NONE); #endif xmlParserInputPtr input = xmlNewIOInputStream(ctx, buf, XML_CHAR_ENCODING_NONE); if(!input) { PROBLEM "can't create new IOInputStream" ERROR; } inputPush(ctx, input); #endif return(ctx); }
void TrackerConfig::dump() const { string s; xmlBufferPtr pBuffer = xmlBufferCreate(); xmlNodeDump(pBuffer, m_Doc, m_pRoot, 0, 0); cerr << xmlBufferContent(pBuffer) << endl; }
bool Xml::saveToBuffer(XmlNode *curNode, std::string *buf) const { if (NULL == curNode) { fprintf(stderr, "[ERROR] In Xml::saveToBuffer, curNode == NULL\n"); return false; } if (NULL == buf) { fprintf(stderr, "[ERROR] In Xml::saveToBuffer, buf == NULL\n"); return false; } xmlBuffer *xmlBuf = xmlBufferCreate(); if (NULL == xmlBuf) { fprintf(stderr, "[ERROR] In Xml::saveToBuffer, " "xmlBufferCreate() error\n"); return false; } int bytes = xmlNodeDump(xmlBuf, _doc, curNode, XML_INDENT_LEVEL, XML_FORMAT_ALLOWED); if (bytes < 0) { fprintf(stderr, "[ERROR] In Xml::saveToBuffer, xmlNodeDump() error\n"); return false; } *buf = (const char *)(xmlBuf->content); xmlBufferFree(xmlBuf); xmlBuf = NULL; return true; }
int srd_printElementSet (xmlDocPtr doc, xmlNodeSet *nodeSet, char **printBuffPtr, int printBuffSize) { xmlNode *cur_node = NULL; int n, i; xmlChar *value; int offset = 0; xmlBuffer *buff; int size = printBuffSize; char *newSpace; for (i=0; i < nodeSet->nodeNr; i++) { cur_node = nodeSet->nodeTab[i]; if (cur_node->type == XML_ELEMENT_NODE) { buff = xmlBufferCreate (); xmlNodeDump (buff, doc,cur_node, 0, 1 ); if (size < (offset + strlen((char *)buff->content) + 1)){ size = offset + strlen((char *)buff->content) + 1; newSpace = (char *)realloc (*printBuffPtr, size); if (newSpace){ *printBuffPtr = newSpace; } else { // unable to allocate space xmlBufferFree (buff); return -1; } } n = sprintf (*printBuffPtr+offset, "%s", buff->content); offset = offset + n; xmlBufferFree (buff); } } return (offset); }
char *generate_xml(NFE *nfe, EVP_PKEY *key, X509 *cert) { int rc; xmlTextWriterPtr writer; xmlDocPtr doc; xmlBufferPtr buf = xmlBufferCreate(); writer = xmlNewTextWriterDoc(&doc, 0); if (writer == NULL) return NULL; xmlTextWriterStartDocument(writer, NULL, "UTF-8", NULL); rc = gen_inf_nfe(writer, nfe); if (rc < 0) return NULL; xmlTextWriterEndDocument(writer); char *URI = malloc(sizeof(char) * (strlen(nfe->idnfe->chave) + strlen(ID_PREFIX) + 2)); strcpy(URI, "#"); strcat(URI, ID_PREFIX); strcat(URI, nfe->idnfe->chave); rc = sign_xml(doc, key, cert, URI); if(rc) return NULL; xmlNodeDump(buf, NULL, xmlDocGetRootElement(doc), 0, 0); nfe->xml = strdup((char*)buf->content); return (char*)buf->content; }
AEResult AEXMLWriter::CreateXML(const std::wstring& file, bool inMemory) { if (m_IsReady) { AETODO("Better return code"); return AEResult::Fail; } if (file.empty()) { return AEResult::EmptyFilename; } if (inMemory) { m_XMLBuffer = xmlBufferCreate(); if (xmlBufferCreate == nullptr) { AETODO("Better return code"); return AEResult::Fail; } m_XMLWriter = xmlNewTextWriterMemory(m_XMLBuffer, 0); if (m_XMLWriter == NULL) { CleanUp(); AETODO("Better return code"); return AEResult::Fail; } m_InMemory = true; } else { std::string fileStr = AE_Base::WideStr2String(file); m_XMLWriter = xmlNewTextWriterFilename(fileStr.c_str(), 0); if (m_XMLWriter == NULL) { AETODO("Better return code"); return AEResult::Fail; } } int ret = xmlTextWriterStartDocument(m_XMLWriter, NULL, AE_XML_ENCODING, NULL); if (ret < 0) { CleanUp(); AETODO("Better return code"); return AEResult::Fail; } m_Filename = file; m_IsReady = true; return AEResult::Ok; }
std::string XMLSchema::xml() const { xmlBuffer *b = xmlBufferCreate(); xmlTextWriterPtr w = xmlNewTextWriterMemory(b, 0); xmlTextWriterSetIndent(w, 1); xmlTextWriterStartDocument(w, NULL, "utf-8", NULL); xmlTextWriterStartElementNS(w, (const xmlChar*)"pc", (const xmlChar*)"PointCloudSchema", NULL); xmlTextWriterWriteAttributeNS(w, (const xmlChar*) "xmlns", (const xmlChar*)"pc", NULL, (const xmlChar*)"http://pointcloud.org/schemas/PC/"); xmlTextWriterWriteAttributeNS(w, (const xmlChar*)"xmlns", (const xmlChar*)"xsi", NULL, (const xmlChar*)"http://www.w3.org/2001/XMLSchema-instance"); writeXml(w); xmlTextWriterEndElement(w); xmlTextWriterEndDocument(w); std::string output((const char *)b->content, b->use); xmlFreeTextWriter(w); xmlBufferFree(b); return output; }
char *teacher_to_message(teacher_t *self, int id){ if(!self) return NULL; char buff[MSG_LENGTH]; xmlDoc * doc = NULL; xmlNode * rootNode = NULL; xmlNode * teacherNode = NULL; xmlNode * cathedraNode = NULL; doc = xmlNewDoc("1.0"); rootNode = xmlNewNode(NULL, "teachers"); xmlDocSetRootElement(doc, rootNode); char strBuf[100]; teacherNode = xmlNewChild(rootNode, NULL, "teacher", NULL); sprintf(strBuf, "%i", id); xmlNewChild(teacherNode, NULL, "id", strBuf); xmlNewChild(teacherNode, NULL, "firstName", self->name); xmlNewChild(teacherNode, NULL, "lastName", self->surname); xmlNewChild(teacherNode, NULL, "pensionDate", self->date); sprintf(strBuf, "%i", self->hours); xmlNewChild(teacherNode, NULL, "hours", strBuf); sprintf(strBuf, "%f", self->rating); xmlNewChild(teacherNode, NULL, "rating", strBuf); cathedraNode = xmlNewChild(teacherNode, NULL, "cathedra", NULL); xmlNewProp(cathedraNode, "name", self->cathedra->name); xmlNewChild(cathedraNode, NULL, "speciality", self->cathedra->speciality); xmlNewChild(cathedraNode, NULL, "group", self->cathedra->groups[0]); xmlNewChild(cathedraNode, NULL, "group", self->cathedra->groups[1]); xmlBuffer * bufferPtr = xmlBufferCreate(); xmlNodeDump(bufferPtr, NULL, (xmlNode *)doc, 0, 1); sprintf(buff, "%s", (const char*)bufferPtr->content); xmlFreeDoc(doc); xmlCleanupParser(); xmlBufferFree(bufferPtr); return buff; }
/*********************************************** * 生成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; }
char * info_to_xml(char * name, char * group, char * var) { xmlDoc * doc = NULL; xmlNode * rootNode = NULL; doc = xmlNewDoc("1.0"); rootNode = xmlNewNode(NULL, "persons"); //FILE * file = fopen("newBase.xml", "w"); char * text = malloc(sizeof(char) * 1000); for (int i = 0; i < 1000; i++) { text[i] = '\0'; } xmlDocSetRootElement(doc, rootNode); xmlNewChild(rootNode, NULL, "student", name); xmlNewChild(rootNode, NULL, "group", group); xmlNewChild(rootNode, NULL, "variant", var); xmlBuffer * bufferPtr = xmlBufferCreate(); xmlNodeDump(bufferPtr, NULL, (xmlNode *)doc, 0, 1); strcat(text, bufferPtr->content); xmlBufferFree(bufferPtr); xmlFreeDoc(doc); xmlCleanupParser(); puts(text); return text; }
string as_string(xmlNodePtr node) { xmlBufferPtr buf = xmlBufferCreate(); xmlNodeDump(buf,node->doc,node,0,1); string result((char*)buf->content); xmlBufferFree(buf); return result; }
/** * 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; }
static int xsltNumberFormatGetValue(xmlXPathContextPtr context, xmlNodePtr node, xmlChar *value, double *number) { int amount = 0; xmlBufferPtr pattern; xmlXPathObjectPtr obj; pattern = xmlBufferCreate(); if (pattern != NULL) { xmlBufferCCat(pattern, "number("); xmlBufferCat(pattern, value); xmlBufferCCat(pattern, ")"); context->node = node; obj = xmlXPathEvalExpression(xmlBufferContent(pattern), context); if (obj != NULL) { *number = obj->floatval; amount++; xmlXPathFreeObject(obj); } xmlBufferFree(pattern); } return amount; }
bool XMLWrapper::nodeDump(xmlNodePtr node, std::string &content, bool trim) { content.clear(); if (!mDocument) { return false; } if (!node) { return false; } bool result = false; xmlBufferPtr buffer = xmlBufferCreate(); if (buffer) { xmlOutputBufferPtr outputBuffer = xmlOutputBufferCreateBuffer(buffer, NULL); if (outputBuffer) { xmlNodeDumpOutput(outputBuffer, mDocument, node, 0, 0, "UTF8"); xmlOutputBufferClose(outputBuffer); outputBuffer = NULL; result = convertToString(buffer->content, content); if (result && trim) { trimString(content); } } xmlBufferFree(buffer); buffer = NULL; } return result; }
static void testReduce(xmlExpCtxtPtr ctxt, xmlExpNodePtr expr, const char *tst) { xmlBufferPtr xmlExpBuf; xmlExpNodePtr sub, deriv; xmlExpBuf = xmlBufferCreate(); sub = xmlExpParse(ctxt, tst); if (sub == NULL) { printf("Subset %s failed to parse\n", tst); return; } xmlExpDump(xmlExpBuf, sub); printf("Subset parsed as: %s\n", (const char *) xmlBufferContent(xmlExpBuf)); deriv = xmlExpExpDerive(ctxt, expr, sub); if (deriv == NULL) { printf("Derivation led to an internal error, report this !\n"); return; } else { xmlBufferEmpty(xmlExpBuf); xmlExpDump(xmlExpBuf, deriv); if (xmlExpIsNillable(deriv)) printf("Resulting nillable derivation: %s\n", (const char *) xmlBufferContent(xmlExpBuf)); else printf("Resulting derivation: %s\n", (const char *) xmlBufferContent(xmlExpBuf)); xmlExpFree(ctxt, deriv); } xmlExpFree(ctxt, sub); }
char * list_to_xml1(list_t list) { xmlDoc * doc = NULL; xmlNode * rootNode = NULL; xmlNode * studentNode = NULL; xmlNode * groupNode = NULL; char strBuf[100]; char * text = malloc(sizeof (char) * 10000); doc = xmlNewDoc("1.0"); rootNode = xmlNewNode(NULL, "directors"); for (int i = 0;i < list_size(list); i++) { if (list_get(list, i) == NULL) continue; puts(((struct admin_s *)list_get(list, i))->name); xmlDocSetRootElement(doc, rootNode); studentNode = xmlNewChild(rootNode, NULL, "director", NULL); xmlNewChild(studentNode, NULL, "name", ((struct admin_s *)list_get(list, i))->name); xmlNewChild(studentNode, NULL, "surname", ((struct admin_s *)list_get(list, i))->surname); sprintf(strBuf, "%i", ((struct admin_s *)list_get(list, i))->buget); xmlNewChild(studentNode, NULL, "buget", strBuf); sprintf(strBuf, "%llf", ((struct admin_s *)list_get(list, i))->years); xmlNewChild(studentNode, NULL, "years", strBuf); xmlNewChild(studentNode, NULL, "birthdate", ((struct admin_s *)list_get(list, i))->birthdate); } xmlBuffer * bufferPtr = xmlBufferCreate(); xmlNodeDump(bufferPtr, NULL, (xmlNode *)doc, 0, 1); printf("%s", (const char *)bufferPtr->content); strcpy(text, (const char *)bufferPtr->content); xmlBufferFree(bufferPtr); xmlFreeDoc(doc); xmlCleanupParser(); return text; }
VALUE search(VALUE self, VALUE xpathExpr) { VALUE results = rb_ary_new(); xmlDocPtr doc; xmlXPathObjectPtr xpathObj; xmlNodeSetPtr nodes; xmlNodePtr cur; xmlBufferPtr nodeBuffer; int size, i; Data_Get_Struct(self, xmlDoc, doc); xpathObj = eval_and_search(doc, StringValueCStr(xpathExpr)); if (xpathObj == NULL) { return Qnil; } nodes = xpathObj->nodesetval; size = (nodes) ? nodes->nodeNr : 0; for (i = 0; i < size; ++i) { nodeBuffer = xmlBufferCreate(); xmlNodeDump(nodeBuffer, doc, nodes->nodeTab[i], 0, 1); rb_ary_push(results, rb_str_new2(nodeBuffer->content)); xmlBufferFree(nodeBuffer); } xmlXPathFreeObject(xpathObj); return results; }
char* fileinfo_get(char* path) { FILE* file; xmlBufferPtr buf; xmlTextWriterPtr writer; char* ret; int wordCount; file = fopen(path, "r"); wordCount = countWords(file); fclose(file); buf = xmlBufferCreate(); writer = xmlNewTextWriterMemory(buf, 0); xmlTextWriterStartDocument(writer, NULL, XML_E, NULL); xmlTextWriterStartElement(writer, BAD_CAST "fileInfo"); xmlTextWriterWriteFormatElement(writer, BAD_CAST "wordCount", "%d", wordCount); xmlTextWriterEndElement(writer); xmlTextWriterEndDocument(writer); ret = malloc(buf->size); strcpy(ret,(char*)buf->content); xmlBufferFree(buf); return ret; }
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; }
bool c_XMLWriter::t_openmemory() { m_output = xmlBufferCreate(); if (m_output == NULL) { raise_warning("Unable to create output buffer"); return false; } return (m_ptr = xmlNewTextWriterMemory(m_output, 0)); }
const std::string XMLElement::dump(bool indent) const { xmlBufferPtr buffer = xmlBufferCreate(); xmlNodeDump(buffer, doc.getRealDocument(), node, 0, indent ? 1 : 0); std::string str = std::string((const char *)buffer->content); xmlBufferFree(buffer); return str; }
bool c_XMLWriter::t_openmemory() { INSTANCE_METHOD_INJECTION_BUILTIN(XMLWriter, XMLWriter::openmemory); m_output = xmlBufferCreate(); if (m_output == NULL) { raise_warning("Unable to create output buffer"); return false; } return m_ptr = xmlNewTextWriterMemory(m_output, 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; }
xmlChar* PmmFastEncodeString( int charset, const xmlChar *string, const xmlChar *encoding ) { xmlCharEncodingHandlerPtr coder = NULL; xmlChar *retval = NULL; xmlBufferPtr in = NULL, out = NULL; if ( charset == XML_CHAR_ENCODING_UTF8 ) { /* warn("use UTF8 for encoding ... %s ", string); */ return xmlStrdup( string ); } else if ( charset == XML_CHAR_ENCODING_ERROR ) { /* warn("no standard encoding %s\n", encoding); */ coder =xmlFindCharEncodingHandler( (const char *)encoding ); } else if ( charset == XML_CHAR_ENCODING_NONE ) { xs_warn("PmmFastEncodeString: no encoding found\n"); } else { /* warn( "use document encoding %s (%d)", encoding, charset ); */ coder= xmlGetCharEncodingHandler( charset ); } if ( coder != NULL ) { xs_warn("PmmFastEncodeString: coding machine found \n"); in = xmlBufferCreate(); out = xmlBufferCreate(); xmlBufferCCat( in, (const char *) string ); if ( xmlCharEncInFunc( coder, out, in ) >= 0 ) { retval = xmlStrdup( out->content ); /* warn( "encoded string is %s" , retval); */ } else { /* warn( "b0rked encoiding!\n"); */ } xmlBufferFree( in ); xmlBufferFree( out ); xmlCharEncCloseFunc( coder ); } return retval; }
/** * @brief Create a File Writer (libxml2 compatibility function) * @param uri Filename * @param compression Unused * @return A xmlTextWriterPtr object */ static xmlTextWriterPtr xmlNewTextWriterFilename(const char *uri, int compression, char *schemaID) { EXIStream *stream; xmlBufferPtr buf = xmlBufferCreate(); FILE *outfile = fopen(uri, "wb" ); if(!outfile) return NULL; stream = calloc(sizeof(EXIStream), 1); // I: First initialize the header of the stream serialize.initHeader(stream); // II: Set any options in the header, if different from the defaults //stream->header.has_cookie = TRUE; stream->header.has_options = TRUE; //stream->header.opts.valueMaxLength = 300; //stream->header.opts.valuePartitionCapacity = 50; // SET_STRICT(stream->header.opts.enumOpt); SET_PRESERVED(stream->header.opts.preserve, PRESERVE_PREFIXES); // SET_ALIGNMENT(stream->header.opts.enumOpt, BYTE_ALIGNMENT); // III: Define an external stream for the output if any buf->ioStrm.readWriteToStream = writeFileOutputStream; buf->ioStrm.stream = outfile; // IV: Initialize the stream #if EXIP_VERSION==276 if (schemaID && *schemaID) { XoExiLoadSchema (schemaID); String s; asciiToString(schemaID, &s, &stream->memList, FALSE); serialize.initStream(stream, *buf, g_schemaPtr, SCHEMA_ID_SET, &s); } else { serialize.initStream(stream, *buf, g_schemaPtr, SCHEMA_ID_ABSENT, NULL); } #endif #if EXIP_VERSION>=289 if (schemaID && *schemaID) { XoExiLoadSchema (schemaID); asciiToString(schemaID, &stream->header.opts.schemaID, &stream->memList, FALSE); stream->header.opts.schemaIDMode = SCHEMA_ID_SET; } else { stream->header.opts.schemaIDMode = SCHEMA_ID_ABSENT; } serialize.initStream(stream, *buf, g_schemaPtr); #endif // V: Start building the stream step by step: header, document, element etc... serialize.exiHeader(stream); return (xmlTextWriterPtr)stream; }
//! //! Places raw XML result of an xpath query into buf. The query must return //! only one element. //! //! @param[in] xml_path a string containing the path to the XML file to parse //! @param[in] xpath a string contianing the XPATH expression to evaluate //! @param[out] buf for the XML string //! @param[in] buf_len size of the buf //! //! @return EUCA_OK or EUCA_ERROR //! int get_xpath_xml(const char *xml_path, const char *xpath, char *buf, int buf_len) { int ret = EUCA_ERROR; xmlDocPtr doc = NULL; xmlXPathContextPtr context = NULL; xmlXPathObjectPtr result = NULL; xmlNodeSetPtr nodeset = NULL; INIT(); pthread_mutex_lock(&xml_mutex); { if ((doc = xmlParseFile(xml_path)) != NULL) { if ((context = xmlXPathNewContext(doc)) != NULL) { if ((result = xmlXPathEvalExpression(((const xmlChar *)xpath), context)) != NULL) { if (!xmlXPathNodeSetIsEmpty(result->nodesetval)) { nodeset = result->nodesetval; if (nodeset->nodeNr > 1) { fprintf(stderr, "multiple matches for '%s' in '%s'\n", xpath, xml_path); } else { xmlNodePtr node = nodeset->nodeTab[0]->xmlChildrenNode; xmlBufferPtr xbuf = xmlBufferCreate(); if (xbuf) { int len = xmlNodeDump(xbuf, doc, node, 0, 1); if (len < 0) { fprintf(stderr, "failed to extract XML from %s\n", xpath); } else if (len > buf_len) { fprintf(stderr, "insufficient buffer for %s\n", xpath); } else { char *str = (char *)xmlBufferContent(xbuf); euca_strncpy(buf, str, buf_len); ret = EUCA_OK; } xmlBufferFree(xbuf); } else { fprintf(stderr, "failed to allocate XML buffer\n"); } } } xmlXPathFreeObject(result); } else { fprintf(stderr, "no results for '%s' in '%s'\n", xpath, xml_path); } xmlXPathFreeContext(context); } else { fprintf(stderr, "failed to set xpath '%s' context for '%s'\n", xpath, xml_path); } xmlFreeDoc(doc); } else { fprintf(stderr, "failed to parse XML in '%s'\n", xml_path); } } pthread_mutex_unlock(&xml_mutex); return ret; }
std::string XMLConfObj::toString() { xmlIndentTreeOutput = 1; xmlKeepBlanksDefault(0); xmlBufferPtr xmlBufPtr = xmlBufferCreate(); xmlNodeDump(xmlBufPtr, documentTree, currentLevel, 0, 1); std::string ret = std::string((char *)xmlBufPtr->content, xmlBufPtr->use); xmlBufferFree(xmlBufPtr); return ret; }