libxml2 :: htmlDocDumpMemory()自动关闭HTML中未关闭的标签 [英] libxml2::htmlDocDumpMemory() automatically close unclosed tags in HTML
问题描述
我在我的VS2010项目中使用libxml2从HTML生成树,找到一些节点,修改它并将树转储回HTML。
主要逻辑是:
// 创建解析器
htmlParserCtxtPtr parser = htmlCreatePushParserCtxt( NULL,NULL,NULL, 0 ,NULL,XML_CHAR_ENCODING_UTF8);
// 设置解析器选项
htmlCtxtUseOptions(解析器,HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING | HTML_PARSE_NONET );
// 从pData解析HTML
htmlParseChunk(parser,pData,dataLen, 0 );
// 获取生成树的根节点
xmlNode * node = xmlDocGetRootElement( parser-> myDoc);
// 在树中进行一些更改,例如更改名称为title的节点的内容
// ...
// 返回HTML,结果位于'newHtml'
htmlDocDumpMemory (parser-> myDoc,& newHtml,& len);
当我使用来自 http://www.youtube.com/watch?v=S77UrnEGs_g [ ^ ]作为输入数据我得到一个多余的输出。
我已在 http://validator.w3.org/ [ ^ ]并收到错误:
第562行,第31列:未关闭的元素div。
< div class = content >
我的问题是:我可以配置libxml2以便它不会自动关闭未关闭的标签吗?
我发现了一些非验证解析器: http://htmlcxx.sourceforge.net/ [ ^ ],它为我解决了这个问题。
I am using libxml2 in my VS2010 project to generate tree from HTML, find some nodes, modify it and dump tree back to HTML.
The main logic is:
// create parser
htmlParserCtxtPtr parser = htmlCreatePushParserCtxt(NULL, NULL, NULL, 0, NULL, XML_CHAR_ENCODING_UTF8);
// set parser options
htmlCtxtUseOptions(parser, HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING | HTML_PARSE_NONET);
// parse HTML from pData
htmlParseChunk(parser, pData, dataLen, 0);
// get root node for generated tree
xmlNode* node = xmlDocGetRootElement(parser->myDoc);
// make some changes in tree, e.g. change content for node with name 'title'
// ...
// return back to HTML, result located in 'newHtml'
htmlDocDumpMemory(parser->myDoc, &newHtml, &len);
When I use HTML from http://www.youtube.com/watch?v=S77UrnEGs_g[^] as input data I get one excess in output.
I have checked above URL on http://validator.w3.org/[^] and get error:
Line 562, Column 31: Unclosed element div.
<div class="content">
My question is: could I configure libxml2 so it would not automatically close unclosed tags?
I found some non-validating parser: http://htmlcxx.sourceforge.net/[^], it solve the problem for me.
这篇关于libxml2 :: htmlDocDumpMemory()自动关闭HTML中未关闭的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!