繁体   English   中英

libxml2获取节点的XML文本的偏移量

[英]libxml2 get offset into XML text of node

我需要知道xml字符串中的哪个偏移量,在使用xmlReadMemory获取dom之后,可以在dom中的某个位置找到特定的任意节点。 问题是我无法弄清楚从哪里获取xmlParserCtxtPtr作为第一个参数传递给xmlParserFindNodeInfo,因为我的整个解析过程不产生这样的上下文; 只有一个xmlDoc。

下面的代码对我有用(libxml2文档几乎没有什么需要,不得不下载源代码并深入挖掘lib,直到我理解到足以破解它为止)。 关键是:

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

以下是一些代码来说明:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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