[
Lists Home |
Date Index |
Thread Index
]
Aaron Voisine wrote:
> I've been spoiled by simpleXML for PHP. I need to do some xml parsing
> in C and was wondering if there's anything remotely similar for C.
The Ali project has similar motivations, probably from similar pain!
It's just too complicated to use many of the XML parsers, resulting in
many lines of code per XML data read! Why should it be so much more
work than fscanf?
> The following example shows how easy it would be to, for instance,
Similar code using Ali would look something like this:
root = ali_open(&doc, "library.xml", ALI_OPTION_NONE, NULL);
library = ali_in(doc, root, "^e", 0, "library");
while (shelf = ali_in(doc, library , "^e", 0, "shelf")) != 0) {
while (book = ali_in(doc, shelf , "^e", 0, "book")) != 0) {
ali_in(doc, book , "^e%as", 0, "title", &title);
printf("Title: %s\n", title);
ali_in(doc, book , "^e%as", 0, "author", &author);
printf("Author: %s\n", author);
}
}
ali_close(doc);
I think they're fairly similar. Ali is a bit larger. It uses
document/element pairs instead of just elements. It has zeros for the
namespace. And the printf calls are on separate lines. Most important,
they're both small enough to comprehend, with about one line of C code
per XML data!
Does anybody have time to make a SAX example? :)
>I couldn't find anything so I started writing my own on top of expat.
A reasonable choice. Ali targets structured data like preference files
and saved data which are fairly controlled, and I wanted the parser
smaller than the program, so it has it's own parser. It obviously does
less, but the omissions aren't missed much.
I was inspecting a program Friday. It is 512KB. It uses Xerces, with a
2MB dll. It uses Xerces to load and save preferences and
configurations. Now it's a 2.5 MB app!
-Roger Flores
http://ali.sourceforge.net/
Aaron Voisine wrote:
> I've been spoiled by simpleXML for PHP. I need to do some xml parsing
> in C and was wondering if there's anything remotely similar for C. I
> couldn't find anything so I started writing my own on top of expat. It
> will load all the xml data into memory at once just like simpleXML.
> The following example shows how easy it would be to, for instance,
> print out all the titles and authors in an xml file called library.xml
> organized like:
>
> <library>
> <shelf>
> <book>
> <title>title</title>
> <author>author</author>
> </book>
> ...
> </shelf>
> ...
> </library>
>
> ==========
>
> ezxml_t library = ezxml_load_file(fopen("library.xml", "r")), shelf, book;
>
> shelf = ezxml_child(library, "shelf");
> for (; shelf; shelf = shelf->next) {
> book = ezxml_child(shelf, "book");
> for (; book; book = book->next) {
> printf("Title: %s\n", ezxml_child(book, "title")->txt);
> printf("Author: %s\n\n", ezxml_child(book, "author")->txt);
> }
> }
> ezxml_free(library);
>
> ========
>
> Or if you you just wanted the title of the second book on the first shelf:
>
> ezxml_t library = ezxml_load_file(fopen("library.xml", "r"));
>
> printf("Title: %s\n", ezxml_get(library, "shelf", 0, "book", 1,
> "title", -1)->txt);
> ezxml_free(library);
>
> =========
>
> the -1 in ezxml_get idicates the end of the arguement list, since
> it'll be using variable argument lists for spanning arbitrary tree
> depths.
>
> My second question is about expat's memory handling. When a handler
> gets passed a pointer to some data from the xml document, I assume
> that memory gets freed when the hander returns? This requires me make
> a copy of any data my handlers recieve. Is there any way to tell expat
> not to free the data until XML_ParserFree is called? That way I
> wouldn't have to allocate my own memory and copy it.
>
> Once it's finished I hope to release as an open source project
> assuming I haven't missed a project that already does this.
>
> -----------------------------------------------------------------
> The xml-dev list is sponsored by XML.org <http://www.xml.org>, an
> initiative of OASIS <http://www.oasis-open.org>
>
> The list archives are at http://lists.xml.org/archives/xml-dev/
>
> To subscribe or unsubscribe from this list use the subscription
> manager: <http://www.oasis-open.org/mlmanage/index.php>
>
>
|