c - 使用 C/C++ 的 LibXml2

标签 c linux libxml2

这是使用 libxml2-2.7.8 完成的,我在其中遇到错误。 我试过这个:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>

int main(int argc, char **argv) {

    char         *docname;
    xmlDocPtr    doc;
    xmlNodePtr   cur;
    xmlChar      *uri;

    if (argc <= 1) {
        printf("Usage: %s docname\n", argv[0]);
        return(0);
    }

    docname = argv[1];

    doc = xmlParseFile(docname);
    cur = xmlDocGetRootElement(doc);

    cur = cur->xmlChildrenNode;
    while (cur != NULL) {
        if ((!xmlStrcmp(cur->name, (const xmlChar *)"reference"))) {
            uri = xmlGetProp(cur, "uri");
            printf("uri: %s\n", uri);
            xmlFree(uri);
        }
        cur = cur->next;
    }
    xmlFreeDoc(doc);
    return (1);
}

和 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<story>
  <storyinfo>
    <author>John Fleck</author>
    <datewritten>June 2, 2002</datewritten>
    <keyword>تخیلی</keyword>
  </storyinfo>
  <body>
    <headline>This is the headline</headline>
    <para>This is the body text.</para>
  </body>
  <reference uri="www.w3.org/TR/xslt20/"/>
</story>

我得到的错误是: 错误 C2664:“xmlChar *xmlGetProp(xmlNodePtr,const xmlChar *)”:无法将参数 2 从“const char [4]”转换为“const xmlChar *”

请帮忙

最佳答案

您缺少对 const xmlChar* 的转换:

uri = xmlGetProp(cur, (const xmlChar*)"uri");

关于c - 使用 C/C++ 的 LibXml2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36301126/

相关文章:

c - Ansi C - 函数期望指向数组的指针

regex - 计算正则表达式的唯一出现次数

iphone - REST -> XML -> 核心数据 -> UITableView 的好策略吗?

ruby - Nokogiri LibXML 版本警告

plone - 用户警告 : libxml2 not available

c - 为什么线程2要等待线程1结束?

c - 排空Intel Core 2 Duo 的指令流水线?

c - 遍历列表 : error lvalue required as left operand of assignment

ruby - 如何使用 crontab 执行脚本

linux - 从 XML 标记解析值