80 lines
2.6 KiB
Plaintext
80 lines
2.6 KiB
Plaintext
To use parsley from C, the following functions are available from parsley.h. In
|
|
addition, there is a function to convert xml documents of the type returned by
|
|
parsley into json.
|
|
|
|
You will also need passing familiarity with libxml2 and json-c to print, manipulate, and free some of the generated objects.
|
|
|
|
- http://svn.metaparadigm.com/svn/json-c/trunk
|
|
- http://xmlsoft.org/
|
|
|
|
From parsley.h
|
|
=============
|
|
|
|
parsedParsleyPtr -- a struct that contains the following elements:
|
|
- xmlDocPtr xml -- the output of a parslet document parse, as a libxml2 document
|
|
- char *error -- an error message, or NULL if no error
|
|
- compiled_parsley *parsley -- reference to the parsley that did the parsing
|
|
|
|
parsleyPtr parsley_compile(char* parsley, char* incl)
|
|
|
|
Arguments:
|
|
- char* parsley -- a string of parsley to compile.
|
|
- char* incl -- arbitrary XSLT to inject directly into the stylesheet,
|
|
outside any templates.
|
|
|
|
Returns: A structure that you can pass to parsley_parse_* to do the actual
|
|
parsing. This structure contains the compiled XSLT.
|
|
|
|
Notes: This is *NOT* thread-safe. (Usage of the parslet via parsley_parse_* *IS*
|
|
thread-safe, however.)
|
|
|
|
void parsley_free(parsleyPtr);
|
|
|
|
Frees the parsleyPtr's memory.
|
|
|
|
void parsed_parsley_free(parsedParsleyPtr);
|
|
|
|
Frees the parsedParsleyPtr's memory.
|
|
|
|
parsedParsleyPtr parsley_parse_file(parsleyPtr parsley, char* file_name, boolean html);
|
|
|
|
Arguments:
|
|
- parsleyPtr parsley -- Compiled parsley struct
|
|
- char* file_name -- file to parse
|
|
- boolean html -- Use the html parser? (instead of xml)
|
|
|
|
Returns: A libxml2 document of the extracted data. You need to free this
|
|
with xmlFree(). To output, look at the libxml2 documentation for functions
|
|
like xmlSaveFormatFile(). If you want json output, look below for xml2json
|
|
docs.
|
|
|
|
parsedParsleyPtr parsley_parse_string(parsleyPtr parsley, char* string, size_t len, boolean html);
|
|
|
|
Parses the in-memory string/length combination given. See parsley_parse_file
|
|
docs.
|
|
|
|
parsedParsleyPtr parsley_parse_doc(parsleyPtr parsley, xmlDocPtr doc);
|
|
|
|
Uses the parsley parser to parse a libxml2 document.
|
|
|
|
From xml2json.h
|
|
===============
|
|
|
|
struct json_object * xml2json(xmlNodePtr);
|
|
|
|
Converts an xml subtree to json. The xml should be in the format returned
|
|
by parsley. Basically, xml attributes get ignored, and if you want an array
|
|
like [a,b], use:
|
|
|
|
<parsley:groups>
|
|
<parsley:group>a</parsley:group>
|
|
<parsley:group>b</parsley:group>
|
|
</parsley:groups>
|
|
|
|
To get a null-terminated string out, use:
|
|
|
|
json_object_to_json_string(struct json_object *)
|
|
|
|
To free (actually, to decrement the reference count), call:
|
|
|
|
json_object_put(struct json_object *) |