[xml] Streaming XPath in libxml2




Hi,

My apologies if this mailing list is not the intended target.

The version of libxml2 and the OS on which it is deployed is displayed
below.

-----------------------------------------------------------------------
shell$ xml2-config --version
2.6.11

shell$ cat /proc/version
Linux version 2.6.9-1.667smp (bhcompile tweety build redhat com) (gcc
version 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)) #1 SMP Tue Nov 2 14:59:52
EST 2004
-----------------------------------------------------------------------

I would like to know how to use streaming XPath. Googling on libxml2
about this shows favorable search results.

The usage scenario follows:

-----------------------------------------------------------------------
//
//open an XML file for reading
FILE *xmlFile;

if ((xmlFile = fopen(argv[1] , "r")) == NULL)
{
   ...
}

//
// read a chunk/fragment of XML
// Evaluate an XPath on the chunk of XML
do
{
   char buffer[BUFFER_SIZE];

   int readlen = fread (buffer, 1, BUFFER_SIZE, xmlFile);

   //
   // Parse the XML fragment
   xmlDocPtr doc = xmlSAXParseMemory(sax, buffer, BUFFER_SIZE, 0);

   if (doc != NULL)
   {
      xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc);
      xmlXPathObjectPtr xpathObj = xmlXPathEvalExpression(
                                   xpathExpr, xpathCtx);

      //
      // Print the XPath results

      xmlXPathFreeObject(xpathObj);
      xmlXPathFreeContext(xpathCtx);
      xmlFreeDoc(doc);
   }
} while (not end of XML file);
-----------------------------------------------------------------------

I understand that xmlSAXParseMemory needs a well-formed XML in-memory
buffer and not just a chunk.

For example, consider the XML

-----------------------------------------------------------------------
<foo>
   <bar>This is a really long string: 123456789012345678901234567890
   </bar>
</foo>
-----------------------------------------------------------------------

The chunks could be

-----------------------------------------------------------------------
Chunk 1:
<foo>
   <bar>This is a really long string: 1234567890

Chunk 2:
12345678901234567890</bar>
</foo>
-----------------------------------------------------------------------

Could you please tell me if streaming XPath is supported? If so, could
you please provide me a code sample that would parse a XML chunk and
evaluate XPaths on it.

Thanks much for your help.

Regards,
Ravi.





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