[英]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.