c - libxml2:不单独报告 ' or "等字符

标签 c libxml2

我是 libxml 的新手,到目前为止一切都很好,但我注意到一件让我烦恼的事情: 当 libxml 报告字符时,即调用处理程序的字符函数时,“特殊”字符如 ' 或 "或单独报告。 示例:

"It's a nice day today. Don't you agree?"
report:"
report: It
report: '
report: s a nice day today. Don
report: '
report: you aggree?
report: "

有什么方法可以改变这种行为,以便将其报告为完整的字符串吗? 不要误会我的意思,使用 strcat 将原始字符串放在一起不是问题,但这是额外的工作;)

我搜索了标题和网络,没有找到解决方案。预先感谢您。

编辑:因为上面的处理程序描述需要更多解释。 报告字符是指当调用处理程序的 (htmlSAXHandler) handler.characters 回调函数时,我指定了该函数:

void _characters(void *context, const xmlChar *ch, int len) {
    printf("report: %s\n", chars);
}

最佳答案

如果您的文档不会太大而无法将其全部保存在内存中,您可能需要查看 DOM 解析而不是注册 SAX 回调。

#include <stdio.h>
#include <libxml/HTMLparser.h>
#include <libxml/tree.h>

int main()
{
  htmlDocPtr doc;
  xmlNodePtr root, node;
  char *output;
  char *rawhtml = "<html><body>\"It's a nice day today.  Don't you agree?\"</body></html>";
  doc = htmlReadDoc(rawhtml, NULL, NULL, XML_PARSE_NOBLANKS);
  root = xmlDocGetRootElement(doc);
  node = root->children;
  output = xmlNodeGetContent(node);
  printf("output=[%s]\n", output);
  if(output)
    xmlFree(output);
  if(doc)
    xmlFreeDoc(doc);
}

产生

output=["It's a nice day today.  Don't you agree?"]

关于c - libxml2:不单独报告 ' or "等字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13165554/

相关文章:

c - C语言如何重写一个函数(系统调用)?

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

xml - 解析 XML Libxmljs (Node.js)

c - 字符数组和整型数组的区别

c++ - 如何使用 coc-clangd/coc-nvim 手动(无需键绑定(bind))搜索源代码中的符号?

c - xmlNodeGetContent 引入换行符

android - 从源代码为 android 构建 libxml2 作为静态库

ruby - Libxml2 缺少 mac os x 10.10

python - 分发使用自定义 c 模块扩展的 python 程序的最佳方法是什么?

[Array of Struct in [Array of Struct in [Array of Struct ]]] 中的结构体动态数组