xmlDoc *XMLDocument::readDocument(const std::string & xmlCode, const char * encoding, bool validate, std::string * error) { xmlParserCtxt *ctxt = initContext(error, validate); xmlDoc *doc = 0; int options = XML_PARSE_NSCLEAN | XML_PARSE_NOBLANKS; if (validate) { options |= XML_PARSE_DTDVALID; } if (!ctxt) { xmlSetGenericErrorFunc(0, errorFunctionWithoutOutput); return 0; } doc = xmlCtxtReadDoc(ctxt, (const xmlChar *)xmlCode.c_str(), 0, encoding, options); if (!doc || !ctxt->valid) { *error = errorBuffer; } xmlSetGenericErrorFunc(0, errorFunctionWithoutOutput); xmlFreeParserCtxt(ctxt); return doc; }
bool CSpmXml::LoadXML(const QString& strXML, bool bDTDValidation) { xmlParserCtxtPtr pXMLParser = xmlNewParserCtxt(); if (0 == pXMLParser) return false; int nOptions = XML_PARSE_NOBLANKS; if (bDTDValidation) nOptions |= XML_PARSE_DTDVALID; m_pXMLDoc = xmlCtxtReadDoc(pXMLParser, (xmlChar*)strXML.toUtf8().data(), "", NULL, nOptions); if (0 == m_pXMLDoc) { xmlFreeParserCtxt(pXMLParser); return false; } // Проверка корректности структуры if (bDTDValidation && (false == pXMLParser->valid)) { xmlFreeParserCtxt(pXMLParser); return false; } m_pCurNode = xmlDocGetRootElement(m_pXMLDoc); if (0 == m_pCurNode) return false; SetModified(false); xmlFreeParserCtxt(pXMLParser); return true; }
//! Parse a document (by URL). //----------------------------------------------------------------------- static bool parse_string(const parser_type &p, const wchar_type *cur, const url_type &url = url_type(), const options_type &opt = options_type()) { p->myDoc = xmlCtxtReadDoc(p, cur, url, "UTF-8", opt); return (p->myDoc != 0); }
char *xml_query_by_id(char *xmlFile, char *xPath, int id) { xmlDocPtr doc; xmlParserCtxtPtr pctxt; xmlXPathContextPtr context; xmlXPathObjectPtr op; xmlNodeSetPtr nodeset; char *data = NULL; int num; if ((xmlFile == NULL) || (strlen(xmlFile) < 1)) return NULL; if (xmlFile[0] != '<') { if (access(xmlFile, R_OK) != 0) { fprintf(stderr, "Error: File %s doesn't exist or is not accessible for reading.\n", xmlFile); return NULL; } doc = xmlParseFile(xmlFile); } else { pctxt = xmlCreateDocParserCtxt((xmlChar *)xmlFile); doc = xmlCtxtReadDoc(pctxt, (xmlChar *)xmlFile, NULL, NULL, XML_PARSE_NOWARNING | XML_PARSE_NOERROR); /* A little hack, but working */ do { char tmp[1024] = { 0 }; char *dtmp = get_datetime(); snprintf(tmp, sizeof(tmp), "[%s ", dtmp); free(dtmp); dtmp=NULL; } while (0); } context = xmlXPathNewContext(doc); if (context == NULL) { DPRINTF("Error in xmlXPathNewContext\n"); return NULL; } DPRINTF("Trying to access xPath node %s (pos %d)\n", xPath, id); op = xmlXPathEvalExpression( (xmlChar *)xPath, context); xmlXPathFreeContext(context); if (op == NULL) { DPRINTF("Error in xmlXPathEvalExpression\n"); return NULL; } if(xmlXPathNodeSetIsEmpty(op->nodesetval)){ xmlXPathFreeObject(op); DPRINTF("No result\n"); return NULL; } nodeset = op->nodesetval; num = nodeset->nodeNr; #if 0 for (i = 0; i < num; i++) { data = (char *)xmlNodeListGetString(doc, (nodeset->nodeTab[i])->xmlChildrenNode, 1); DPRINTF("%d. >>> %s\n", i, data); } #endif DPRINTF("Current num value is %d, id value is %d\n", num, id); if (num > id) { char *tmp = (char *)xmlNodeListGetString(doc, (nodeset->nodeTab[id])->xmlChildrenNode, 1); data = strdup(tmp); DPRINTF("Got data element of '%s'\n", tmp); } else DPRINTF("Trying to access element out of bounds (id > num)\n"); xmlXPathFreeObject(op); xmlFreeDoc(doc); xmlCleanupParser(); return data; }
char *xmlrpc_parse(char *xml) { xmlParserCtxtPtr xp; xmlDocPtr doc; xmlXPathContextPtr context; xmlXPathObjectPtr result; xmlNodeSetPtr nodeset; char *methodName = NULL; char *ret = NULL; int i; _xmlrpc_vars = NULL; _xmlrpc_vars_num = 0; _xlastElementNames = NULL; _xlastElementNames_num = 0; _xIdParent = 0; /* We need to strip the new line characters as it's making issues */ while (strstr(xml, "\n") != NULL) xml = replace(xml, "\n", ""); DPRINTF("Parsing XML:\n%s\n", xml); xp = xmlCreateDocParserCtxt( (xmlChar *)xml ); if (!xp) { DPRINTF("Cannot create DocParserCtxt\n"); return NULL; } doc = xmlCtxtReadDoc(xp, (xmlChar *)xml, NULL, NULL, 0); if (!doc) { DPRINTF("Cannot get xmlDocPtr\n"); return NULL; } context = xmlXPathNewContext(doc); if (!context) { DPRINTF("Cannot get new XPath context\n"); return NULL; } result = xmlXPathEvalExpression( (xmlChar *)"//methodCall/methodName", context); if (!result) { xmlXPathFreeContext(context); DPRINTF("Cannot evaluate expression\n"); goto cleanup; } if(xmlXPathNodeSetIsEmpty(result->nodesetval)){ xmlXPathFreeObject(result); xmlXPathFreeContext(context); DPRINTF("Nothing found\n"); goto cleanup; } nodeset = result->nodesetval; if (nodeset->nodeNr != 1) { xmlXPathFreeObject(result); xmlXPathFreeContext(context); DPRINTF("Invalid number of methodName elements\n"); goto cleanup; } methodName = (char *)xmlNodeListGetString(doc, nodeset->nodeTab[0]->xmlChildrenNode, 1); xmlXPathFreeObject(result); result = xmlXPathEvalExpression( (xmlChar *)"//methodCall/params/param", context); nodeset = result->nodesetval; for (i = 0; i < nodeset->nodeNr; i++) { xmlrpc_process_param(doc, nodeset->nodeTab[i], 0, 1); } ret = strdup(methodName); cleanup: xmlXPathFreeObject(result); xmlFreeDoc(doc); xmlCleanupParser(); return ret; }