xml_parser.cpp

00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003 // "xml_parser.h"
00004 
00005 // (c) OPAC Team, LIFL, August 2005
00006 
00007 /* This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Lesser General Public
00009    License as published by the Free Software Foundation; either
00010    version 2 of the License, or (at your option) any later version.
00011    
00012    This library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Lesser General Public License for more details.
00016    
00017    You should have received a copy of the GNU Lesser General Public
00018    License along with this library; if not, write to the Free Software
00019    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00020    
00021    Contact: paradiseo-help@lists.gforge.inria.fr
00022 */
00023 
00024 #include <libxml/xmlreader.h>
00025 
00026 #include "xml_parser.h"
00027 
00028 static xmlTextReaderPtr reader;
00029 
00030 void openXMLDocument (const char * __filename) {
00031   
00032   reader = xmlNewTextReaderFilename (__filename);
00033   
00034   if (! reader) {
00035     
00036     fprintf (stderr, "unable to open '%s'.\n", __filename);
00037     exit (1);
00038   }
00039 }
00040 
00041 void closeXMLDocument () {
00042 
00043   xmlFreeTextReader (reader);
00044 }
00045 
00046 std :: string getAttributeValue (const std :: string & __attr) {
00047   
00048   xmlChar * value = xmlTextReaderGetAttribute (reader, (const xmlChar *) __attr.c_str ());
00049   
00050   std :: string str ((const char *) value);
00051   
00052   xmlFree (value);
00053   
00054   return str;
00055 }
00056 
00057 static bool isSep (const xmlChar * __text) {
00058   
00059   for (unsigned i = 0; i < strlen ((char *) __text); i ++)
00060     if (__text [i] != ' ' && __text [i] != '\t' && __text [i] != '\n')
00061       return false;
00062   return true;
00063 }
00064 
00065 std :: string getNextNode () {
00066   
00067   xmlChar * name, * value;
00068 
00069   do {
00070     xmlTextReaderRead (reader);
00071     name = xmlTextReaderName (reader);
00072     value = xmlTextReaderValue (reader);
00073     //    printf ("value = %s\n", value);
00074   } while (! strcmp ((char *) name, "#text") && isSep (value));
00075 
00076   std :: string str;
00077 
00078   if (strcmp ((char *) name, "#text"))
00079     str.assign ((char *) name);
00080   else
00081     str.assign ((char *) value);
00082   
00083   if (name)
00084     xmlFree (name);
00085   if (value)
00086     xmlFree (value);
00087     
00088   return str;
00089 }
00090 

Generated on Sat Dec 30 14:55:29 2006 for ParadisEO-PEO by  doxygen 1.4.7