예제 #1
0
/**
 * execute_xpath_expression:
 * @filename:		the input XML filename.
 * @xpathExpr:		the xpath expression for evaluation.
 * @nsList:		the optional list of known namespaces in 
 *			"<prefix1>=<href1> <prefix2>=href2> ..." format.
 *
 * Parses input XML file, evaluates XPath expression and prints results.
 *
 * Returns 0 on success and a negative value otherwise.
 */
int 
execute_xpath_expression(const char* filename, const xmlChar* xpathExpr, const xmlChar* nsList) {
    xmlDocPtr doc;
    xmlXPathContextPtr xpathCtx; 
    xmlXPathObjectPtr xpathObj; 
    
    assert(filename);
    assert(xpathExpr);

    /* Load XML document */
    doc = xmlParseFile(filename);
    if (doc == NULL) {
	fprintf(stderr, "Error: unable to parse file \"%s\"\n", filename);
	return(-1);
    }

    /* process XIncludes to enable the inclusion of dynamic or reference documentation */
    if (0 > xmlXIncludeProcess(doc)) {
	fprintf(stderr, "Error: unable to process xincludes in file \"%s\"\n", filename);
	return(-1);
    }

    /* Create xpath evaluation context */
    xpathCtx = xmlXPathNewContext(doc);
    if(xpathCtx == NULL) {
        fprintf(stderr,"Error: unable to create new XPath context\n");
        xmlFreeDoc(doc); 
        return(-1);
    }
    
    /* Register namespaces from list (if any) */
    if((nsList != NULL) && (register_namespaces(xpathCtx, nsList) < 0)) {
        fprintf(stderr,"Error: failed to register namespaces list \"%s\"\n", nsList);
        xmlXPathFreeContext(xpathCtx); 
        xmlFreeDoc(doc); 
        return(-1);
    }

    /* Evaluate xpath expression */
    xpathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx);
    if(xpathObj == NULL) {
        fprintf(stderr,"Error: unable to evaluate xpath expression \"%s\"\n", xpathExpr);
        xmlXPathFreeContext(xpathCtx); 
        xmlFreeDoc(doc); 
        return(-1);
    }

    /* Print results */
    //print_xpath_nodes(xpathObj->nodesetval, stdout);
    print_xpath_node_values(xpathObj->nodesetval, stdout);

    /* Cleanup */
    xmlXPathFreeObject(xpathObj);
    xmlXPathFreeContext(xpathCtx); 
    xmlFreeDoc(doc); 
    
    return(0);
}
예제 #2
0
char *xml_parser_get_xpath_value(WsXmlDocH doc, const char *expression)
{
	//int i;
	char *result = NULL;
	xmlXPathObject *obj;
	xmlNodeSetPtr nodeset;
	xmlXPathContextPtr ctxt;
	xmlDocPtr d = (xmlDocPtr) doc->parserDoc;
	WsXmlNodeH body;

	ctxt = xmlXPathNewContext(d);
	if (ctxt == NULL) {
		error("failed while creating xpath context");
		return NULL;
	}
	body = ws_xml_get_soap_body(doc);
	register_namespaces(ctxt, doc, xml_parser_get_root(doc));
	if (ws_xml_get_child(body, 0, NULL, NULL)) {
		register_namespaces(ctxt, doc,
				ws_xml_get_child(body, 0, NULL, NULL));
	}

	obj = xmlXPathEvalExpression(BAD_CAST expression, ctxt);
	if (obj) {
		nodeset = obj->nodesetval;
		if (nodeset && nodeset->nodeNr > 0)
			result = (char *) xmlNodeListGetString(d,
					nodeset->
					nodeTab[0]->
					xmlChildrenNode,
					1);

		xmlXPathFreeContext(ctxt);
		xmlXPathFreeObject(obj);
	} else {
		return NULL;
	}

	return result;
}
예제 #3
0
int xml_parser_check_xpath(WsXmlDocH doc, const char *expression)
{
	xmlXPathObject *obj;
	xmlNodeSetPtr nodeset;
	xmlXPathContextPtr ctxt;
	xmlDocPtr d = (xmlDocPtr) doc->parserDoc;
	int retval = 0;

	ctxt = xmlXPathNewContext(d);
	if (ctxt == NULL) {
		error("failed while creating xpath context");
		return 0;
	}
	register_namespaces(ctxt, doc, xml_parser_get_root(doc));
	obj = xmlXPathEvalExpression(BAD_CAST expression, ctxt);
	if (obj) {
		nodeset = obj->nodesetval;
		if (nodeset && nodeset->nodeNr > 0) {
			int size = nodeset->nodeNr;
			int i;
			xmlNodePtr cur;
			for(i = 0; i < size; ++i) {
				if(nodeset->nodeTab[i]->type == XML_ELEMENT_NODE) {
					cur = nodeset->nodeTab[i];
					if(cur->ns) {
						fprintf(stdout, "= element node \"%s:%s\"\n",
								cur->ns->href, cur->name);
					} else {
						fprintf(stdout, "= element node \"%s\"\n",
								cur->name);
					}
				}
			}

			retval = 1;
		}
		xmlXPathFreeContext(ctxt);
		xmlXPathFreeObject(obj);
	} else {
		return 0;
	}

	return retval;
}
예제 #4
0
파일: iwb_loader.c 프로젝트: fourks/ardesia
/* Load an iwb file and create the list of save-point. */
GSList *
load_iwb (gchar *iwbfile)
{
    const gchar *tmpdir = g_get_tmp_dir ();
    GSList *savepoint_list = (GSList *) NULL;
    gchar  *ardesia_tmp_dir = g_build_filename (tmpdir, PACKAGE_NAME, (gchar *) 0);
    gchar  *project_name = get_project_name ();
    gchar  *project_tmp_dir = g_build_filename (ardesia_tmp_dir, project_name, (gchar *) 0);
    gchar  *content_filename = "content.xml";
    gchar  *content_filepath = g_build_filename (project_tmp_dir, content_filename, (gchar *) 0);
    xmlDocPtr doc = (xmlDocPtr) NULL; // the resulting document tree
    xmlXPathContextPtr context = (xmlXPathContextPtr) NULL;

    decompress_iwb (iwbfile, project_tmp_dir);

    /* Initialize libxml. */
    xmlInitParser ();

    /*
     * This initialize the library and check potential ABI mismatches
     * between the version it was compiled for and the actual shared
     * library used.
     */
    LIBXML_TEST_VERSION

    /*
     * Build an XML tree from a the file.
     */
    doc = xmlParseFile (content_filepath);

    if (doc == NULL)
    {
        printf ("Failed to parse %s\n", content_filepath);
        exit (EXIT_FAILURE);
    }

    context = xmlXPathNewContext (doc);

    if (context == NULL)
    {
        xmlFreeDoc (doc);
        printf ("Error: unable to create new XPath context\n");
        exit (EXIT_FAILURE);
    }

    context = register_namespaces (context);

    savepoint_list = load_savepoints_by_iwb (savepoint_list, project_tmp_dir, context);

    g_remove (content_filepath);

    xmlXPathFreeContext (context);

    xmlFreeDoc (doc);
    doc = NULL;

    /*
     * Cleanup function for the XML library.
     */
    xmlCleanupParser ();

    g_free (ardesia_tmp_dir);
    ardesia_tmp_dir = NULL;

    g_free (project_tmp_dir);
    project_tmp_dir = NULL;

    g_free (content_filepath);
    content_filepath = NULL;

    return savepoint_list;
}