简体   繁体   中英

Reading xml with libxml2 in C

I have the following XML File:

<?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. I have looked at several examples but I didn't succeed.

To read the XML I use:

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. Why?

You should check the node type is XNT_Element , XNT_EndElement , or XNT_Text . It could be the XNT_Whitespace type after any of the element which followed by an indent.

ps: I don't think you can actually get the <data> element as the sequence you pasted above. You probably get the first XNT_Whitespace right after <category> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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