xref: /aosp_15_r20/external/libxml2/example/testWriter.c (revision 7c5688314b92172186c154356a6374bf7684c3ca)
1 /**
2  * section: xmlWriter
3  * synopsis: use various APIs for the xmlWriter
4  * purpose: tests a number of APIs for the xmlWriter, especially
5  *          the various methods to write to a filename, to a memory
6  *          buffer, to a new document, or to a subtree. It shows how to
7  *          do encoding string conversions too. The resulting
8  *          documents are then serialized.
9  * usage: testWriter
10  * test: testWriter && for i in 1 2 3 4 ; do diff $(srcdir)/writer.xml writer$$i.tmp || break ; done
11  * author: Alfred Mickautsch
12  * copy: see Copyright for the status of this software.
13  */
14 #include <stdio.h>
15 #include <string.h>
16 #include <libxml/encoding.h>
17 #include <libxml/xmlwriter.h>
18 #include <libxml/parser.h>
19 
20 #ifndef _WIN32
21 #include <unistd.h>
22 #endif
23 
24 #if defined(LIBXML_WRITER_ENABLED) && defined(LIBXML_OUTPUT_ENABLED)
25 
26 #define MY_ENCODING "ISO-8859-1"
27 
28 void testXmlwriterFilename(const char *uri);
29 void testXmlwriterMemory(void);
30 void testXmlwriterDoc(void);
31 void testXmlwriterTree(void);
32 
33 int
main(void)34 main(void)
35 {
36     /*
37      * this initialize the library and check potential ABI mismatches
38      * between the version it was compiled for and the actual shared
39      * library used.
40      */
41     LIBXML_TEST_VERSION
42 
43     /* first, the file version */
44     testXmlwriterFilename("writer1.tmp");
45     unlink("writer1.tmp");
46 
47     /* next, the memory version */
48     testXmlwriterMemory();
49 
50     /* next, the DOM version */
51     testXmlwriterDoc();
52 
53     /* next, the tree version */
54     testXmlwriterTree();
55 
56     return 0;
57 }
58 
59 /**
60  * testXmlwriterFilename:
61  * @uri: the output URI
62  *
63  * test the xmlWriter interface when writing to a new file
64  */
65 void
testXmlwriterFilename(const char * uri)66 testXmlwriterFilename(const char *uri)
67 {
68     int rc;
69     xmlTextWriterPtr writer;
70 
71     /* Create a new XmlWriter for uri, with no compression. */
72     writer = xmlNewTextWriterFilename(uri, 0);
73     if (writer == NULL) {
74         printf("testXmlwriterFilename: Error creating the xml writer\n");
75         return;
76     }
77 
78     /* Start the document with the xml default for the version,
79      * encoding ISO 8859-1 and the default for the standalone
80      * declaration. */
81     rc = xmlTextWriterStartDocument(writer, NULL, MY_ENCODING, NULL);
82     if (rc < 0) {
83         printf
84             ("testXmlwriterFilename: Error at xmlTextWriterStartDocument\n");
85         return;
86     }
87 
88     /* Start an element named "EXAMPLE". Since this is the first
89      * element, this will be the root element of the document. */
90     rc = xmlTextWriterStartElement(writer, BAD_CAST "EXAMPLE");
91     if (rc < 0) {
92         printf
93             ("testXmlwriterFilename: Error at xmlTextWriterStartElement\n");
94         return;
95     }
96 
97     rc = xmlTextWriterWriteComment(writer, BAD_CAST "This is a comment");
98     if (rc < 0) {
99         printf
100             ("testXmlwriterFilename: Error at xmlTextWriterWriteComment\n");
101         return;
102     }
103 
104     /* Start an element named "ORDER" as child of EXAMPLE. */
105     rc = xmlTextWriterStartElement(writer, BAD_CAST "ORDER");
106     if (rc < 0) {
107         printf
108             ("testXmlwriterFilename: Error at xmlTextWriterStartElement\n");
109         return;
110     }
111 
112     /* Add an attribute with name "version" and value "1.0" to ORDER. */
113     rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "version",
114                                      BAD_CAST "1.0");
115     if (rc < 0) {
116         printf
117             ("testXmlwriterFilename: Error at xmlTextWriterWriteAttribute\n");
118         return;
119     }
120 
121     /* Add an attribute with name "xml:lang" and value "de" to ORDER. */
122     rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "xml:lang",
123                                      BAD_CAST "de");
124     if (rc < 0) {
125         printf
126             ("testXmlwriterFilename: Error at xmlTextWriterWriteAttribute\n");
127         return;
128     }
129 
130     /* Write a comment as child of ORDER */
131     rc = xmlTextWriterWriteComment(writer, BAD_CAST "This is another comment");
132     if (rc < 0) {
133         printf
134             ("testXmlwriterFilename: Error at xmlTextWriterWriteFormatComment\n");
135         return;
136     }
137 
138     /* Start an element named "HEADER" as child of ORDER. */
139     rc = xmlTextWriterStartElement(writer, BAD_CAST "HEADER");
140     if (rc < 0) {
141         printf
142             ("testXmlwriterFilename: Error at xmlTextWriterStartElement\n");
143         return;
144     }
145 
146     /* Write an element named "X_ORDER_ID" as child of HEADER. */
147     rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "X_ORDER_ID",
148                                          "%010d", 53535);
149     if (rc < 0) {
150         printf
151             ("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
152         return;
153     }
154 
155     /* Write an element named "CUSTOMER_ID" as child of HEADER. */
156     rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "CUSTOMER_ID",
157                                          "%d", 1010);
158     if (rc < 0) {
159         printf
160             ("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
161         return;
162     }
163 
164     /* Write an element named "NAME_1" as child of HEADER. */
165     rc = xmlTextWriterWriteElement(writer, BAD_CAST "NAME_1",
166                                    BAD_CAST "Mueller");
167     if (rc < 0) {
168         printf
169             ("testXmlwriterFilename: Error at xmlTextWriterWriteElement\n");
170         return;
171     }
172 
173     /* Write an element named "NAME_2" as child of HEADER. */
174     rc = xmlTextWriterWriteElement(writer, BAD_CAST "NAME_2",
175                                    BAD_CAST "Joerg");
176     if (rc < 0) {
177         printf
178             ("testXmlwriterFilename: Error at xmlTextWriterWriteElement\n");
179         return;
180     }
181 
182     /* Close the element named HEADER. */
183     rc = xmlTextWriterEndElement(writer);
184     if (rc < 0) {
185         printf
186             ("testXmlwriterFilename: Error at xmlTextWriterEndElement\n");
187         return;
188     }
189 
190     /* Start an element named "ENTRIES" as child of ORDER. */
191     rc = xmlTextWriterStartElement(writer, BAD_CAST "ENTRIES");
192     if (rc < 0) {
193         printf
194             ("testXmlwriterFilename: Error at xmlTextWriterStartElement\n");
195         return;
196     }
197 
198     /* Start an element named "ENTRY" as child of ENTRIES. */
199     rc = xmlTextWriterStartElement(writer, BAD_CAST "ENTRY");
200     if (rc < 0) {
201         printf
202             ("testXmlwriterFilename: Error at xmlTextWriterStartElement\n");
203         return;
204     }
205 
206     /* Write an element named "ARTICLE" as child of ENTRY. */
207     rc = xmlTextWriterWriteElement(writer, BAD_CAST "ARTICLE",
208                                    BAD_CAST "<Test>");
209     if (rc < 0) {
210         printf
211             ("testXmlwriterFilename: Error at xmlTextWriterWriteElement\n");
212         return;
213     }
214 
215     /* Write an element named "ENTRY_NO" as child of ENTRY. */
216     rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ENTRY_NO", "%d",
217                                          10);
218     if (rc < 0) {
219         printf
220             ("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
221         return;
222     }
223 
224     /* Close the element named ENTRY. */
225     rc = xmlTextWriterEndElement(writer);
226     if (rc < 0) {
227         printf
228             ("testXmlwriterFilename: Error at xmlTextWriterEndElement\n");
229         return;
230     }
231 
232     /* Start an element named "ENTRY" as child of ENTRIES. */
233     rc = xmlTextWriterStartElement(writer, BAD_CAST "ENTRY");
234     if (rc < 0) {
235         printf
236             ("testXmlwriterFilename: Error at xmlTextWriterStartElement\n");
237         return;
238     }
239 
240     /* Write an element named "ARTICLE" as child of ENTRY. */
241     rc = xmlTextWriterWriteElement(writer, BAD_CAST "ARTICLE",
242                                    BAD_CAST "<Test 2>");
243     if (rc < 0) {
244         printf
245             ("testXmlwriterFilename: Error at xmlTextWriterWriteElement\n");
246         return;
247     }
248 
249     /* Write an element named "ENTRY_NO" as child of ENTRY. */
250     rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ENTRY_NO", "%d",
251                                          20);
252     if (rc < 0) {
253         printf
254             ("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
255         return;
256     }
257 
258     /* Close the element named ENTRY. */
259     rc = xmlTextWriterEndElement(writer);
260     if (rc < 0) {
261         printf
262             ("testXmlwriterFilename: Error at xmlTextWriterEndElement\n");
263         return;
264     }
265 
266     /* Close the element named ENTRIES. */
267     rc = xmlTextWriterEndElement(writer);
268     if (rc < 0) {
269         printf
270             ("testXmlwriterFilename: Error at xmlTextWriterEndElement\n");
271         return;
272     }
273 
274     /* Start an element named "FOOTER" as child of ORDER. */
275     rc = xmlTextWriterStartElement(writer, BAD_CAST "FOOTER");
276     if (rc < 0) {
277         printf
278             ("testXmlwriterFilename: Error at xmlTextWriterStartElement\n");
279         return;
280     }
281 
282     /* Write an element named "TEXT" as child of FOOTER. */
283     rc = xmlTextWriterWriteElement(writer, BAD_CAST "TEXT",
284                                    BAD_CAST "This is a text.");
285     if (rc < 0) {
286         printf
287             ("testXmlwriterFilename: Error at xmlTextWriterWriteElement\n");
288         return;
289     }
290 
291     /* Close the element named FOOTER. */
292     rc = xmlTextWriterEndElement(writer);
293     if (rc < 0) {
294         printf
295             ("testXmlwriterFilename: Error at xmlTextWriterEndElement\n");
296         return;
297     }
298 
299     /* Here we could close the elements ORDER and EXAMPLE using the
300      * function xmlTextWriterEndElement, but since we do not want to
301      * write any other elements, we simply call xmlTextWriterEndDocument,
302      * which will do all the work. */
303     rc = xmlTextWriterEndDocument(writer);
304     if (rc < 0) {
305         printf
306             ("testXmlwriterFilename: Error at xmlTextWriterEndDocument\n");
307         return;
308     }
309 
310     xmlFreeTextWriter(writer);
311 }
312 
313 /**
314  * testXmlwriterMemory:
315  * @file: the output file
316  *
317  * test the xmlWriter interface when writing to memory
318  */
319 void
testXmlwriterMemory(void)320 testXmlwriterMemory(void)
321 {
322     int rc;
323     xmlTextWriterPtr writer;
324     xmlBufferPtr buf;
325 
326     /* Create a new XML buffer, to which the XML document will be
327      * written */
328     buf = xmlBufferCreate();
329     if (buf == NULL) {
330         printf("testXmlwriterMemory: Error creating the xml buffer\n");
331         return;
332     }
333 
334     /* Create a new XmlWriter for memory, with no compression.
335      * Remark: there is no compression for this kind of xmlTextWriter */
336     writer = xmlNewTextWriterMemory(buf, 0);
337     if (writer == NULL) {
338         printf("testXmlwriterMemory: Error creating the xml writer\n");
339         return;
340     }
341 
342     /* Start the document with the xml default for the version,
343      * encoding ISO 8859-1 and the default for the standalone
344      * declaration. */
345     rc = xmlTextWriterStartDocument(writer, NULL, MY_ENCODING, NULL);
346     if (rc < 0) {
347         printf
348             ("testXmlwriterMemory: Error at xmlTextWriterStartDocument\n");
349         return;
350     }
351 
352     /* Start an element named "EXAMPLE". Since this is the first
353      * element, this will be the root element of the document. */
354     rc = xmlTextWriterStartElement(writer, BAD_CAST "EXAMPLE");
355     if (rc < 0) {
356         printf
357             ("testXmlwriterMemory: Error at xmlTextWriterStartElement\n");
358         return;
359     }
360 
361     /* Write a comment as child of EXAMPLE. */
362     rc = xmlTextWriterWriteComment(writer, BAD_CAST "This is a comment");
363     if (rc < 0) {
364         printf
365             ("testXmlwriterMemory: Error at xmlTextWriterWriteComment\n");
366         return;
367     }
368 
369     /* Start an element named "ORDER" as child of EXAMPLE. */
370     rc = xmlTextWriterStartElement(writer, BAD_CAST "ORDER");
371     if (rc < 0) {
372         printf
373             ("testXmlwriterMemory: Error at xmlTextWriterStartElement\n");
374         return;
375     }
376 
377     /* Add an attribute with name "version" and value "1.0" to ORDER. */
378     rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "version",
379                                      BAD_CAST "1.0");
380     if (rc < 0) {
381         printf
382             ("testXmlwriterMemory: Error at xmlTextWriterWriteAttribute\n");
383         return;
384     }
385 
386     /* Add an attribute with name "xml:lang" and value "de" to ORDER. */
387     rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "xml:lang",
388                                      BAD_CAST "de");
389     if (rc < 0) {
390         printf
391             ("testXmlwriterMemory: Error at xmlTextWriterWriteAttribute\n");
392         return;
393     }
394 
395     /* Write a comment as child of ORDER */
396     rc = xmlTextWriterWriteComment(writer, BAD_CAST "This is another comment");
397     if (rc < 0) {
398         printf
399             ("testXmlwriterMemory: Error at xmlTextWriterWriteFormatComment\n");
400         return;
401     }
402 
403     /* Start an element named "HEADER" as child of ORDER. */
404     rc = xmlTextWriterStartElement(writer, BAD_CAST "HEADER");
405     if (rc < 0) {
406         printf
407             ("testXmlwriterMemory: Error at xmlTextWriterStartElement\n");
408         return;
409     }
410 
411     /* Write an element named "X_ORDER_ID" as child of HEADER. */
412     rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "X_ORDER_ID",
413                                          "%010d", 53535);
414     if (rc < 0) {
415         printf
416             ("testXmlwriterMemory: Error at xmlTextWriterWriteFormatElement\n");
417         return;
418     }
419 
420     /* Write an element named "CUSTOMER_ID" as child of HEADER. */
421     rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "CUSTOMER_ID",
422                                          "%d", 1010);
423     if (rc < 0) {
424         printf
425             ("testXmlwriterMemory: Error at xmlTextWriterWriteFormatElement\n");
426         return;
427     }
428 
429     /* Write an element named "NAME_1" as child of HEADER. */
430     rc = xmlTextWriterWriteElement(writer, BAD_CAST "NAME_1",
431                                    BAD_CAST "Mueller");
432     if (rc < 0) {
433         printf
434             ("testXmlwriterMemory: Error at xmlTextWriterWriteElement\n");
435         return;
436     }
437 
438     /* Write an element named "NAME_2" as child of HEADER. */
439     rc = xmlTextWriterWriteElement(writer, BAD_CAST "NAME_2",
440                                    BAD_CAST "Joerg");
441 
442     if (rc < 0) {
443         printf
444             ("testXmlwriterMemory: Error at xmlTextWriterWriteElement\n");
445         return;
446     }
447 
448     /* Close the element named HEADER. */
449     rc = xmlTextWriterEndElement(writer);
450     if (rc < 0) {
451         printf("testXmlwriterMemory: Error at xmlTextWriterEndElement\n");
452         return;
453     }
454 
455     /* Start an element named "ENTRIES" as child of ORDER. */
456     rc = xmlTextWriterStartElement(writer, BAD_CAST "ENTRIES");
457     if (rc < 0) {
458         printf
459             ("testXmlwriterMemory: Error at xmlTextWriterStartElement\n");
460         return;
461     }
462 
463     /* Start an element named "ENTRY" as child of ENTRIES. */
464     rc = xmlTextWriterStartElement(writer, BAD_CAST "ENTRY");
465     if (rc < 0) {
466         printf
467             ("testXmlwriterMemory: Error at xmlTextWriterStartElement\n");
468         return;
469     }
470 
471     /* Write an element named "ARTICLE" as child of ENTRY. */
472     rc = xmlTextWriterWriteElement(writer, BAD_CAST "ARTICLE",
473                                    BAD_CAST "<Test>");
474     if (rc < 0) {
475         printf
476             ("testXmlwriterMemory: Error at xmlTextWriterWriteElement\n");
477         return;
478     }
479 
480     /* Write an element named "ENTRY_NO" as child of ENTRY. */
481     rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ENTRY_NO", "%d",
482                                          10);
483     if (rc < 0) {
484         printf
485             ("testXmlwriterMemory: Error at xmlTextWriterWriteFormatElement\n");
486         return;
487     }
488 
489     /* Close the element named ENTRY. */
490     rc = xmlTextWriterEndElement(writer);
491     if (rc < 0) {
492         printf("testXmlwriterMemory: Error at xmlTextWriterEndElement\n");
493         return;
494     }
495 
496     /* Start an element named "ENTRY" as child of ENTRIES. */
497     rc = xmlTextWriterStartElement(writer, BAD_CAST "ENTRY");
498     if (rc < 0) {
499         printf
500             ("testXmlwriterMemory: Error at xmlTextWriterStartElement\n");
501         return;
502     }
503 
504     /* Write an element named "ARTICLE" as child of ENTRY. */
505     rc = xmlTextWriterWriteElement(writer, BAD_CAST "ARTICLE",
506                                    BAD_CAST "<Test 2>");
507     if (rc < 0) {
508         printf
509             ("testXmlwriterMemory: Error at xmlTextWriterWriteElement\n");
510         return;
511     }
512 
513     /* Write an element named "ENTRY_NO" as child of ENTRY. */
514     rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ENTRY_NO", "%d",
515                                          20);
516     if (rc < 0) {
517         printf
518             ("testXmlwriterMemory: Error at xmlTextWriterWriteFormatElement\n");
519         return;
520     }
521 
522     /* Close the element named ENTRY. */
523     rc = xmlTextWriterEndElement(writer);
524     if (rc < 0) {
525         printf("testXmlwriterMemory: Error at xmlTextWriterEndElement\n");
526         return;
527     }
528 
529     /* Close the element named ENTRIES. */
530     rc = xmlTextWriterEndElement(writer);
531     if (rc < 0) {
532         printf("testXmlwriterMemory: Error at xmlTextWriterEndElement\n");
533         return;
534     }
535 
536     /* Start an element named "FOOTER" as child of ORDER. */
537     rc = xmlTextWriterStartElement(writer, BAD_CAST "FOOTER");
538     if (rc < 0) {
539         printf
540             ("testXmlwriterMemory: Error at xmlTextWriterStartElement\n");
541         return;
542     }
543 
544     /* Write an element named "TEXT" as child of FOOTER. */
545     rc = xmlTextWriterWriteElement(writer, BAD_CAST "TEXT",
546                                    BAD_CAST "This is a text.");
547     if (rc < 0) {
548         printf
549             ("testXmlwriterMemory: Error at xmlTextWriterWriteElement\n");
550         return;
551     }
552 
553     /* Close the element named FOOTER. */
554     rc = xmlTextWriterEndElement(writer);
555     if (rc < 0) {
556         printf("testXmlwriterMemory: Error at xmlTextWriterEndElement\n");
557         return;
558     }
559 
560     /* Here we could close the elements ORDER and EXAMPLE using the
561      * function xmlTextWriterEndElement, but since we do not want to
562      * write any other elements, we simply call xmlTextWriterEndDocument,
563      * which will do all the work. */
564     rc = xmlTextWriterEndDocument(writer);
565     if (rc < 0) {
566         printf("testXmlwriterMemory: Error at xmlTextWriterEndDocument\n");
567         return;
568     }
569 
570     xmlFreeTextWriter(writer);
571 
572     xmlBufferFree(buf);
573 }
574 
575 /**
576  * testXmlwriterDoc:
577  * @file: the output file
578  *
579  * test the xmlWriter interface when creating a new document
580  */
581 void
testXmlwriterDoc(void)582 testXmlwriterDoc(void)
583 {
584     int rc;
585     xmlTextWriterPtr writer;
586     xmlDocPtr doc;
587 
588 
589     /* Create a new XmlWriter for DOM, with no compression. */
590     writer = xmlNewTextWriterDoc(&doc, 0);
591     if (writer == NULL) {
592         printf("testXmlwriterDoc: Error creating the xml writer\n");
593         return;
594     }
595 
596     /* Start the document with the xml default for the version,
597      * encoding ISO 8859-1 and the default for the standalone
598      * declaration. */
599     rc = xmlTextWriterStartDocument(writer, NULL, MY_ENCODING, NULL);
600     if (rc < 0) {
601         printf("testXmlwriterDoc: Error at xmlTextWriterStartDocument\n");
602         return;
603     }
604 
605     /* Start an element named "EXAMPLE". Since this is the first
606      * element, this will be the root element of the document. */
607     rc = xmlTextWriterStartElement(writer, BAD_CAST "EXAMPLE");
608     if (rc < 0) {
609         printf("testXmlwriterDoc: Error at xmlTextWriterStartElement\n");
610         return;
611     }
612 
613     /* Write a comment as child of EXAMPLE. */
614     rc = xmlTextWriterWriteComment(writer, BAD_CAST "This is a comment");
615     if (rc < 0) {
616         printf("testXmlwriterDoc: Error at xmlTextWriterWriteComment\n");
617         return;
618     }
619 
620     /* Start an element named "ORDER" as child of EXAMPLE. */
621     rc = xmlTextWriterStartElement(writer, BAD_CAST "ORDER");
622     if (rc < 0) {
623         printf("testXmlwriterDoc: Error at xmlTextWriterStartElement\n");
624         return;
625     }
626 
627     /* Add an attribute with name "version" and value "1.0" to ORDER. */
628     rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "version",
629                                      BAD_CAST "1.0");
630     if (rc < 0) {
631         printf("testXmlwriterDoc: Error at xmlTextWriterWriteAttribute\n");
632         return;
633     }
634 
635     /* Add an attribute with name "xml:lang" and value "de" to ORDER. */
636     rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "xml:lang",
637                                      BAD_CAST "de");
638     if (rc < 0) {
639         printf("testXmlwriterDoc: Error at xmlTextWriterWriteAttribute\n");
640         return;
641     }
642 
643     /* Write a comment as child of ORDER */
644     rc = xmlTextWriterWriteComment(writer, BAD_CAST "This is another comment");
645     if (rc < 0) {
646         printf
647             ("testXmlwriterDoc: Error at xmlTextWriterWriteFormatComment\n");
648         return;
649     }
650 
651     /* Start an element named "HEADER" as child of ORDER. */
652     rc = xmlTextWriterStartElement(writer, BAD_CAST "HEADER");
653     if (rc < 0) {
654         printf("testXmlwriterDoc: Error at xmlTextWriterStartElement\n");
655         return;
656     }
657 
658     /* Write an element named "X_ORDER_ID" as child of HEADER. */
659     rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "X_ORDER_ID",
660                                          "%010d", 53535);
661     if (rc < 0) {
662         printf
663             ("testXmlwriterDoc: Error at xmlTextWriterWriteFormatElement\n");
664         return;
665     }
666 
667     /* Write an element named "CUSTOMER_ID" as child of HEADER. */
668     rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "CUSTOMER_ID",
669                                          "%d", 1010);
670     if (rc < 0) {
671         printf
672             ("testXmlwriterDoc: Error at xmlTextWriterWriteFormatElement\n");
673         return;
674     }
675 
676     /* Write an element named "NAME_1" as child of HEADER. */
677     rc = xmlTextWriterWriteElement(writer, BAD_CAST "NAME_1",
678                                    BAD_CAST "Mueller");
679     if (rc < 0) {
680         printf("testXmlwriterDoc: Error at xmlTextWriterWriteElement\n");
681         return;
682     }
683 
684     /* Write an element named "NAME_2" as child of HEADER. */
685     rc = xmlTextWriterWriteElement(writer, BAD_CAST "NAME_2",
686                                    BAD_CAST "Joerg");
687     if (rc < 0) {
688         printf("testXmlwriterDoc: Error at xmlTextWriterWriteElement\n");
689         return;
690     }
691 
692     /* Close the element named HEADER. */
693     rc = xmlTextWriterEndElement(writer);
694     if (rc < 0) {
695         printf("testXmlwriterDoc: Error at xmlTextWriterEndElement\n");
696         return;
697     }
698 
699     /* Start an element named "ENTRIES" as child of ORDER. */
700     rc = xmlTextWriterStartElement(writer, BAD_CAST "ENTRIES");
701     if (rc < 0) {
702         printf("testXmlwriterDoc: Error at xmlTextWriterStartElement\n");
703         return;
704     }
705 
706     /* Start an element named "ENTRY" as child of ENTRIES. */
707     rc = xmlTextWriterStartElement(writer, BAD_CAST "ENTRY");
708     if (rc < 0) {
709         printf("testXmlwriterDoc: Error at xmlTextWriterStartElement\n");
710         return;
711     }
712 
713     /* Write an element named "ARTICLE" as child of ENTRY. */
714     rc = xmlTextWriterWriteElement(writer, BAD_CAST "ARTICLE",
715                                    BAD_CAST "<Test>");
716     if (rc < 0) {
717         printf("testXmlwriterDoc: Error at xmlTextWriterWriteElement\n");
718         return;
719     }
720 
721     /* Write an element named "ENTRY_NO" as child of ENTRY. */
722     rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ENTRY_NO", "%d",
723                                          10);
724     if (rc < 0) {
725         printf
726             ("testXmlwriterDoc: Error at xmlTextWriterWriteFormatElement\n");
727         return;
728     }
729 
730     /* Close the element named ENTRY. */
731     rc = xmlTextWriterEndElement(writer);
732     if (rc < 0) {
733         printf("testXmlwriterDoc: Error at xmlTextWriterEndElement\n");
734         return;
735     }
736 
737     /* Start an element named "ENTRY" as child of ENTRIES. */
738     rc = xmlTextWriterStartElement(writer, BAD_CAST "ENTRY");
739     if (rc < 0) {
740         printf("testXmlwriterDoc: Error at xmlTextWriterStartElement\n");
741         return;
742     }
743 
744     /* Write an element named "ARTICLE" as child of ENTRY. */
745     rc = xmlTextWriterWriteElement(writer, BAD_CAST "ARTICLE",
746                                    BAD_CAST "<Test 2>");
747     if (rc < 0) {
748         printf("testXmlwriterDoc: Error at xmlTextWriterWriteElement\n");
749         return;
750     }
751 
752     /* Write an element named "ENTRY_NO" as child of ENTRY. */
753     rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ENTRY_NO", "%d",
754                                          20);
755     if (rc < 0) {
756         printf
757             ("testXmlwriterDoc: Error at xmlTextWriterWriteFormatElement\n");
758         return;
759     }
760 
761     /* Close the element named ENTRY. */
762     rc = xmlTextWriterEndElement(writer);
763     if (rc < 0) {
764         printf("testXmlwriterDoc: Error at xmlTextWriterEndElement\n");
765         return;
766     }
767 
768     /* Close the element named ENTRIES. */
769     rc = xmlTextWriterEndElement(writer);
770     if (rc < 0) {
771         printf("testXmlwriterDoc: Error at xmlTextWriterEndElement\n");
772         return;
773     }
774 
775     /* Start an element named "FOOTER" as child of ORDER. */
776     rc = xmlTextWriterStartElement(writer, BAD_CAST "FOOTER");
777     if (rc < 0) {
778         printf("testXmlwriterDoc: Error at xmlTextWriterStartElement\n");
779         return;
780     }
781 
782     /* Write an element named "TEXT" as child of FOOTER. */
783     rc = xmlTextWriterWriteElement(writer, BAD_CAST "TEXT",
784                                    BAD_CAST "This is a text.");
785     if (rc < 0) {
786         printf("testXmlwriterDoc: Error at xmlTextWriterWriteElement\n");
787         return;
788     }
789 
790     /* Close the element named FOOTER. */
791     rc = xmlTextWriterEndElement(writer);
792     if (rc < 0) {
793         printf("testXmlwriterDoc: Error at xmlTextWriterEndElement\n");
794         return;
795     }
796 
797     /* Here we could close the elements ORDER and EXAMPLE using the
798      * function xmlTextWriterEndElement, but since we do not want to
799      * write any other elements, we simply call xmlTextWriterEndDocument,
800      * which will do all the work. */
801     rc = xmlTextWriterEndDocument(writer);
802     if (rc < 0) {
803         printf("testXmlwriterDoc: Error at xmlTextWriterEndDocument\n");
804         return;
805     }
806 
807     xmlFreeTextWriter(writer);
808 
809     xmlFreeDoc(doc);
810 }
811 
812 /**
813  * testXmlwriterTree:
814  * @file: the output file
815  *
816  * test the xmlWriter interface when writing to a subtree
817  */
818 void
testXmlwriterTree(void)819 testXmlwriterTree(void)
820 {
821     int rc;
822     xmlTextWriterPtr writer;
823     xmlDocPtr doc;
824     xmlNodePtr node;
825 
826     /* Create a new XML DOM tree, to which the XML document will be
827      * written */
828     doc = xmlNewDoc(BAD_CAST XML_DEFAULT_VERSION);
829     if (doc == NULL) {
830         printf
831             ("testXmlwriterTree: Error creating the xml document tree\n");
832         return;
833     }
834 
835     /* Create a new XML node, to which the XML document will be
836      * appended */
837     node = xmlNewDocNode(doc, NULL, BAD_CAST "EXAMPLE", NULL);
838     if (node == NULL) {
839         printf("testXmlwriterTree: Error creating the xml node\n");
840         return;
841     }
842 
843     /* Make ELEMENT the root node of the tree */
844     xmlDocSetRootElement(doc, node);
845 
846     /* Create a new XmlWriter for DOM tree, with no compression. */
847     writer = xmlNewTextWriterTree(doc, node, 0);
848     if (writer == NULL) {
849         printf("testXmlwriterTree: Error creating the xml writer\n");
850         return;
851     }
852 
853     /* Start the document with the xml default for the version,
854      * encoding ISO 8859-1 and the default for the standalone
855      * declaration. */
856     rc = xmlTextWriterStartDocument(writer, NULL, MY_ENCODING, NULL);
857     if (rc < 0) {
858         printf("testXmlwriterTree: Error at xmlTextWriterStartDocument\n");
859         return;
860     }
861 
862     /* Write a comment as child of EXAMPLE. */
863     rc = xmlTextWriterWriteComment(writer, BAD_CAST "This is a comment");
864     if (rc < 0) {
865         printf("testXmlwriterTree: Error at xmlTextWriterWriteComment\n");
866         return;
867     }
868 
869     /* Start an element named "ORDER" as child of EXAMPLE. */
870     rc = xmlTextWriterStartElement(writer, BAD_CAST "ORDER");
871     if (rc < 0) {
872         printf("testXmlwriterTree: Error at xmlTextWriterStartElement\n");
873         return;
874     }
875 
876     /* Add an attribute with name "version" and value "1.0" to ORDER. */
877     rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "version",
878                                      BAD_CAST "1.0");
879     if (rc < 0) {
880         printf
881             ("testXmlwriterTree: Error at xmlTextWriterWriteAttribute\n");
882         return;
883     }
884 
885     /* Add an attribute with name "xml:lang" and value "de" to ORDER. */
886     rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "xml:lang",
887                                      BAD_CAST "de");
888     if (rc < 0) {
889         printf
890             ("testXmlwriterTree: Error at xmlTextWriterWriteAttribute\n");
891         return;
892     }
893 
894     /* Write a comment as child of ORDER */
895     rc = xmlTextWriterWriteComment(writer, BAD_CAST "This is another comment");
896     if (rc < 0) {
897         printf
898             ("testXmlwriterTree: Error at xmlTextWriterWriteFormatComment\n");
899         return;
900     }
901 
902     /* Start an element named "HEADER" as child of ORDER. */
903     rc = xmlTextWriterStartElement(writer, BAD_CAST "HEADER");
904     if (rc < 0) {
905         printf("testXmlwriterTree: Error at xmlTextWriterStartElement\n");
906         return;
907     }
908 
909     /* Write an element named "X_ORDER_ID" as child of HEADER. */
910     rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "X_ORDER_ID",
911                                          "%010d", 53535);
912     if (rc < 0) {
913         printf
914             ("testXmlwriterTree: Error at xmlTextWriterWriteFormatElement\n");
915         return;
916     }
917 
918     /* Write an element named "CUSTOMER_ID" as child of HEADER. */
919     rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "CUSTOMER_ID",
920                                          "%d", 1010);
921     if (rc < 0) {
922         printf
923             ("testXmlwriterTree: Error at xmlTextWriterWriteFormatElement\n");
924         return;
925     }
926 
927     /* Write an element named "NAME_1" as child of HEADER. */
928     rc = xmlTextWriterWriteElement(writer, BAD_CAST "NAME_1",
929                                    BAD_CAST "Mueller");
930     if (rc < 0) {
931         printf("testXmlwriterTree: Error at xmlTextWriterWriteElement\n");
932         return;
933     }
934 
935     /* Write an element named "NAME_2" as child of HEADER. */
936     rc = xmlTextWriterWriteElement(writer, BAD_CAST "NAME_2",
937                                    BAD_CAST "Joerg");
938     if (rc < 0) {
939         printf("testXmlwriterTree: Error at xmlTextWriterWriteElement\n");
940         return;
941     }
942 
943     /* Close the element named HEADER. */
944     rc = xmlTextWriterEndElement(writer);
945     if (rc < 0) {
946         printf("testXmlwriterTree: Error at xmlTextWriterEndElement\n");
947         return;
948     }
949 
950     /* Start an element named "ENTRIES" as child of ORDER. */
951     rc = xmlTextWriterStartElement(writer, BAD_CAST "ENTRIES");
952     if (rc < 0) {
953         printf("testXmlwriterTree: Error at xmlTextWriterStartElement\n");
954         return;
955     }
956 
957     /* Start an element named "ENTRY" as child of ENTRIES. */
958     rc = xmlTextWriterStartElement(writer, BAD_CAST "ENTRY");
959     if (rc < 0) {
960         printf("testXmlwriterTree: Error at xmlTextWriterStartElement\n");
961         return;
962     }
963 
964     /* Write an element named "ARTICLE" as child of ENTRY. */
965     rc = xmlTextWriterWriteElement(writer, BAD_CAST "ARTICLE",
966                                    BAD_CAST "<Test>");
967     if (rc < 0) {
968         printf("testXmlwriterTree: Error at xmlTextWriterWriteElement\n");
969         return;
970     }
971 
972     /* Write an element named "ENTRY_NO" as child of ENTRY. */
973     rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ENTRY_NO", "%d",
974                                          10);
975     if (rc < 0) {
976         printf
977             ("testXmlwriterTree: Error at xmlTextWriterWriteFormatElement\n");
978         return;
979     }
980 
981     /* Close the element named ENTRY. */
982     rc = xmlTextWriterEndElement(writer);
983     if (rc < 0) {
984         printf("testXmlwriterTree: Error at xmlTextWriterEndElement\n");
985         return;
986     }
987 
988     /* Start an element named "ENTRY" as child of ENTRIES. */
989     rc = xmlTextWriterStartElement(writer, BAD_CAST "ENTRY");
990     if (rc < 0) {
991         printf("testXmlwriterTree: Error at xmlTextWriterStartElement\n");
992         return;
993     }
994 
995     /* Write an element named "ARTICLE" as child of ENTRY. */
996     rc = xmlTextWriterWriteElement(writer, BAD_CAST "ARTICLE",
997                                    BAD_CAST "<Test 2>");
998     if (rc < 0) {
999         printf("testXmlwriterTree: Error at xmlTextWriterWriteElement\n");
1000         return;
1001     }
1002 
1003     /* Write an element named "ENTRY_NO" as child of ENTRY. */
1004     rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ENTRY_NO", "%d",
1005                                          20);
1006     if (rc < 0) {
1007         printf
1008             ("testXmlwriterTree: Error at xmlTextWriterWriteFormatElement\n");
1009         return;
1010     }
1011 
1012     /* Close the element named ENTRY. */
1013     rc = xmlTextWriterEndElement(writer);
1014     if (rc < 0) {
1015         printf("testXmlwriterTree: Error at xmlTextWriterEndElement\n");
1016         return;
1017     }
1018 
1019     /* Close the element named ENTRIES. */
1020     rc = xmlTextWriterEndElement(writer);
1021     if (rc < 0) {
1022         printf("testXmlwriterTree: Error at xmlTextWriterEndElement\n");
1023         return;
1024     }
1025 
1026     /* Start an element named "FOOTER" as child of ORDER. */
1027     rc = xmlTextWriterStartElement(writer, BAD_CAST "FOOTER");
1028     if (rc < 0) {
1029         printf("testXmlwriterTree: Error at xmlTextWriterStartElement\n");
1030         return;
1031     }
1032 
1033     /* Write an element named "TEXT" as child of FOOTER. */
1034     rc = xmlTextWriterWriteElement(writer, BAD_CAST "TEXT",
1035                                    BAD_CAST "This is a text.");
1036     if (rc < 0) {
1037         printf("testXmlwriterTree: Error at xmlTextWriterWriteElement\n");
1038         return;
1039     }
1040 
1041     /* Close the element named FOOTER. */
1042     rc = xmlTextWriterEndElement(writer);
1043     if (rc < 0) {
1044         printf("testXmlwriterTree: Error at xmlTextWriterEndElement\n");
1045         return;
1046     }
1047 
1048     /* Here we could close the elements ORDER and EXAMPLE using the
1049      * function xmlTextWriterEndElement, but since we do not want to
1050      * write any other elements, we simply call xmlTextWriterEndDocument,
1051      * which will do all the work. */
1052     rc = xmlTextWriterEndDocument(writer);
1053     if (rc < 0) {
1054         printf("testXmlwriterTree: Error at xmlTextWriterEndDocument\n");
1055         return;
1056     }
1057 
1058     xmlFreeTextWriter(writer);
1059 
1060     xmlFreeDoc(doc);
1061 }
1062 
1063 #else
main(void)1064 int main(void) {
1065     fprintf(stderr, "Writer or output support not compiled in\n");
1066     return 0;
1067 }
1068 #endif
1069