简体   繁体   English

解析XML文件并写入链接列表

[英]Parse XML file and write in linked list

I am trying to parse XML data and save it in a linked list which is supposed to be loaded in the main memory on boot. 我正在尝试解析XML数据并将其保存在链表中,该链表应该在引导时加载到主内存中。 When the XML data is parsed, I want a set of XML entity/elements to be written in one Node of the linked list and the next set of data in the next node. 解析XML数据时,我希望将一组XML实体/元素写入链表的一个节点中,并将下一组数据写入下一个节点中。 While trying to achieve this, I am able to parse the data that I need but the assigning of the data to the linked list isn't happening as the logic I am trying to apply seems incorrect. 在尝试实现此目的时,我能够解析所需的数据,但是由于将要应用的逻辑似乎不正确,因此没有将数据分配给链表。 I would like some help on the same. 我希望在同一方面有所帮助。

File.xml File.xml

<policyList>
        <policySecurity>
                <policyName>AutoAdd</policyName>
                <deviceName>PA-722</deviceName>
                <status>ACTIVE</status>
                <srcZone>any</srcZone>
                <dstZone>any</dstZone>
                <srcAddr>any</srcAddr>
                <dstAddr>5.5.5.4</dstAddr>
                <srcUser>any</srcUser>
                <application>any</application>
                <service>any</service>
                <urlCategory>any</urlCategory>
                <action>deny</action>
        </policySecurity>
        <policySecurity>
                <policyName>Test-1</policyName>
                <deviceName>PA-710</deviceName>
                <status>ACTIVE</status>
                <srcZone>any</srcZone>
                <dstZone>any</dstZone>
                <srcAddr>192.168.1.23</srcAddr>
                <dstAddr>8.8.8.8</dstAddr>
                <srcUser>vivek</srcUser>
                <application>any</application>
                <service>any</service>
                <urlCategory>any</urlCategory>
                <action>deny</action>
        </policySecurity>
</policyList>

File.c File.c

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


struct node {
        char *polname;
        char *devname;
        char *status;
        char *srczone;
        char *dstzone;
        char *srcaddr;
        char *dstaddr;
        char *srcuser;
        char *app;
        char *service;
        char *urlcategory;
        char *action;
        char *vulnerability;


        struct node *next;
            };
        struct node *head;

void insert(char *a, char *b, char *c, char *d, char *e, char *f, char*g, char *h, char *i, char *j, char *k, char *l, char *m){
        struct node *temp;
        temp = (struct node*)malloc(sizeof(struct node));
        temp->polname = a;
        temp->devname = b;
        temp->status = c;
        temp->srczone = d;
        temp->dstzone = e;
        temp->srcaddr = f;
        temp->dstaddr = g;
        temp->srcuser = h;
        temp->app = i;
        temp->service = j;
        temp->urlcategory = k;
        temp->action = l;
        temp->next = head;
        head= temp;
}
/* This is where the error occurs */
void traverse_dom_trees(xmlNode * a_node)
{
    xmlNode *cur_node = NULL;

    for (cur_node = a_node; cur_node; cur_node = cur_node->next)
    {
        if (cur_node->type == XML_ELEMENT_NODE)
        {
              /*  char *a,*b,*c,*d,*e,*f,*g,*h,*i,*j,*k,*l,*m; */
                 if(strcmp((const char *)cur_node->name,(const char *)"policyName") == 0){
                    printf("entered second if");
                    cur_node = cur_node->next;
                    printf("cur_node->next");
                    a = (char *)cur_node->content;
                    printf("value of a is %d", *a);
                    /*strcpy(*a,(const char *)cur_node->content);
                    printf("strcpy");
                    break;*/
               /*     }
             else if(strcmp((const char *)cur_node->name,(const char *)"deviceName") == 0)  {
                     cur_node = cur_node->next;
                    strcpy(*b,(const char *)cur_node->content);
                    break;
                    }
            else if(strcmp((const char *)cur_node->name,(const char *)"status") == 0){
                     cur_node = cur_node->next;
                    strcpy(*c,(const char *)cur_node->content);
                    break;
                    }
            else if(strcmp((const char *)cur_node->name,(const char *)"srcZone") == 0){
                     cur_node = cur_node->next;
                    strcpy(*d,(const char *)cur_node->content);
                    break;
                    }
            else if(strcmp((const char *)cur_node->name,(const char *)"dstZone") == 0){
                     cur_node = cur_node->next;
                    strcpy(*e,(const char *)cur_node->content);
                    break;
                    }
            else if(strcmp((const

 char *)cur_node->name,(const char *)"srcAddr") == 0){
                         cur_node = cur_node->next;
                        strcpy(*f,(const char *)cur_node->content);
                        break;
                        }
                else if(strcmp((const char *)cur_node->name,(const char *)"dstAddr") == 0){
                         cur_node = cur_node->next;
                        strcpy(*g,(const char *)cur_node->content);
                        break;
   }
                else if(strcmp((const char *)cur_node->name,(const char *)"srcUser") == 0){
                         cur_node = cur_node->next;
                         strcpy(*h,(const char *)cur_node->content);
                        break;
                        }
                else if(strcmp((const char *)cur_node->name,(const char *)"application") == 0){
                         cur_node = cur_node->next;
                         strcpy(*i,(const char *)cur_node->content);
                        break;
                        }
                else if(strcmp((const char *)cur_node->name,(const char *)"service") == 0){
                         cur_node = cur_node->next;
                         strcpy(*j,(const char *)cur_node->content);
                        break;
                        }
                else if(strcmp((const char *)cur_node->name,(const char *)"urlCategory") == 0){
                         cur_node = cur_node->next;
                         strcpy(*k,(const char *)cur_node->content);
                        break;
                        }
                else if(strcmp((const char *)cur_node->name,(const char *)"action") == 0){
                         cur_node = cur_node->next;
                         strcpy(*l,(const char *)cur_node->content);
                        break;
                        }
                else if(strcmp((const char *)cur_node->name,(const char *)"vulnerability") == 0){
                         cur_node = cur_node->next;
                         strcpy(*m,(const char *)cur_node->content);
                        break;
                        }*/

          /*insert( a, b, c, d, e, f, g, h, i, j, k, l, m);*/
    }

        traverse_dom_trees(cur_node->children);
    }
}

int main(int argc, char **argv)
{
    xmlDocPtr doc;
    xmlNode *roo_element = NULL;

    if (argc != 2)
    {
        printf("\nInvalid argument\n");
        return(1);
    }

    doc = xmlReadFile(argv[1], NULL, XML_PARSE_NOBLANKS | XML_PARSE_NOERROR | XML_PARSE_NOWARNING | XML_PARSE_NONET);
    if (doc == NULL)
    {
        fprintf(stderr, "Document not parsed successfully.\n");
        return 0;
    }

    roo_element = xmlDocGetRootElement(doc);

    if (roo_element == NULL)
 {
        fprintf(stderr, "empty document\n");
        xmlFreeDoc(doc);
        return 0;
    }

    printf("Root Node is %s\n", roo_element->name);
    traverse_dom_trees(roo_element);

    xmlFreeDoc(doc);       // free document
    xmlCleanupParser();    // Free globals
    return 0;
}

As you can see, I am trying a if/else if statement to assign the respective values to the node in linked list and it prints the following: 如您所见,我正在尝试一个if/else if语句,以将各自的值分配给链表中的节点,并输出以下内容:

Root Node is policyList
Segmentation fault

Am I applying a wrong logic to implement what I have to do? 我是否采用错误的逻辑来实现自己必须做的事情? Should I take a different approach for the same? 我是否应该采取其他相同的方法?

Please bear with the code, as it does seem long at various places(and I am still in my learning phase). 请耐心看一下代码,因为它在各个地方似乎都很长(而且我仍处于学习阶段)。 Any and all help/suggestions will be appreciated. 任何和所有帮助/建议将不胜感激。 Thanks in advance :) 提前致谢 :)

Edit 1: I switched to if/else rather than switch/case . 编辑1:我切换到if/else而不是switch/case Moreover, most of the warnings that were being shown before are mainly because of wrong assignment. 此外,以前显示的大多数警告主要是由于分配错误。 I have commented most of the bugsy code to know what part of my code is working and what needs to be corrected. 我已经注释了大多数错误代码,以了解我的代码的哪些部分在起作用,哪些部分需要更正。 While debugging, I realized that it's the assignment part which is not working, because I think I'm not using the right way to save the data present in the pointer memory to the corresponding variable/pointer variable. 在调试时,我意识到这是赋值部分不起作用,因为我认为我没有使用正确的方法将指针内存中存在的数据保存到相应的变量/指针变量中。 Since it is printing the root node for me and also the printf till second if is called, so I assume that the rest of the code is correct, just the pointer assignment isn't working as it is supposed to. 由于它正在为我打印根节点,并且还会打印到第二个if的printf ,因此我假设其余代码是正确的,只是指针分配没有按预期工作。

The content is not contained in the XML_ELEMENT_NODE , but in a child node of type XML_TEXT_NODE (see Retrieving Element Content ), so we'd have to change content不包含在XML_ELEMENT_NODE ,但在类型的子节点XML_TEXT_NODE (见检索元素含量 ),因此我们必须改变

                    a = (char *)cur_node->content;

to

                    a = (char *)cur_node->xmlChildrenNode->content;

or 要么

                    a = (char *)xmlNodeListGetString(cur_node->doc, cur_node->xmlChildrenNode, 0);

The latter returns a copy of the string, which can be freed with xmlFree() . 后者返回字符串的副本,可以使用xmlFree()将其释放。

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

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