[xml] Remove whitespaces from text nodes



Hello,

I write a C program using the libxml2 library.
It takes as argument an input XML file and it displays the content at the screen.

This is the input file :
<library name="library of UM2">
        <book name="Design Patterns: Elements of Reusable Object-Oriented Software"
                author="Erich Gamma"
                editor="Addison Wesley"
                isbn="9780201633610">
                My book "A"
        </book>
        <book name="Computer Networks"
                author="Andrew S. Tanenbaum"
                editor="Prentice Hall"
                isbn="9780133499452">
                My book "B"
        </book>
        <book name="Introduction to Algorithms"
                author="Thomas H. Cormen"
                editor="MIT Press"
                isbn="9780262033848">
                My book "C"
        </book>
</library>

This is the output :
$ src/xmlnodelistgetstring data/data.xml 
"
                My book "A"
        "
"
                My book "B"
        "
"
                My book "C"
        "

I would like to have this output (without editing my XML file) :
"My book "A""
"My book "B""
"My book "C""

The useless whitespaces are removed from text nodes.

Is there a function which do this work?

Thank you.

The source code :
#include <libxml2/libxml/parser.h> /* for xmlDocPtr */
int parseDoc(char* file)
{
        xmlDocPtr xmldocptr;
        xmlNodePtr xmlnodeptr;
        xmlChar* xmlchar;
        // load the file
        xmldocptr = xmlParseFile(file);
        if(xmldocptr == NULL)
        {
                fprintf(stderr, "%s:%d: %s: %s\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, "document not 
parsed successfully");
                xmlCleanupParser();
                return -1;
        }
        // get the root element of the document
        xmlnodeptr = xmlDocGetRootElement(xmldocptr);
        if(xmlnodeptr == NULL)
        {
                fprintf(stderr, "%s:%d: %s: %s\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, "empty document");
                xmlFreeDoc(xmldocptr);
                xmlCleanupParser();
                return -1;
        }
        // check the name of the root element
        if(xmlStrcmp(xmlnodeptr->name, (const xmlChar*) "library"))
        {
                fprintf(stderr, "%s:%d: %s: %s\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, "bad root node");
                xmlFreeDoc(xmldocptr);
                return -1;
        }
        // for each children with the "book" name, display the node name
        xmlnodeptr = xmlnodeptr->xmlChildrenNode;
        while(xmlnodeptr != NULL)
        {
                if((!xmlStrcmp(xmlnodeptr->name, (const xmlChar*)"book")))
                {
                        xmlchar = xmlNodeListGetString(xmldocptr, xmlnodeptr->xmlChildrenNode, 1);
                        printf("\"%s\"\n", xmlchar);
                        xmlFree(xmlchar);
                }
                xmlnodeptr = xmlnodeptr->next;
        }
        // free the memory
        xmlFreeDoc(xmldocptr);
        xmlCleanupParser();
        return 0;
}
int main(int argc, char** argv)
{
        // if the file isn't gived as an argument then exit
        if(argc <= 1)
        {
                printf("Usage: %s file.xml\n", argv[0]);
                exit(EXIT_FAILURE);
        }
        // parse the document
        if(parseDoc(argv[1]) == -1)
        {
                fprintf(stderr, "%s:%d: %s: %s\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, "error while 
parsing the document");
                exit(EXIT_FAILURE);
        }
        exit(EXIT_SUCCESS);
}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]