title: libxml2 sax parser - extracting text nodes
body: <p>I wanted to extract values from text nodes from an xml input. I have got the following code from web as the official documentation of libxml has many broken links of which sax parser is one. Please help me obtain the value of a text node. in startElementNs when i tried to look for my text node, i get NULL. Appreciate any help here.</p>
  
  <p>My xml looks like this:</p>
  
  <pre><code>&lt;a&gt;
     &lt;b&gt;
        &lt;c&gt; text values &lt;/c&gt;
     &lt;/b&gt;
  &lt;/a&gt;
  </code></pre>
  
  <p>My code looks like this:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;assert.h&gt;
  #include &lt;memory.h&gt;
  #include &lt;libxml/xmlmemory.h&gt;
  #include &lt;libxml/parser.h&gt;
  #include &lt;string&gt;
  
  
  class ParseFSM
  {
  public:
     /** SAX2 callback when an element start has been detected by the parser. It provides the namespace informations for the element, as well as the new namespace declarations on the element.
        ctx:  the user data (XML parser context)
        localname:  the local name of the element
        prefix:  the element namespace prefix if available
        URI:  the element namespace name if available
        nb_namespaces: number of namespace definitions on that node
        namespaces: pointer to the array of prefix/URI pairs namespace definitions
        nb_attributes: the number of attributes on that node
        nb_defaulted:  the number of defaulted attributes. The defaulted ones are at the end of the array
        attributes: pointer to the array of (localname/prefix/URI/value/end) attribute values.
        **/
    static void startElementNs (void *ctx,
                    const xmlChar * localname,
                    const xmlChar * prefix,
                    const xmlChar * URI,
                    int nb_namespaces,
                    const xmlChar ** namespaces,
                    int nb_attributes,
                    int nb_defaulted, const xmlChar ** attributes)
    {
      ParseFSM &amp; fsm = *(static_cast &lt; ParseFSM * &gt;(ctx));
      printf ("startElementNs: name = '%s' prefix = '%s' uri = (%p)'%s'\n", localname, prefix, URI, URI);
      for (int indexNamespace = 0; indexNamespace &lt; nb_namespaces; ++indexNamespace)
        {
          const xmlChar *prefix = namespaces[indexNamespace * 2];
          const xmlChar *nsURI = namespaces[indexNamespace * 2 + 1];
          printf ("  namespace: name='%s' uri=(%p)'%s'\n", prefix, nsURI, nsURI);
        }
      unsigned int index = 0;
      for (int indexAttribute = 0; indexAttribute &lt; nb_attributes; ++indexAttribute, index += 5)
        {
          const xmlChar *localname = attributes[index];
          const xmlChar *prefix = attributes[index + 1];
          const xmlChar *nsURI = attributes[index + 2];
          const xmlChar *valueBegin = attributes[index + 3];
          const xmlChar *valueEnd = attributes[index + 4];
          std::string value ((const char *) valueBegin, (const char *) valueEnd);
          printf ("  %sattribute: localname='%s', prefix='%s', uri=(%p)'%s', value='%s'\n", indexAttribute &gt;= (nb_attributes - nb_defaulted) ? "defaulted " : "", localname, prefix, nsURI, nsURI, value.c_str ());
        }
    }
     /** SAX2 callback when an element end has been detected by the parser. It provides the namespace informations for the element.
        ctx:  the user data (XML parser context)
        localname:  the local name of the element
        prefix:  the element namespace prefix if available
        URI:  the element namespace name if available
        **/
    static void endElementNs (void *ctx,
                  const xmlChar * localname,
                  const xmlChar * prefix, const xmlChar * URI)
    {
      ParseFSM &amp; fsm = *(static_cast &lt; ParseFSM * &gt;(ctx));
      printf ("endElementNs: name = '%s' prefix = '%s' uri = '%s'\n", localname,
          prefix, URI);
    }
     /** Display and format an error messages, callback.
        ctx:  an XML parser context
        msg:  the message to display/transmit
        ...:  extra parameters for the message display
        */
    static void error (void *ctx, const char *msg, ...)
    {
      ParseFSM &amp; fsm = *(static_cast &lt; ParseFSM * &gt;(ctx));
      va_list args;
      va_start (args, msg);
      vprintf (msg, args);
      va_end (args);
    }
  
     /** Display and format a warning messages, callback.
        ctx:  an XML parser context
        msg:  the message to display/transmit
        ...:  extra parameters for the message display
        */
    static void warning (void *ctx, const char *msg, ...)
    {
      ParseFSM &amp; fsm = *(static_cast &lt; ParseFSM * &gt;(ctx));
      va_list args;
      va_start (args, msg);
      vprintf (msg, args);
      va_end (args);
    }
  };
  int
  main (int argc, const char *argv[])
  {
    std::string xmlIn = "&lt;a&gt;&lt;b&gt;&lt;c&gt; text values &lt;/c&gt; &lt;/b&gt; &lt;/a&gt;"
    /*
     * 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 xmlSAXHandler saxHandler; // See http://xmlsoft.org/html/libxml-tree.html#xmlSAXHandler
    memset (&amp;saxHandler, 0, sizeof (saxHandler));
    // Using xmlSAXVersion( &amp;saxHandler, 2 ) generate crash as it sets plenty of other pointers...
    saxHandler.initialized = XML_SAX2_MAGIC;  // so we do this to force parsing as SAX2.
    saxHandler.startElementNs = &amp;ParseFSM::startElementNs;
    saxHandler.endElementNs = &amp;ParseFSM::endElementNs;
    saxHandler.warning = &amp;ParseFSM::warning;
    saxHandler.error = &amp;ParseFSM::error;
  
    ParseFSM fsm;
    int result =
      xmlSAXUserParseMemory (&amp;saxHandler, &amp;fsm, xmlIn.c_str (),
                 int (xmlIn.length ()));
    if (result != 0)
      {
        printf ("Failed to parse document.\n");
        return 1;
      }
  
    /*
     * Cleanup function for the XML library.
     */
    xmlCleanupParser ();
    /*
     * this is to debug memory for regression tests
     */
    xmlMemoryDump ();
  
    return 0;
  }
  </code></pre>