简体   繁体   中英

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM