MiniXML:用 C 解析 xml

发布于 2024-11-16 13:31:41 字数 2070 浏览 1 评论 0原文

我正在使用 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);

                }
             }
        }

I am parsing an xml file in C using Minixml. I want to get the values "check-time", "check-key" etc from this small eg. file:

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

Here is my code. I dont know how to extract the data withing the tag. Please help. Why is subnode->next not showing the next instance value?

   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);

                }
             }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

So尛奶瓶 2024-11-23 13:31:41

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

Now it has don't use subnode, you can try use node->child->value.xxx. Good luck to you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文