Search code examples
cxmllibxml2

libxml2 get offset into XML text of node


I need to know at which offset into an xml string a specific arbitrary node somewhere in dom can be found after xmlReadMemory was used to get dom. The problem is I can't figure out where to get the xmlParserCtxtPtr from to pass as first argument to xmlParserFindNodeInfo because my entire process of parsing yields no such context; only a xmlDoc.


Solution

  • The following code worked for me (libxml2 documentation leaves little to desire, had to download source code and dig in the lib until I understood enough to hack this together). The key is:

    xmlSetFeature(ctxt, "gather line info", (void *)&v);
    

    Here is some code to illustrate:

    const char *xml = ...
    xmlParserCtxt *ctxt = NULL;
    xmlDoc *doc = NULL;
    if (!(ctxt = xmlCreateDocParserCtxt((const unsigned char *)xml)))
        return -1;
    int v = 1;
    xmlSetFeature(ctxt, "gather line info", (void *)&v);
    if (xmlParseDocument(ctxt) == -1)
    {
        xmlFreeParserCtxt(ctxt);
        return -1;
    }
    else
    {
        if ((ctxt->wellFormed) || ctxt->recovery)
            doc = ctxt->myDoc;
        else
        {
            xmlFreeParserCtxt(ctxt);
            return -1;
        }
    }
    
    // use doc to get a node and then xmlParserFindNodeInfo(ctxt, node)
    …
    
    xmlFreeParserCtxt(ctxt);