简体   繁体   English

在C语言中使用libxml2读取xml

[英]Reading xml with libxml2 in C

I have the following XML File: 我有以下XML文件:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<category>
    <data>
        <ca0>100</ca0>
        <ca1>Spielfilm</ca1>
        <ca2>Spielfilm</ca2>
    </data>
    <data>
        <ca0>200</ca0>
        <ca1>Serie</ca1>
        <ca2>Serie</ca2>
    </data>
</category>

I am developing a C program, which should read this XML file and transfer it into a MySQL database. 我正在开发一个C程序,该程序应读取此XML文件并将其传输到MySQL数据库中。 I have looked at several examples but I didn't succeed. 我看了几个例子,但没有成功。

To read the XML I use: 要读取XML,请使用:

reader = xmlReaderForFile("/tmp/category.xml", NULL, 0);
xmlTextReaderRead(reader);
name = xmlTextReaderName(reader);
if (!xmlStrcmp(name, (const xmlChar *) "category"))
{
    xmlTextReaderRead(reader);
    name = xmlTextReaderName(reader);
    if(!xmlStrcmp(name, (const xmlChar*) "data")) {
        xmlTextReaderRead(reader);
        name = xmlTextReaderName(reader);
    }
}

I expected that name contained "ca0", but it is empty. 我希望该name包含“ ca0”,但它为空。 Why? 为什么?

You should check the node type is XNT_Element , XNT_EndElement , or XNT_Text . 您应该检查节点类型为XNT_ElementXNT_EndElementXNT_Text It could be the XNT_Whitespace type after any of the element which followed by an indent. 它可以是XNT_Whitespace类型,后跟任何缩进元素。

ps: I don't think you can actually get the <data> element as the sequence you pasted above. ps:我认为您实际上并不能像上面粘贴的那样获得<data>元素。 You probably get the first XNT_Whitespace right after <category> . 您可能在<category>之后获得第一个XNT_Whitespace

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

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