Hi all,
I'm very new to libxml and I've started experimenting with xmlreader and
xmlwriter.
I've successully used xmlreader for read a configuration file, and submit
each node to my program.
I'd like now to use xmlwriter to write changes to a new configuration file.
I've tested xmlwriter standalone to create a configuration file and it works
fine.
Now I've written this simple example to read from the config file, check the
value of the current node, and write to another file with xmlwriter, but
I've some problems:
the last xmlWriterEndElement fails with -1, and xmlWriterEndDocument lock
the program (no activity and fixed cursor). As a result the new file is
empty.
If I remove the last xmlWriterEndElement to use only xmlWriterEndDocument to
complete the file and close open nodes the program locks anyway.
If I remove xmlWriterEndDocument I got a segmentation fault end the newfile
si empty.


This is the configuration file to read:
----------------------------------------------------
<config>
  <rss>
    <link>http://rss.cnn.com/rss/edition_europe.rss</link>
    <date>20091016180852</date>
  </rss>
  <rss>
    <link>http://www.linuxfordevices.com/rss.xml</link>
    <date>20091017140723</date>
  </rss>
  <rss>
    <link>http://www.engadget.com/rss.xml</link>
    <date>20091217140723</date>
  </rss>
</config>

And this is my program:
-----------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libxml/xmlreader.h>
#include <libxml/xmlwriter.h>

int isrss;
int i;
int isid;
int islink;
int isdate;

struct {
  char *link;
  long date;
} myfeed;


static void processNode(xmlTextReaderPtr reader, xmlTextWriterPtr writer) {
    xmlChar *name, *value;
    int rc;

    name = xmlTextReaderName(reader);

    if (xmlTextReaderNodeType(reader)==1 && !strcmp(name,"config")) {
      myfeed.link=malloc(1*sizeof(char));
      strcpy(myfeed.link, "");
      myfeed.date=0;
      rc = xmlTextWriterStartElement(writer, BAD_CAST "config");
      if (rc<0) {
        printf("error in xmlTextWriterStartElement - config\n");
      }
    }
    if (xmlTextReaderNodeType(reader)==1 && !strcmp(name,"rss")) {
      isrss=1;
      i++;
      printf("%d° feed\n", i);
      xmlFree(name);
      rc = xmlTextWriterStartElement(writer, BAD_CAST "rss");
      if (rc<0) {
        printf("error in xmlTextWriterStartElement - rss\n");
      }
    }
    if (xmlTextReaderNodeType(reader)==1 && !strcmp(name,"link") && isrss) {
      islink=1;
      xmlFree(name);
    }
    if (xmlTextReaderNodeType(reader)==3 && isrss && islink) {
      value = xmlTextReaderValue(reader);
      strcpy(myfeed.link, (char*)value);
      printf("link of %d° feed: %s\n", i,myfeed.link);
      rc = xmlTextWriterWriteElement(writer, BAD_CAST "link", value);
      if (rc<0) {
        printf("error in xmlTextWriterWriteElement - link\n");
      }
      xmlFree(name);
      xmlFree(value);
    }
    if (xmlTextReaderNodeType(reader)==15 && !strcmp(name,"link")) {
      islink=0;
      xmlFree(name);
    }
    if (xmlTextReaderNodeType(reader)==1 && !strcmp(name,"date") && isrss) {
      isdate=1;
      xmlFree(name);
    }
    if (xmlTextReaderNodeType(reader)==3 && isrss && isdate) {
      value = xmlTextReaderValue(reader);
      myfeed.date=atol(value);
      printf("date of %d° feed: %ld\n", i,myfeed.date);
      rc = xmlTextWriterWriteElement(writer, BAD_CAST "link", value);
      if (rc<0) {
        printf("error in xmlTextWriterWriteElement - date\n");
      }
      xmlFree(name);
      xmlFree(value);
    }
    if (xmlTextReaderNodeType(reader)==15 && !strcmp(name,"date")) {
      isdate=0;
      xmlFree(name);
    }
    if (xmlTextReaderNodeType(reader)==15 && !strcmp(name,"rss")) {
      printf("enf of %d° feed\n", i);
      isrss=0;
      xmlFree(name);
      rc = xmlTextWriterEndElement(writer);
      if (rc<0) {
        printf("error in xmlTextWriterEndElement - rss\n");
      }
      strcpy(myfeed.link, "");
      myfeed.date=0;
    }
    if (xmlTextReaderNodeType(reader)==15 && !strcmp(name,"config")) {
      printf("configuration file ends\n");
      xmlFree(name);
      rc = xmlTextWriterEndElement(writer);
      if (rc<0) {
        printf("error in xmlTextWriterEndElement - config\n");
      }
      rc = xmlTextWriterEndDocument(writer);
      if (rc<0) {
        printf("error in xmlTextWriterEndDocument - config\n");
      }
    }

}

int streamFile(char *filename, char *newfile) {
    xmlTextReaderPtr reader;
    xmlTextWriterPtr writer;

    int ret;

    reader = xmlNewTextReaderFilename(filename);
    writer = xmlNewTextWriterFilename(newfile, 0);
    if (writer == NULL) {
        printf("testXmlwriterFilename: Error creating the xml writer\n");
        return;
    }

    if (reader != NULL) {
        ret = xmlTextReaderRead(reader);
        while (ret == 1) {
            processNode(reader, writer);
            ret = xmlTextReaderRead(reader);
        }
        xmlFreeTextReader(reader);
        xmlFreeTextWriter(writer);

        if (ret != 0) {
            printf("%s : failed to parse\n", filename);
        }
    } else {
        printf("Unable to open %s\n", filename);
    }
}

int main() {
  isrss=isid=islink=isdate=i=0;

  char *filename="/var/config/rss-config";
  char *newfile="/var/config/new-config";

  if (!streamFile(filename, newfile)) {
    printf("error is streamFile call\n");
    exit(0);
 }

}


Tnx for your attention.
_______________________________________________
xml mailing list, project page  http://xmlsoft.org/
[email protected]
http://mail.gnome.org/mailman/listinfo/xml

Reply via email to