[libxml2] Problem while getting node content

From: Teel (mail_w_kosmos_at_domena.xyz)
Date: 11/10/05

  • Next message: David Schwartz: "Re: static linking broken"
    Date: Thu, 10 Nov 2005 16:48:10 +0100
    
    

    Hi there!
    I have a little(?) problem while getting node content. The code is
    below. It seems that item_subnode->content for summary node doesn't
    work. Trying to do
    cout << strlen((char*)item_subnode->content) << endl;

    I get SEGV :(
    Is this some kind of libxml2 bug or my mistake?

    Regards
    Teel

    You can compile the code like this:

    g++ -Wall -Wno-deprecated test.cc -o test `pkg-config libxml++-2.6
    --cflags --libs`

    *******************************************************************
    #include <libxml/parser.h>
    #include <libxml/tree.h>

    int main() {
       xmlDoc *doc = NULL;
       xmlNode *root_element = NULL, *item_node = NULL, *item_subnode;

       LIBXML_TEST_VERSION

       char* filename = "test.xml";
       doc = xmlReadFile(filename, NULL, 0);

       if (doc == NULL)
          printf("error: could not parse file %s\n", filename);

       root_element = xmlDocGetRootElement(doc); // get the root element node

        if (strcmp((char*)root_element->name,"itemlist") == 0) { //
    processing only <itemlist> childnodes
          for (item_node = root_element->children; item_node; item_node =
    item_node->next) { // processing itemlist (ie. <item>)
            if (item_node->type == XML_ELEMENT_NODE &&
    (strcmp((char*)item_node->name,"item") == 0)) { // processing only
    <item> childnodes
             cout << item_node->name << endl;
             for (item_subnode = item_node->children; item_subnode; item_subnode =
    item_subnode->next) { // processing item childnodes (ie. <summary>)
                if (item_subnode->type == XML_ELEMENT_NODE) {
                   cout << item_subnode->name << " = ";
                   cout << item_subnode->content << endl;
                }
             }
            }
          }
        }

        xmlFreeDoc(doc);
        xmlCleanupParser();

       return 0;
    }
    *******************************************************************

    and this is my test.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <itemlist>
      <item datetime="1234567890">
        <summary>bla bla bla</summary>
      </item>
      <item datetime="1234567890">
        <summary>bzzzz bzzz bzzzz</summary>
      </item>
    </itemlist>


  • Next message: David Schwartz: "Re: static linking broken"