00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
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