2 * gjobread.c : a small test program for gnome jobs XML format
4 * See Copyright for the status of this software.
6 * Daniel.Veillard@w3.org
14 * This example should compile and run indifferently with libxml-1.8.8 +
16 * Check the COMPAT comments below
20 * COMPAT using xml-config --cflags to get the include path this will
23 #include <libxml/xmlmemory.h>
24 #include <libxml/parser.h>
26 #define DEBUG(x) printf(x)
30 * an xmlChar * is really an UTF8 encoded char string (0 terminated)
32 typedef struct person
{
36 xmlChar
*organisation
;
43 * And the code needed to parse it
46 parsePerson(xmlDocPtr doc
, xmlNsPtr ns
, xmlNodePtr cur
) {
49 DEBUG("parsePerson\n");
53 ret
= (personPtr
) malloc(sizeof(person
));
55 fprintf(stderr
,"out of memory\n");
58 memset(ret
, 0, sizeof(person
));
60 /* We don't care what the top level element name is */
61 /* COMPAT xmlChildrenNode is a macro unifying libxml1 and libxml2 names */
62 cur
= cur
->xmlChildrenNode
;
64 if ((!xmlStrcmp(cur
->name
, (const xmlChar
*)"Person")) &&
66 ret
->name
= xmlNodeListGetString(doc
, cur
->xmlChildrenNode
, 1);
67 if ((!xmlStrcmp(cur
->name
, (const xmlChar
*)"Email")) &&
69 ret
->email
= xmlNodeListGetString(doc
, cur
->xmlChildrenNode
, 1);
80 printPerson(personPtr cur
) {
81 if (cur
== NULL
) return;
82 printf("------ Person\n");
83 if (cur
->name
) printf(" name: %s\n", cur
->name
);
84 if (cur
->email
) printf(" email: %s\n", cur
->email
);
85 if (cur
->company
) printf(" company: %s\n", cur
->company
);
86 if (cur
->organisation
) printf(" organisation: %s\n", cur
->organisation
);
87 if (cur
->smail
) printf(" smail: %s\n", cur
->smail
);
88 if (cur
->webPage
) printf(" Web: %s\n", cur
->webPage
);
89 if (cur
->phone
) printf(" phone: %s\n", cur
->phone
);
94 * a Description for a Job
102 personPtr developers
[100]; /* using dynamic alloc is left as an exercise */
106 * And the code needed to parse it
109 parseJob(xmlDocPtr doc
, xmlNsPtr ns
, xmlNodePtr cur
) {
114 * allocate the struct
116 ret
= (jobPtr
) malloc(sizeof(job
));
118 fprintf(stderr
,"out of memory\n");
121 memset(ret
, 0, sizeof(job
));
123 /* We don't care what the top level element name is */
124 cur
= cur
->xmlChildrenNode
;
125 while (cur
!= NULL
) {
127 if ((!xmlStrcmp(cur
->name
, (const xmlChar
*) "Project")) &&
129 ret
->projectID
= xmlGetProp(cur
, (const xmlChar
*) "ID");
130 if (ret
->projectID
== NULL
) {
131 fprintf(stderr
, "Project has no ID\n");
134 if ((!xmlStrcmp(cur
->name
, (const xmlChar
*) "Application")) &&
137 xmlNodeListGetString(doc
, cur
->xmlChildrenNode
, 1);
138 if ((!xmlStrcmp(cur
->name
, (const xmlChar
*) "Category")) &&
141 xmlNodeListGetString(doc
, cur
->xmlChildrenNode
, 1);
142 if ((!xmlStrcmp(cur
->name
, (const xmlChar
*) "Contact")) &&
144 ret
->contact
= parsePerson(doc
, ns
, cur
);
155 printJob(jobPtr cur
) {
158 if (cur
== NULL
) return;
159 printf("======= Job\n");
160 if (cur
->projectID
!= NULL
) printf("projectID: %s\n", cur
->projectID
);
161 if (cur
->application
!= NULL
) printf("application: %s\n", cur
->application
);
162 if (cur
->category
!= NULL
) printf("category: %s\n", cur
->category
);
163 if (cur
->contact
!= NULL
) printPerson(cur
->contact
);
164 printf("%d developers\n", cur
->nbDevelopers
);
166 for (i
= 0;i
< cur
->nbDevelopers
;i
++) printPerson(cur
->developers
[i
]);
167 printf("======= \n");
171 * A pool of Gnome Jobs
173 typedef struct gjob
{
175 jobPtr jobs
[500]; /* using dynamic alloc is left as an exercise */
180 parseGjobFile(char *filename
) {
187 #ifdef LIBXML_SAX1_ENABLED
189 * build an XML tree from a the file;
191 doc
= xmlParseFile(filename
);
192 if (doc
== NULL
) return(NULL
);
195 * the library has been compiled without some of the old interfaces
198 #endif /* LIBXML_SAX1_ENABLED */
201 * Check the document is of the right kind
204 cur
= xmlDocGetRootElement(doc
);
206 fprintf(stderr
,"empty document\n");
210 ns
= xmlSearchNsByHref(doc
, cur
,
211 (const xmlChar
*) "http://www.gnome.org/some-location");
214 "document of the wrong type, GJob Namespace not found\n");
218 if (xmlStrcmp(cur
->name
, (const xmlChar
*) "Helping")) {
219 fprintf(stderr
,"document of the wrong type, root node != Helping");
225 * Allocate the structure to be returned.
227 ret
= (gJobPtr
) malloc(sizeof(gJob
));
229 fprintf(stderr
,"out of memory\n");
233 memset(ret
, 0, sizeof(gJob
));
236 * Now, walk the tree.
238 /* First level we expect just Jobs */
239 cur
= cur
->xmlChildrenNode
;
240 while ( cur
&& xmlIsBlankNode ( cur
) )
246 if ((xmlStrcmp(cur
->name
, (const xmlChar
*) "Jobs")) || (cur
->ns
!= ns
)) {
247 fprintf(stderr
,"document of the wrong type, was '%s', Jobs expected",
249 fprintf(stderr
,"xmlDocDump follows\n");
250 #ifdef LIBXML_OUTPUT_ENABLED
251 xmlDocDump ( stderr
, doc
);
252 fprintf(stderr
,"xmlDocDump finished\n");
253 #endif /* LIBXML_OUTPUT_ENABLED */
259 /* Second level is a list of Job, but be laxist */
260 cur
= cur
->xmlChildrenNode
;
261 while (cur
!= NULL
) {
262 if ((!xmlStrcmp(cur
->name
, (const xmlChar
*) "Job")) &&
264 curjob
= parseJob(doc
, ns
, cur
);
266 ret
->jobs
[ret
->nbJobs
++] = curjob
;
267 if (ret
->nbJobs
>= 500) break;
276 handleGjob(gJobPtr cur
) {
280 * Do whatever you want and free the structure.
282 printf("%d Jobs registered\n", cur
->nbJobs
);
283 for (i
= 0; i
< cur
->nbJobs
; i
++) printJob(cur
->jobs
[i
]);
286 int main(int argc
, char **argv
) {
290 /* COMPAT: Do not genrate nodes for formatting spaces */
292 xmlKeepBlanksDefault(0);
294 for (i
= 1; i
< argc
; i
++) {
295 cur
= parseGjobFile(argv
[i
]);
299 fprintf( stderr
, "Error parsing file '%s'\n", argv
[i
]);
303 /* Clean up everything else before quitting. */