source: trunk/gsdl/src/colservr/expat_document.cpp@ 9189

Last change on this file since 9189 was 9188, checked in by kjdon, 19 years ago

now pass in the section level tag name when getting a document

  • Property svn:keywords set to Author Date Id Revision
File size: 2.2 KB
Line 
1#include <string.h>
2#include <expat.h>
3
4#include "expat_document.h"
5
6#if defined(GSDL_USE_OBJECTSPACE)
7# include <ospace\std\iostream>
8#elif defined(GSDL_USE_IOS_H)
9# include <iostream.h>
10#else
11# include <iostream>
12using namespace std;
13#endif
14
15#include "text_t.h"
16#include "fileutil.h"
17#include "expat_resultset.h"
18
19struct resultpack {
20 text_t text;
21 text_t current_section;
22};
23
24text_t current_text;
25text_t section_num;
26text_t section_level;
27
28bool store_text = false;
29
30
31static void XMLCALL
32startElement(void *userData, const char *name, const char **attributes)
33{
34 text_t element_name = (char*)name;
35 if (element_name == section_level) {
36 text_t id_att = (char *)get_attribute(attributes,"gs2:id");
37 if (id_att == section_num) {
38 store_text = true;
39 }
40
41 }
42}
43
44static void XMLCALL
45endElement(void *userData, const char *name)
46{
47 text_t element_name = (char*)name;
48 if (element_name == section_level) {
49 if (store_text == true) {
50 // we have finished now, can we quit this??
51 store_text = false;
52 }
53 }
54
55}
56
57static void XMLCALL
58characterData(void *userData, const char * text, int len) {
59 if (store_text) {
60 current_text.appendcarr(text, len);
61 }
62}
63int expat_document(const text_t &filename, const text_t &sec_level, const text_t &sec_num, text_t & doc_content){
64
65 current_text.clear();
66 text_t current_section;
67 resultpack rpack = { doc_content, current_section };
68
69 section_num = sec_num;
70 section_level = sec_level;
71 text_t doc_text;
72 read_file(filename, doc_text);
73
74
75 char * c_doc_text = doc_text.getcstr();
76 XML_Parser parser = XML_ParserCreate(NULL);
77
78 XML_SetUserData(parser, &rpack);
79 XML_SetElementHandler(parser, startElement, endElement);
80 XML_SetCharacterDataHandler(parser, characterData);
81 int return_status = 0;
82 const int parse_status
83 = XML_Parse(parser, c_doc_text, strlen(c_doc_text), XML_TRUE);
84
85 if (parse_status == XML_STATUS_ERROR) {
86 fprintf(stderr,
87 "%s at line %d\n",
88 XML_ErrorString(XML_GetErrorCode(parser)),
89 XML_GetCurrentLineNumber(parser));
90 return_status = 1;
91 }
92
93 XML_ParserFree(parser);
94 free(c_doc_text);
95 doc_content.clear();
96 doc_content.append(current_text);
97 return return_status;
98}
99
100
Note: See TracBrowser for help on using the repository browser.