[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] simplexml wrapper for expat?
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.
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|