c - 迷你XML : parsing xml in C

标签 c xml xml-parsing mini-xml

我正在使用 Minixml 在 C 中解析 xml 文件。我想从这个小例子中获取“check-time”、“check-key”等值。文件:

<?xml version="1.0"?>
<!--Test-->
<myfile> 
    <command type="start"> 
       <instance>check-time</instance>
       <instance>check-key</instance>
       <instance>check-position</instance>
        <action type="press button">
       <blue>1</blue>
    </action> 
    </command>

</myfile>

这是我的代码。我不知道如何使用标签提取数据。请帮忙。为什么 subnode->next 不显示下一个实例值?

   fp = fopen( "trial.xml", "r" );
        if(fp == NULL){
            perror("file missing");
        }

        mxml_node_t *tree, *Asset_elem;
        tree =  mxmlLoadFile(NULL, fp,MXML_TEXT_CALLBACK);

        fclose(fp);

        Asset_elem = mxmlWalkNext(tree, tree, MXML_DESCEND_FIRST);

        if(tree != NULL){
        mxml_node_t *node, *subnode, *subsubnode;

            for (node = mxmlFindElement(Asset_elem, tree,
                                        "command",
                                        "type", "start",
                                        MXML_DESCEND);
                 node != NULL;
                 node = mxmlFindElement(node, Asset_elem,
                         "command",
                         "type", "start",
                                        MXML_DESCEND))
        {

        printf("Inside for loop\n");
            printf("node name= %s\n", node->value.element.name);

            if(node){
                subnode = mxmlFindElement(node, tree, "instance", NULL, NULL, MXML_DESCEND);
                if(subnode != NULL){
                    printf("subnode name= %s\n", subnode->value.element.name);
                    subsubnode = subnode->child;
                    printf("subsubnode name= %s\n", subsubnode->value.text.string);
                    subsubnode = subnode->next;
                    printf("subsubnode name= %s\n", subsubnode->value.text.string);

                }
             }
        }

最佳答案

现在已经不使用子节点了,可以尝试使用node->child->value.xxx。祝你好运。

关于c - 迷你XML : parsing xml in C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6423585/

相关文章:

java - Xpath 没有给出正确的结果

xml - "collapse all"在firefox或chrome中一键所有节点

c - 无限提示循环问题

c - 为什么指针算术可用于非连续的2d数组?

c - 在 c 中使用 '"#include"和此链接错误?

JavaScript 匹配和替换;什么是最好的方法?

c# - Xml 节点的 Xml 后代

c++ - C 和 C++ 静态初始化之间的区别

java - 如何将两个按钮并排放置在按钮底部的图像上?

r - 通过 R 解析 XML 总是返回 XML 声明错误