123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206 |
- (**
- * section: xmlWriter
- * synopsis: use various APIs for the xmlWriter
- * purpose: tests a number of APIs for the xmlWriter, especially
- * the various methods to write to a filename, to a memory
- * buffer, to a new document, or to a subtree. It shows how to
- * do encoding string conversions too. The resulting
- * documents are then serialized.
- * usage: testWriter
- * test: testWriter && for i in 1 2 3 4 ; do diff $(srcdir)/writer.xml writer$$i.tmp || break ; done
- * author: Alfred Mickautsch
- * copy: see Copyright for the status of this software.
- *)
- program testWriter;
- {$mode objfpc}
- uses
- ctypes,
- xml2,
- exutils,
- SysUtils,
- Strings;
- const
- MY_ENCODING = 'ISO-8859-1';
- (**
- * ConvertInput:
- * @in: string in a given encoding
- * @encoding: the encoding used
- *
- * Converts @in into UTF-8 for processing with libxml2 APIs
- *
- * Returns the converted UTF-8 string, or NULL in case of error.
- *)
- function ConvertInput(const inp, encoding: PAnsiChar): xmlCharPtr;
- var
- ret: cint;
- size: cint;
- out_size: cint;
- temp: cint;
- handler: xmlCharEncodingHandlerPtr;
- begin
- Result := Nil;
- if inp = Nil then
- Exit;
- handler := xmlFindCharEncodingHandler(encoding);
- if handler = Nil then
- begin
- printfn('ConvertInput: no encoding handler found for ''%s''',
- [specialize IfThen<PAnsiChar>(encoding <> Nil, encoding, '')]);
- Exit;
- end;
- size := strlen(inp) + 1;
- out_size := size * 2 - 1;
- Result := xmlCharPtr(xmlMalloc(out_size));
- if Result <> nil then
- begin
- temp := size - 1;
- ret := handler^.input(Result, @out_size, inp, @temp);
- if (ret < 0) or (temp - size + 1 <> 0) then
- begin
- if ret < 0 then
- printfn('ConvertInput: conversion wasn''t successful.')
- else
- printfn('ConvertInput: conversion wasn''t successful. converted: %i octets.',
- [temp]);
- xmlFree(Result);
- Result := Nil;
- end else
- begin
- Result := xmlCharPtr(xmlRealloc(Result, out_size + 1));
- Result[out_size] := #0; (*null terminating out *)
- end;
- end else
- printf('ConvertInput: no mem');
- end;
- (**
- * testXmlwriterFilename:
- * @uri: the output URI
- *
- * test the xmlWriter interface when writing to a new file
- *)
- procedure testXmlwriterFilename(const uri: PAnsiChar);
- var
- rc: cint;
- writer: xmlTextWriterPtr;
- tmp: xmlCharPtr;
- begin
- (* Create a new XmlWriter for uri, with no compression. *)
- writer := xmlNewTextWriterFilename(uri, 0);
- if writer = Nil then
- begin
- printfn('testXmlwriterFilename: Error creating the xml writer');
- Exit;
- end;
- (* Start the document with the xml default for the version,
- * encoding ISO 8859-1 and the default for the standalone
- * declaration. *)
- rc := xmlTextWriterStartDocument(writer, Nil, MY_ENCODING, Nil);
- if rc < 0 then
- begin
- printfn('testXmlwriterFilename: Error at xmlTextWriterStartDocument');
- Exit;
- end;
- (* Start an element named "EXAMPLE". Since thist is the first
- * element, this will be the root element of the document. *)
- rc := xmlTextWriterStartElement(writer, 'EXAMPLE');
- if rc < 0 then
- begin
- printfn('testXmlwriterFilename: Error at xmlTextWriterStartElement');
- Exit;
- end;
- (* Write a comment as child of EXAMPLE.
- * Please observe, that the input to the xmlTextWriter functions
- * HAS to be in UTF-8, even if the output XML is encoded
- * in iso-8859-1 *)
- tmp := ConvertInput('This is a comment with special chars: <'#$E4#$F6#$FC'>',
- MY_ENCODING);
- rc := xmlTextWriterWriteComment(writer, tmp);
- if rc < 0 then
- begin
- printfn('testXmlwriterFilename: Error at xmlTextWriterWriteComment');
- Exit;
- end;
- if tmp <> Nil then
- xmlFree(tmp);
- (* Start an element named "ORDER" as child of EXAMPLE. *)
- rc := xmlTextWriterStartElement(writer, 'ORDER');
- if rc < 0 then
- begin
- printfn('testXmlwriterFilename: Error at xmlTextWriterStartElement');
- Exit;
- end;
- (* Add an attribute with name "version" and value "1.0" to ORDER. *)
- rc := xmlTextWriterWriteAttribute(writer, 'version', '1.0');
- if rc < 0 then
- begin
- printfn('testXmlwriterFilename: Error at xmlTextWriterWriteAttribute');
- Exit;
- end;
- (* Add an attribute with name "xml:lang" and value "de" to ORDER. *)
- rc := xmlTextWriterWriteAttribute(writer, 'xml:lang', 'de');
- if rc < 0 then
- begin
- printfn('testXmlwriterFilename: Error at xmlTextWriterWriteAttribute');
- Exit;
- end;
- (* Write a comment as child of ORDER *)
- tmp := ConvertInput('<'#$E4#$F6#$FC'>', MY_ENCODING);
- rc := xmlTextWriterWriteFormatComment(writer,
- 'This is another comment with special chars: %s', [tmp]);
- if (rc < 0) then
- begin
- printfn('testXmlwriterFilename: Error at xmlTextWriterWriteFormatComment');
- Exit;
- end;
- if tmp <> Nil then
- xmlFree(tmp);
- (* Start an element named "HEADER" as child of ORDER. *)
- rc := xmlTextWriterStartElement(writer, 'HEADER');
- if rc < 0 then
- begin
- printfn('testXmlwriterFilename: Error at xmlTextWriterStartElement');
- Exit;
- end;
- (* Write an element named "X_ORDER_ID" as child of HEADER. *)
- rc := xmlTextWriterWriteFormatElement(writer, 'X_ORDER_ID', '%010d', [53535]);
- if rc < 0 then
- begin
- printf('testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement');
- Exit;
- end;
- (* Write an element named "CUSTOMER_ID" as child of HEADER. *)
- rc := xmlTextWriterWriteFormatElement(writer, 'CUSTOMER_ID', '%d', [1010]);
- if rc < 0 then
- begin
- printf('testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement');
- Exit;
- end;
- (* Write an element named "NAME_1" as child of HEADER. *)
- tmp := ConvertInput('M'#$FC'ller', MY_ENCODING);
- rc := xmlTextWriterWriteElement(writer, 'NAME_1', tmp);
- if rc < 0 then
- begin
- printfn('testXmlwriterFilename: Error at xmlTextWriterWriteElement');
- Exit;
- end;
- if tmp <> Nil then
- xmlFree(tmp);
- (* Write an element named "NAME_2" as child of HEADER. *)
- tmp := ConvertInput('J'#$F6'rg', MY_ENCODING);
- rc := xmlTextWriterWriteElement(writer, 'NAME_2', tmp);
- if rc < 0 then
- begin
- printfn('testXmlwriterFilename: Error at xmlTextWriterWriteElement');
- Exit;
- end;
- if tmp <> Nil then
- xmlFree(tmp);
- (* Close the element named HEADER. *)
- rc := xmlTextWriterEndElement(writer);
- if rc < 0 then
- begin
- printfn('testXmlwriterFilename: Error at xmlTextWriterEndElement');
- Exit;
- end;
- (* Start an element named "ENTRIES" as child of ORDER. *)
- rc := xmlTextWriterStartElement(writer, 'ENTRIES');
- if rc < 0 then
- begin
- printfn('testXmlwriterFilename: Error at xmlTextWriterStartElement');
- Exit;
- end;
- (* Start an element named "ENTRY" as child of ENTRIES. *)
- rc := xmlTextWriterStartElement(writer, 'ENTRY');
- if rc < 0 then
- begin
- printfn('testXmlwriterFilename: Error at xmlTextWriterStartElement');
- Exit;
- end;
- (* Write an element named "ARTICLE" as child of ENTRY. *)
- rc := xmlTextWriterWriteElement(writer, 'ARTICLE', '<Test>');
- if rc < 0 then
- begin
- printfn('testXmlwriterFilename: Error at xmlTextWriterWriteElement');
- Exit;
- end;
- (* Write an element named "ENTRY_NO" as child of ENTRY. *)
- rc := xmlTextWriterWriteFormatElement(writer, 'ENTRY_NO', '%d', [10]);
- if rc < 0 then
- begin
- printfn('testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement');
- Exit;
- end;
- (* Close the element named ENTRY. *)
- rc := xmlTextWriterEndElement(writer);
- if rc < 0 then
- begin
- printfn('testXmlwriterFilename: Error at xmlTextWriterEndElement');
- Exit;
- end;
- (* Start an element named "ENTRY" as child of ENTRIES. *)
- rc := xmlTextWriterStartElement(writer, 'ENTRY');
- if rc < 0 then
- begin
- printfn('testXmlwriterFilename: Error at xmlTextWriterStartElement');
- Exit;
- end;
- (* Write an element named "ARTICLE" as child of ENTRY. *)
- rc := xmlTextWriterWriteElement(writer, 'ARTICLE', '<Test 2>');
- if rc < 0 then
- begin
- printfn('testXmlwriterFilename: Error at xmlTextWriterWriteElement');
- Exit;
- end;
- (* Write an element named "ENTRY_NO" as child of ENTRY. *)
- rc := xmlTextWriterWriteFormatElement(writer, 'ENTRY_NO', '%d', [20]);
- if rc < 0 then
- begin
- printfn('testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement');
- Exit;
- end;
- (* Close the element named ENTRY. *)
- rc := xmlTextWriterEndElement(writer);
- if rc < 0 then
- begin
- printfn('testXmlwriterFilename: Error at xmlTextWriterEndElement');
- Exit;
- end;
- (* Close the element named ENTRIES. *)
- rc := xmlTextWriterEndElement(writer);
- if (rc < 0) then
- begin
- printfn('testXmlwriterFilename: Error at xmlTextWriterEndElement');
- Exit;
- end;
- (* Start an element named "FOOTER" as child of ORDER. *)
- rc := xmlTextWriterStartElement(writer, 'FOOTER');
- if rc < 0 then
- begin
- printfn('testXmlwriterFilename: Error at xmlTextWriterStartElement');
- Exit;
- end;
- (* Write an element named "TEXT" as child of FOOTER. *)
- rc := xmlTextWriterWriteElement(writer, 'TEXT', 'This is a text.');
- if rc < 0 then
- begin
- printfn('testXmlwriterFilename: Error at xmlTextWriterWriteElement');
- Exit;
- end;
- (* Close the element named FOOTER. *)
- rc := xmlTextWriterEndElement(writer);
- if rc < 0 then
- begin
- printfn('testXmlwriterFilename: Error at xmlTextWriterEndElement');
- Exit;
- end;
- (* Here we could close the elements ORDER and EXAMPLE using the
- * function xmlTextWriterEndElement, but since we do not want to
- * write any other elements, we simply call xmlTextWriterEndDocument,
- * which will do all the work. *)
- rc := xmlTextWriterEndDocument(writer);
- if rc < 0 then
- begin
- printfn('testXmlwriterFilename: Error at xmlTextWriterEndDocument');
- Exit;
- end;
- xmlFreeTextWriter(writer);
- end;
- (**
- * testXmlwriterMemory:
- * @file: the output file
- *
- * test the xmlWriter interface when writing to memory
- *)
- procedure testXmlwriterMemory(const _file: PAnsiChar);
- var
- rc: cint;
- writer: xmlTextWriterPtr;
- buf: xmlBufferPtr;
- tmp: xmlCharPtr;
- fp: THandle;
- begin
- (* Create a new XML buffer, to which the XML document will be
- * written *)
- buf := xmlBufferCreate();
- if buf = Nil then
- begin
- printfn('testXmlwriterMemory: Error creating the xml buffer');
- Exit;
- end;
- (* Create a new XmlWriter for memory, with no compression.
- * Remark: there is no compression for this kind of xmlTextWriter *)
- writer := xmlNewTextWriterMemory(buf, 0);
- if writer = Nil then
- begin
- printfn('testXmlwriterMemory: Error creating the xml writer');
- Exit;
- end;
- (* Start the document with the xml default for the version,
- * encoding ISO 8859-1 and the default for the standalone
- * declaration. *)
- rc := xmlTextWriterStartDocument(writer, Nil, MY_ENCODING, Nil);
- if rc < 0 then
- begin
- printfn('testXmlwriterMemory: Error at xmlTextWriterStartDocument');
- Exit;
- end;
- (* Start an element named "EXAMPLE". Since thist is the first
- * element, this will be the root element of the document. *)
- rc := xmlTextWriterStartElement(writer, 'EXAMPLE');
- if rc < 0 then
- begin
- printfn('testXmlwriterMemory: Error at xmlTextWriterStartElement');
- Exit;
- end;
- (* Write a comment as child of EXAMPLE.
- * Please observe, that the input to the xmlTextWriter functions
- * HAS to be in UTF-8, even if the output XML is encoded
- * in iso-8859-1 *)
- tmp := ConvertInput('This is a comment with special chars: <'#$E4#$F6#$FC'>',
- MY_ENCODING);
- rc := xmlTextWriterWriteComment(writer, tmp);
- if rc < 0 then
- begin
- printfn('testXmlwriterMemory: Error at xmlTextWriterWriteComment');
- Exit;
- end;
- if tmp <> Nil then
- xmlFree(tmp);
- (* Start an element named "ORDER" as child of EXAMPLE. *)
- rc := xmlTextWriterStartElement(writer, 'ORDER');
- if rc < 0 then
- begin
- printfn('testXmlwriterMemory: Error at xmlTextWriterStartElement');
- Exit;
- end;
- (* Add an attribute with name "version" and value "1.0" to ORDER. *)
- rc := xmlTextWriterWriteAttribute(writer, 'version', '1.0');
- if rc < 0 then
- begin
- printfn('testXmlwriterMemory: Error at xmlTextWriterWriteAttribute');
- Exit;
- end;
- (* Add an attribute with name "xml:lang" and value "de" to ORDER. *)
- rc := xmlTextWriterWriteAttribute(writer, 'xml:lang', 'de');
- if rc < 0 then
- begin
- printfn('testXmlwriterMemory: Error at xmlTextWriterWriteAttribute');
- Exit;
- end;
- (* Write a comment as child of ORDER *)
- tmp := ConvertInput('<'#$E4#$F6#$FC'>', MY_ENCODING);
- rc := xmlTextWriterWriteFormatComment(writer,
- 'This is another comment with special chars: %s', [tmp]);
- if rc < 0 then
- begin
- printfn('testXmlwriterMemory: Error at xmlTextWriterWriteFormatComment');
- Exit;
- end;
- if tmp <> Nil then
- xmlFree(tmp);
- (* Start an element named "HEADER" as child of ORDER. *)
- rc := xmlTextWriterStartElement(writer, 'HEADER');
- if rc < 0 then
- begin
- printfn('testXmlwriterMemory: Error at xmlTextWriterStartElement\n');
- Exit;
- end;
- (* Write an element named "X_ORDER_ID" as child of HEADER. *)
- rc := xmlTextWriterWriteFormatElement(writer, 'X_ORDER_ID', '%010d', [53535]);
- if rc < 0 then
- begin
- printfn('testXmlwriterMemory: Error at xmlTextWriterWriteFormatElement');
- Exit;
- end;
- (* Write an element named "CUSTOMER_ID" as child of HEADER. *)
- rc := xmlTextWriterWriteFormatElement(writer, 'CUSTOMER_ID', '%d', [1010]);
- if rc < 0 then
- begin
- printfn('testXmlwriterMemory: Error at xmlTextWriterWriteFormatElement\n');
- Exit;
- end;
- (* Write an element named "NAME_1" as child of HEADER. *)
- tmp := ConvertInput('M'#$FC'ller', MY_ENCODING);
- rc := xmlTextWriterWriteElement(writer, 'NAME_1', tmp);
- if rc < 0 then
- begin
- printfn('testXmlwriterMemory: Error at xmlTextWriterWriteElement');
- Exit;
- end;
- if tmp <> Nil then
- xmlFree(tmp);
- (* Write an element named "NAME_2" as child of HEADER. *)
- tmp := ConvertInput('J'#$F6'rg', MY_ENCODING);
- rc := xmlTextWriterWriteElement(writer, 'NAME_2', tmp);
- if rc < 0 then
- begin
- printfn('testXmlwriterMemory: Error at xmlTextWriterWriteElement');
- Exit;
- end;
- if tmp <> Nil then
- xmlFree(tmp);
- (* Close the element named HEADER. *)
- rc := xmlTextWriterEndElement(writer);
- if rc < 0 then
- begin
- printfn('testXmlwriterMemory: Error at xmlTextWriterEndElement');
- Exit;
- end;
- (* Start an element named "ENTRIES" as child of ORDER. *)
- rc := xmlTextWriterStartElement(writer, 'ENTRIES');
- if rc < 0 then
- begin
- printfn('testXmlwriterMemory: Error at xmlTextWriterStartElement');
- Exit;
- end;
- (* Start an element named "ENTRY" as child of ENTRIES. *)
- rc := xmlTextWriterStartElement(writer, 'ENTRY');
- if rc < 0 then
- begin
- printfn('testXmlwriterMemory: Error at xmlTextWriterStartElement');
- Exit;
- end;
- (* Write an element named "ARTICLE" as child of ENTRY. *)
- rc := xmlTextWriterWriteElement(writer, 'ARTICLE', '<Test>');
- if rc < 0 then
- begin
- printfn('testXmlwriterMemory: Error at xmlTextWriterWriteElement');
- Exit;
- end;
- (* Write an element named "ENTRY_NO" as child of ENTRY. *)
- rc := xmlTextWriterWriteFormatElement(writer, 'ENTRY_NO', '%d', [10]);
- if rc < 0 then
- begin
- printfn('testXmlwriterMemory: Error at xmlTextWriterWriteFormatElement');
- Exit;
- end;
- (* Close the element named ENTRY. *)
- rc := xmlTextWriterEndElement(writer);
- if rc < 0 then
- begin
- printfn('testXmlwriterMemory: Error at xmlTextWriterEndElement');
- Exit;
- end;
- (* Start an element named "ENTRY" as child of ENTRIES. *)
- rc := xmlTextWriterStartElement(writer, 'ENTRY');
- if rc < 0 then
- begin
- printfn('testXmlwriterMemory: Error at xmlTextWriterStartElement');
- Exit;
- end;
- (* Write an element named "ARTICLE" as child of ENTRY. *)
- rc := xmlTextWriterWriteElement(writer, 'ARTICLE', '<Test 2>');
- if rc < 0 then
- begin
- printfn('testXmlwriterMemory: Error at xmlTextWriterWriteElement\n');
- Exit;
- end;
- (* Write an element named "ENTRY_NO" as child of ENTRY. *)
- rc := xmlTextWriterWriteFormatElement(writer, 'ENTRY_NO', '%d', [20]);
- if rc < 0 then
- begin
- printfn('testXmlwriterMemory: Error at xmlTextWriterWriteFormatElement');
- Exit;
- end;
- (* Close the element named ENTRY. *)
- rc := xmlTextWriterEndElement(writer);
- if rc < 0 then
- begin
- printfn('testXmlwriterMemory: Error at xmlTextWriterEndElement');
- Exit;
- end;
- (* Close the element named ENTRIES. *)
- rc := xmlTextWriterEndElement(writer);
- if rc < 0 then
- begin
- printfn('testXmlwriterMemory: Error at xmlTextWriterEndElement');
- Exit;
- end;
- (* Start an element named "FOOTER" as child of ORDER. *)
- rc := xmlTextWriterStartElement(writer, 'FOOTER');
- if rc < 0 then
- begin
- printfn('testXmlwriterMemory: Error at xmlTextWriterStartElement');
- Exit;
- end;
- (* Write an element named "TEXT" as child of FOOTER. *)
- rc := xmlTextWriterWriteElement(writer, 'TEXT', 'This is a text.');
- if rc < 0 then
- begin
- printfn('testXmlwriterMemory: Error at xmlTextWriterWriteElement');
- Exit;
- end;
- (* Close the element named FOOTER. *)
- rc := xmlTextWriterEndElement(writer);
- if rc < 0 then
- begin
- printfn('testXmlwriterMemory: Error at xmlTextWriterEndElement');
- Exit;
- end;
- (* Here we could close the elements ORDER and EXAMPLE using the
- * function xmlTextWriterEndElement, but since we do not want to
- * write any other elements, we simply call xmlTextWriterEndDocument,
- * which will do all the work. *)
- rc := xmlTextWriterEndDocument(writer);
- if rc < 0 then
- begin
- printfn('testXmlwriterMemory: Error at xmlTextWriterEndDocument');
- Exit;
- end;
- xmlFreeTextWriter(writer);
- fp := FileCreate(_file);
- FileWrite(fp, buf^.content^, strlen(buf^.content));
- FileClose(fp);
- xmlBufferFree(buf);
- end;
- (**
- * testXmlwriterDoc:
- * @file: the output file
- *
- * test the xmlWriter interface when creating a new document
- *)
- procedure testXmlwriterDoc(const _file: PAnsiChar);
- var
- rc: cint;
- writer: xmlTextWriterPtr;
- tmp: xmlCharPtr;
- doc: xmlDocPtr;
- begin
- (* Create a new XmlWriter for DOM, with no compression. *)
- writer := xmlNewTextWriterDoc(doc, 0);
- if writer = Nil then
- begin
- printfn('testXmlwriterDoc: Error creating the xml writer');
- Exit;
- end;
- (* Start the document with the xml default for the version,
- * encoding ISO 8859-1 and the default for the standalone
- * declaration. *)
- rc := xmlTextWriterStartDocument(writer, Nil, MY_ENCODING, Nil);
- if rc < 0 then
- begin
- printfn('testXmlwriterDoc: Error at xmlTextWriterStartDocument');
- Exit;
- end;
- (* Start an element named 'EXAMPLE'. Since thist is the first
- * element, this will be the root element of the document. *)
- rc := xmlTextWriterStartElement(writer, 'EXAMPLE');
- if rc < 0 then
- begin
- printfn('testXmlwriterDoc: Error at xmlTextWriterStartElement');
- Exit;
- end;
- (* Write a comment as child of EXAMPLE.
- * Please observe, that the input to the xmlTextWriter functions
- * HAS to be in UTF-8, even if the output XML is encoded
- * in iso-8859-1 *)
- tmp := ConvertInput('This is a comment with special chars: <'#$E4#$F6#$FC'>',
- MY_ENCODING);
- rc := xmlTextWriterWriteComment(writer, tmp);
- if rc < 0 then
- begin
- printfn('testXmlwriterDoc: Error at xmlTextWriterWriteComment');
- Exit;
- end;
- if tmp <> Nil then
- xmlFree(tmp);
- (* Start an element named 'ORDER' as child of EXAMPLE. *)
- rc := xmlTextWriterStartElement(writer, 'ORDER');
- if rc < 0 then
- begin
- printfn('testXmlwriterDoc: Error at xmlTextWriterStartElement');
- Exit;
- end;
- (* Add an attribute with name 'version' and value '1.0' to ORDER. *)
- rc := xmlTextWriterWriteAttribute(writer, 'version', '1.0');
- if rc < 0 then
- begin
- printfn('testXmlwriterDoc: Error at xmlTextWriterWriteAttribute');
- Exit;
- end;
- (* Add an attribute with name 'xml:lang' and value 'de' to ORDER. *)
- rc := xmlTextWriterWriteAttribute(writer, 'xml:lang', 'de');
- if rc < 0 then
- begin
- printfn('testXmlwriterDoc: Error at xmlTextWriterWriteAttribute');
- Exit;
- end;
- (* Write a comment as child of ORDER *)
- tmp := ConvertInput('<'#$E4#$F6#$FC'>', MY_ENCODING);
- rc := xmlTextWriterWriteFormatComment(writer,
- 'This is another comment with special chars: %s', [tmp]);
- if rc < 0 then
- begin
- printfn('testXmlwriterDoc: Error at xmlTextWriterWriteFormatComment');
- Exit;
- end;
- if tmp <> Nil then
- xmlFree(tmp);
- (* Start an element named 'HEADER' as child of ORDER. *)
- rc := xmlTextWriterStartElement(writer, 'HEADER');
- if rc < 0 then
- begin
- printfn('testXmlwriterDoc: Error at xmlTextWriterStartElement');
- Exit;
- end;
- (* Write an element named 'X_ORDER_ID' as child of HEADER. *)
- rc := xmlTextWriterWriteFormatElement(writer, 'X_ORDER_ID', '%010d', [53535]);
- if rc < 0 then
- begin
- printfn('testXmlwriterDoc: Error at xmlTextWriterWriteFormatElement');
- Exit;
- end;
- (* Write an element named 'CUSTOMER_ID' as child of HEADER. *)
- rc := xmlTextWriterWriteFormatElement(writer, 'CUSTOMER_ID', '%d', [1010]);
- if rc < 0 then
- begin
- printfn('testXmlwriterDoc: Error at xmlTextWriterWriteFormatElement');
- Exit;
- end;
- (* Write an element named 'NAME_1' as child of HEADER. *)
- tmp := ConvertInput('M'#$FC'ller', MY_ENCODING);
- rc := xmlTextWriterWriteElement(writer, 'NAME_1', tmp);
- if rc < 0 then
- begin
- printfn('testXmlwriterDoc: Error at xmlTextWriterWriteElement');
- Exit;
- end;
- if tmp <> Nil then
- xmlFree(tmp);
- (* Write an element named 'NAME_2' as child of HEADER. *)
- tmp := ConvertInput('J'#$F6'rg', MY_ENCODING);
- rc := xmlTextWriterWriteElement(writer, 'NAME_2', tmp);
- if rc < 0 then
- begin
- printfn('testXmlwriterDoc: Error at xmlTextWriterWriteElement');
- Exit;
- end;
- if tmp <> Nil then
- xmlFree(tmp);
- (* Close the element named HEADER. *)
- rc := xmlTextWriterEndElement(writer);
- if rc < 0 then
- begin
- printfn('testXmlwriterDoc: Error at xmlTextWriterEndElement');
- Exit;
- end;
- (* Start an element named 'ENTRIES' as child of ORDER. *)
- rc := xmlTextWriterStartElement(writer, 'ENTRIES');
- if rc < 0 then
- begin
- printfn('testXmlwriterDoc: Error at xmlTextWriterStartElement');
- Exit;
- end;
- (* Start an element named 'ENTRY' as child of ENTRIES. *)
- rc := xmlTextWriterStartElement(writer, 'ENTRY');
- if rc < 0 then
- begin
- printfn('testXmlwriterDoc: Error at xmlTextWriterStartElement');
- Exit;
- end;
- (* Write an element named 'ARTICLE' as child of ENTRY. *)
- rc := xmlTextWriterWriteElement(writer, 'ARTICLE', '<Test>');
- if rc < 0 then
- begin
- printfn('testXmlwriterDoc: Error at xmlTextWriterWriteElement');
- Exit;
- end;
- (* Write an element named 'ENTRY_NO' as child of ENTRY. *)
- rc := xmlTextWriterWriteFormatElement(writer, 'ENTRY_NO', '%d', [10]);
- if rc < 0 then
- begin
- printfn('testXmlwriterDoc: Error at xmlTextWriterWriteFormatElement');
- Exit;
- end;
- (* Close the element named ENTRY. *)
- rc := xmlTextWriterEndElement(writer);
- if rc < 0 then
- begin
- printfn('testXmlwriterDoc: Error at xmlTextWriterEndElement');
- Exit;
- end;
- (* Start an element named 'ENTRY' as child of ENTRIES. *)
- rc := xmlTextWriterStartElement(writer, 'ENTRY');
- if rc < 0 then
- begin
- printfn('testXmlwriterDoc: Error at xmlTextWriterStartElement');
- Exit;
- end;
- (* Write an element named 'ARTICLE' as child of ENTRY. *)
- rc := xmlTextWriterWriteElement(writer, 'ARTICLE', '<Test 2>');
- if rc < 0 then
- begin
- printfn('testXmlwriterDoc: Error at xmlTextWriterWriteElement');
- Exit;
- end;
- (* Write an element named 'ENTRY_NO' as child of ENTRY. *)
- rc := xmlTextWriterWriteFormatElement(writer, 'ENTRY_NO', '%d', [20]);
- if rc < 0 then
- begin
- printfn('testXmlwriterDoc: Error at xmlTextWriterWriteFormatElement');
- Exit;
- end;
- (* Close the element named ENTRY. *)
- rc := xmlTextWriterEndElement(writer);
- if rc < 0 then
- begin
- printfn('testXmlwriterDoc: Error at xmlTextWriterEndElement');
- Exit;
- end;
- (* Close the element named ENTRIES. *)
- rc := xmlTextWriterEndElement(writer);
- if rc < 0 then
- begin
- printfn('testXmlwriterDoc: Error at xmlTextWriterEndElement');
- Exit;
- end;
- (* Start an element named 'FOOTER' as child of ORDER. *)
- rc := xmlTextWriterStartElement(writer, 'FOOTER');
- if rc < 0 then
- begin
- printfn('testXmlwriterDoc: Error at xmlTextWriterStartElement');
- Exit;
- end;
- (* Write an element named 'TEXT' as child of FOOTER. *)
- rc := xmlTextWriterWriteElement(writer, 'TEXT', 'This is a text.');
- if rc < 0 then
- begin
- printfn('testXmlwriterDoc: Error at xmlTextWriterWriteElement');
- Exit;
- end;
- (* Close the element named FOOTER. *)
- rc := xmlTextWriterEndElement(writer);
- if rc < 0 then
- begin
- printfn('testXmlwriterDoc: Error at xmlTextWriterEndElement');
- Exit;
- end;
- (* Here we could close the elements ORDER and EXAMPLE using the
- * function xmlTextWriterEndElement, but since we do not want to
- * write any other elements, we simply call xmlTextWriterEndDocument,
- * which will do all the work. *)
- rc := xmlTextWriterEndDocument(writer);
- if rc < 0 then
- begin
- printfn('testXmlwriterDoc: Error at xmlTextWriterEndDocument');
- Exit;
- end;
- xmlFreeTextWriter(writer);
- xmlSaveFileEnc(_file, doc, MY_ENCODING);
- xmlFreeDoc(doc);
- end;
- (**
- * testXmlwriterTree:
- * @file: the output file
- *
- * test the xmlWriter interface when writing to a subtree
- *)
- procedure testXmlwriterTree(const _file: PAnsiChar);
- var
- rc: cint;
- writer: xmlTextWriterPtr;
- doc: xmlDocPtr;
- node: xmlNodePtr;
- tmp: xmlCharPtr;
- begin
- (* Create a new XML DOM tree, to which the XML document will be
- * written *)
- doc := xmlNewDoc(XML_DEFAULT_VERSION);
- if doc = Nil then
- begin
- printf('testXmlwriterTree: Error creating the xml document tree');
- Exit;
- end;
- (* Create a new XML node, to which the XML document will be
- * appended *)
- node := xmlNewDocNode(doc, Nil, 'EXAMPLE', Nil);
- if node = Nil then
- begin
- printf('testXmlwriterTree: Error creating the xml node');
- Exit;
- end;
- (* Make ELEMENT the root node of the tree *)
- xmlDocSetRootElement(doc, node);
- (* Create a new XmlWriter for DOM tree, with no compression. *)
- writer := xmlNewTextWriterTree(doc, node, 0);
- if writer = Nil then
- begin
- printf('testXmlwriterTree: Error creating the xml writer');
- Exit;
- end;
- (* Start the document with the xml default for the version,
- * encoding ISO 8859-1 and the default for the standalone
- * declaration. *)
- rc := xmlTextWriterStartDocument(writer, Nil, MY_ENCODING, Nil);
- if rc < 0 then
- begin
- printf('testXmlwriterTree: Error at xmlTextWriterStartDocument');
- Exit;
- end;
- (* Write a comment as child of EXAMPLE.
- * Please observe, that the input to the xmlTextWriter functions
- * HAS to be in UTF-8, even if the output XML is encoded
- * in iso-8859-1 *)
- tmp := ConvertInput('This is a comment with special chars: <'#$E4#$F6#$FC'>',
- MY_ENCODING);
- rc := xmlTextWriterWriteComment(writer, tmp);
- if rc < 0 then
- begin
- printf('testXmlwriterTree: Error at xmlTextWriterWriteComment');
- Exit;
- end;
- if tmp <> Nil then
- xmlFree(tmp);
- (* Start an element named 'ORDER' as child of EXAMPLE. *)
- rc := xmlTextWriterStartElement(writer, 'ORDER');
- if rc < 0 then
- begin
- printf('testXmlwriterTree: Error at xmlTextWriterStartElement');
- Exit;
- end;
- (* Add an attribute with name 'version' and value '1.0' to ORDER. *)
- rc := xmlTextWriterWriteAttribute(writer, 'version', '1.0');
- if rc < 0 then
- begin
- printf('testXmlwriterTree: Error at xmlTextWriterWriteAttribute');
- Exit;
- end;
- (* Add an attribute with name 'xml:lang' and value 'de' to ORDER. *)
- rc := xmlTextWriterWriteAttribute(writer, 'xml:lang', 'de');
- if rc < 0 then
- begin
- printf('testXmlwriterTree: Error at xmlTextWriterWriteAttribute');
- Exit;
- end;
- (* Write a comment as child of ORDER *)
- tmp := ConvertInput('<'#$E4#$F6#$FC'>', MY_ENCODING);
- rc := xmlTextWriterWriteFormatComment(writer,
- 'This is another comment with special chars: %s', [tmp]);
- if rc < 0 then
- begin
- printf('testXmlwriterTree: Error at xmlTextWriterWriteFormatComment');
- Exit;
- end;
- if tmp <> Nil then
- xmlFree(tmp);
- (* Start an element named 'HEADER' as child of ORDER. *)
- rc := xmlTextWriterStartElement(writer, 'HEADER');
- if rc < 0 then
- begin
- printf('testXmlwriterTree: Error at xmlTextWriterStartElement');
- Exit;
- end;
- (* Write an element named 'X_ORDER_ID' as child of HEADER. *)
- rc := xmlTextWriterWriteFormatElement(writer, 'X_ORDER_ID', '%010d', [53535]);
- if rc < 0 then
- begin
- printf('testXmlwriterTree: Error at xmlTextWriterWriteFormatElement');
- Exit;
- end;
- (* Write an element named 'CUSTOMER_ID' as child of HEADER. *)
- rc := xmlTextWriterWriteFormatElement(writer, 'CUSTOMER_ID', '%d', [1010]);
- if rc < 0 then
- begin
- printf('testXmlwriterTree: Error at xmlTextWriterWriteFormatElement');
- Exit;
- end;
- (* Write an element named 'NAME_1' as child of HEADER. *)
- tmp := ConvertInput('M'#$FC'ller', MY_ENCODING);
- rc := xmlTextWriterWriteElement(writer, 'NAME_1', tmp);
- if rc < 0 then
- begin
- printf('testXmlwriterTree: Error at xmlTextWriterWriteElement');
- Exit;
- end;
- if tmp <> Nil then
- xmlFree(tmp);
- (* Write an element named 'NAME_2' as child of HEADER. *)
- tmp := ConvertInput('J'#$F6'rg', MY_ENCODING);
- rc := xmlTextWriterWriteElement(writer, 'NAME_2', tmp);
- if rc < 0 then
- begin
- printf('testXmlwriterTree: Error at xmlTextWriterWriteElement');
- Exit;
- end;
- if tmp <> Nil then
- xmlFree(tmp);
- (* Close the element named HEADER. *)
- rc := xmlTextWriterEndElement(writer);
- if rc < 0 then
- begin
- printf('testXmlwriterTree: Error at xmlTextWriterEndElement');
- Exit;
- end;
- (* Start an element named 'ENTRIES' as child of ORDER. *)
- rc := xmlTextWriterStartElement(writer, 'ENTRIES');
- if rc < 0 then
- begin
- printf('testXmlwriterTree: Error at xmlTextWriterStartElement');
- Exit;
- end;
- (* Start an element named 'ENTRY' as child of ENTRIES. *)
- rc := xmlTextWriterStartElement(writer, 'ENTRY');
- if rc < 0 then
- begin
- printf('testXmlwriterTree: Error at xmlTextWriterStartElement');
- Exit;
- end;
- (* Write an element named 'ARTICLE' as child of ENTRY. *)
- rc := xmlTextWriterWriteElement(writer, 'ARTICLE', '<Test>');
- if rc < 0 then
- begin
- printf('testXmlwriterTree: Error at xmlTextWriterWriteElement');
- Exit;
- end;
- (* Write an element named 'ENTRY_NO' as child of ENTRY. *)
- rc := xmlTextWriterWriteFormatElement(writer, 'ENTRY_NO', '%d', [10]);
- if rc < 0 then
- begin
- printf('testXmlwriterTree: Error at xmlTextWriterWriteFormatElement');
- Exit;
- end;
- (* Close the element named ENTRY. *)
- rc := xmlTextWriterEndElement(writer);
- if rc < 0 then
- begin
- printf('testXmlwriterTree: Error at xmlTextWriterEndElement');
- Exit;
- end;
- (* Start an element named 'ENTRY' as child of ENTRIES. *)
- rc := xmlTextWriterStartElement(writer, 'ENTRY');
- if rc < 0 then
- begin
- printf('testXmlwriterTree: Error at xmlTextWriterStartElement');
- Exit;
- end;
- (* Write an element named 'ARTICLE' as child of ENTRY. *)
- rc := xmlTextWriterWriteElement(writer, 'ARTICLE', '<Test 2>');
- if rc < 0 then
- begin
- printf('testXmlwriterTree: Error at xmlTextWriterWriteElement');
- Exit;
- end;
- (* Write an element named 'ENTRY_NO' as child of ENTRY. *)
- rc := xmlTextWriterWriteFormatElement(writer, 'ENTRY_NO', '%d', [20]);
- if rc < 0 then
- begin
- printf('testXmlwriterTree: Error at xmlTextWriterWriteFormatElement');
- Exit;
- end;
- (* Close the element named ENTRY. *)
- rc := xmlTextWriterEndElement(writer);
- if rc < 0 then
- begin
- printf('testXmlwriterTree: Error at xmlTextWriterEndElement');
- Exit;
- end;
- (* Close the element named ENTRIES. *)
- rc := xmlTextWriterEndElement(writer);
- if rc < 0 then
- begin
- printf('testXmlwriterTree: Error at xmlTextWriterEndElement');
- Exit;
- end;
- (* Start an element named 'FOOTER' as child of ORDER. *)
- rc := xmlTextWriterStartElement(writer, 'FOOTER');
- if rc < 0 then
- begin
- printf('testXmlwriterTree: Error at xmlTextWriterStartElement');
- Exit;
- end;
- (* Write an element named 'TEXT' as child of FOOTER. *)
- rc := xmlTextWriterWriteElement(writer, 'TEXT', 'This is a text.');
- if rc < 0 then
- begin
- printf('testXmlwriterTree: Error at xmlTextWriterWriteElement');
- Exit;
- end;
- (* Close the element named FOOTER. *)
- rc := xmlTextWriterEndElement(writer);
- if rc < 0 then
- begin
- printf('testXmlwriterTree: Error at xmlTextWriterEndElement');
- Exit;
- end;
- (* Here we could close the elements ORDER and EXAMPLE using the
- * function xmlTextWriterEndElement, but since we do not want to
- * write any other elements, we simply call xmlTextWriterEndDocument,
- * which will do all the work. *)
- rc := xmlTextWriterEndDocument(writer);
- if rc < 0 then
- begin
- printf('testXmlwriterTree: Error at xmlTextWriterEndDocument');
- Exit;
- end;
- xmlFreeTextWriter(writer);
- xmlSaveFileEnc(_file, doc, MY_ENCODING);
- xmlFreeDoc(doc);
- end;
- begin
- (*
- * this initialize the library and check potential ABI mismatches
- * between the version it was compiled for and the actual shared
- * library used.
- *)
- LIBXML_TEST_VERSION;
- (* first, the file version *)
- testXmlwriterFilename('writer1.tmp');
- (* next, the memory version *)
- testXmlwriterMemory('writer2.tmp');
- (* next, the DOM version *)
- testXmlwriterDoc('writer3.tmp');
- (* next, the tree version *)
- testXmlwriterTree('writer4.tmp');
- (*
- * Cleanup function for the XML library.
- *)
- xmlCleanupParser();
- (*
- * this is to debug memory for regression tests
- *)
- xmlMemoryDump();
- end.
|