OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

 


 

   simplexml wrapper for expat?

[ Lists Home | Date Index | Thread Index ]
  • To: xml-dev@lists.xml.org
  • Subject: simplexml wrapper for expat?
  • From: Aaron Voisine <voisine@gmail.com>
  • Date: Sat, 16 Oct 2004 21:25:13 -0700
  • Domainkey-signature: a=rsa-sha1; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:reply-to:to:subject:mime-version:content-type:content-transfer-encoding; b=SnXwSCvXCAliYJ+n5+iZGjeBJpOS+jHZ0pHZ8PMLdR3JoJFo8LP7ua/5gOg2jX84gd2uOcrvVTks/vMlzqD06W4OLwqO03GaTl1taZoPsxvEGy27SgbseV2lNEiLB1zRnDN9pyMkmRpRe+zLi59t+NNkTxEkG0GDemtvppHtC/s
  • Reply-to: Aaron Voisine <voisine@gmail.com>

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.




 

News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 2001 XML.org. This site is hosted by OASIS