[xml] lot of disk access using libxml2
- From: flo <flo mataf net>
- To: xml gnome org
- Subject: [xml] lot of disk access using libxml2
- Date: Mon, 14 Dec 2009 11:25:35 +0100
Hello,
I use the libxml2 lib to parse and write xml file data.
For this I use the code below:
/* parse the file */
doc = xmlParseFile(xmlFile);
if(doc == NULL) {
fprintf(stderr, "error: could not parse the file %s\n",xmlFile);
return 1;
}
/* get the root element node */
cur = xmlDocGetRootElement(doc);
if(cur == NULL) {
fprintf(stderr, "empty doc %s\n", xmlFile);
xmlFreeDoc(doc);
return 1;
}
/* code path then the xml data in advancing the pointer "cur" * /
/ * Data is stored in a linked list for processing * /
/* free the document */
xmlFreeDoc(doc);
/* free the global variables that may have been
* allocated by the parser */
xmlCleanupParser();
Working with the linked list, and then write the XML file with the
reprocessed data by calling the function:
writeXmlData(struct accesLC * liste, char * fileOut) {
int err = 0;
xmlTextWriterPtr writer;
xmlChar *tmp;
/* Create a new xmlWriter for fileOut */
writer = xmlNewTextWriterFilename(fileOut, 0);
if(writer == NULL) {
fprintf(stderr,"writeXmlData: Error creating the xml writer fileout=
%s\n",fileOut);
return 1;
}
/* Start the document with encoding */
err = xmlTextWriterStartDocument(writer, NULL, "ISO8859-1", NULL);
if(err < 0) {
fprintf(stderr,"writeXmlData: Error at xmlTextWriterStartDocument
\n");
xmlFreeTextWriter(writer);
return 1;
}
/* First element is datafeed */
err = xmlTextWriterStartElement(writer, BAD_CAST "datafeed");
if(err < 0) {
fprintf(stderr,"writeXmlData: Error at xmlTextWriterStartElement for
datafeed\n");
xmlTextWriterEndDocument(writer);
xmlFreeTextWriter(writer);
return 1;
}
/* Retour à la ligne */
err = xmlTextWriterWriteRaw(writer, "\n");
if(err < 0) {
fprintf(stderr,"writeXmlData: Error at xmlTextWriterWriteComment
writing date\n");
xmlTextWriterEndDocument(writer);
xmlFreeTextWriter(writer);
return 1;
}
while(liste->last != NULL) {
/* Secund element is point, as child of datafeed */
err = xmlTextWriterStartElement(writer, BAD_CAST "point");
if(err < 0) {
fprintf(stderr,"writeXmlData: Error at xmlTextWriterStartElement
for point\n");
xmlTextWriterEndDocument(writer);
xmlFreeTextWriter(writer);
return 1;
}
/* Add the attribute f101 f102 f103 f104 with his value */
err = xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "f101", "%
f",
liste->last->data->f101);
if(err < 0) {
fprintf(stderr,"writeXmlData: Error at xmlTextWriterWriteAttribute
for correl\n");
xmlTextWriterEndDocument(writer);
xmlFreeTextWriter(writer);
return 1;
}
err = xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "f102", "%
f",
liste->last->data->f102);
if(err < 0) {
fprintf(stderr,"writeXmlData: Error at xmlTextWriterWriteAttribute
for correl\n");
xmlTextWriterEndDocument(writer);
xmlFreeTextWriter(writer);
return 1;
}
err = xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "f103", "%
f",
liste->last->data->f103);
if(err < 0) {
fprintf(stderr,"writeXmlData: Error at xmlTextWriterWriteAttribute
for correl\n");
xmlTextWriterEndDocument(writer);
xmlFreeTextWriter(writer);
return 1;
}
err = xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "f104", "%
f",
liste->last->data->f104);
if(err < 0) {
fprintf(stderr,"writeXmlData: Error at xmlTextWriterWriteAttribute
for correl\n");
xmlTextWriterEndDocument(writer);
xmlFreeTextWriter(writer);
return 1;
}
/* Write the date as child of point */
err = xmlTextWriterWriteFormatRaw(writer, "%s",
liste->last->data->date);
if(err < 0) {
fprintf(stderr,"writeXmlData: Error at xmlTextWriterWriteComment
writing date\n");
xmlTextWriterEndDocument(writer);
xmlFreeTextWriter(writer);
return 1;
}
/* Close the element named point */
err = xmlTextWriterEndElement(writer);
if(err < 0) {
fprintf(stderr,"writeXmlData: Error at xmlTextWriterEndElement for
point err=%d\n", err);
xmlTextWriterEndDocument(writer);
xmlFreeTextWriter(writer);
return 1;
}
/* Retour à la ligne */
err = xmlTextWriterWriteRaw(writer, "\n");
if(err < 0) {
fprintf(stderr,"writeXmlData: Error at xmlTextWriterWriteComment
writing date\n");
xmlTextWriterEndDocument(writer);
xmlFreeTextWriter(writer);
return 1;
}
liste->last = liste->last->prev;
}
/*fin du document */
/* Here we could close the elements datafeed 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. */
err = xmlTextWriterEndDocument(writer);
if (err < 0) {
fprintf(stderr,"writeXmlData : Error at xmlTextWriterEndDocument
\n");
xmlFreeTextWriter(writer);
return 1;
}
xmlFreeTextWriter(writer);
return 0;
}
My problem: This code generates a lot of disk access. I wanted to know
if I use the correct lib libxml2 to write my file?
I understand the appeal: writer = xmlNewTextWriterFilename (fileout, 0);
allows me to work with a memory pointer and that the call to:
xmlTextWriterEndDocument (), which is writing to disk.
Am I wrong??
thank you for your responses.
Flo
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]