testWriter.c 38 KB

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