我只是解析了 xml 并将其中的值插入到 char 数组中。但是在打印它时,我得到了一些不同的输出,如下所示。
ȷ
�,
��
但实际上应该是
Alu Tomato
Baigan Bharta
Chicken 65
源代码:
#include<stdio.h>
#include<string.h>
#include<libxml/parser.h>
#include<libxml/tree.h>
static void print_element_names(xmlNode * a_node);
char node_name[1024] = "";
char *menu_list[200];
int no_of_item = 0;
char tempstr[1024];
int flag_node = 0;
int main ()
{
memset (menu_list, -1, sizeof(menu_list));
parsexml();
printMenuList();
return 0;
}
int parsexml()
{
xmlDoc *doc = NULL;
xmlNode *root_element = NULL;
const char *Filename = "/mnt/jffs2/temp.xml";
doc = xmlReadFile(Filename, NULL, 0);
if (doc == NULL)
{
printf("error: could not parse file %s\n", Filename);
}
else
{
root_element = xmlDocGetRootElement(doc);
print_element_names(root_element);
xmlFreeDoc(doc);
}
xmlCleanupParser();
return 0;
}
static void print_element_names(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)
{
sprintf(node_name, "%s", cur_node->name);
if(strcmp(cur_node->name,"itemName") == 0)
flag_node = 1;
}
if(cur_node->content!=NULL)
{
if(flag_node == 1)
{
printf("Items\t%s\n", cur_node->content);
sprintf(menu_list[no_of_item], "%s", cur_node->content);
//menu_list[no_of_item] = cur_node->content;
flag_node = 0;
no_of_item++;
}
}
print_element_names(cur_node->children);
}
}
int printMenuList()
{
int i;
for (i=0; i<no_of_item; i++)
{
printf("%s\n", menu_list[i]);
}
return 0;
}
还有我的xml
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<userloginMethodResponse xmlns="http://wsdlclass.wsdlcreat.sims.triesten.com">
<userloginMethodReturn>
<itemCode>ALT</itemCode>
<itemName>Alu Tomato</itemName>
<itemPrice>0.0</itemPrice>
<loginStatusId>0</loginStatusId>
<loginid xsi:nil="true"/>
<messId>1</messId>
<password xsi:nil="true"/>
<schoolId>1</schoolId>
<userId>60000100</userId>
</userloginMethodReturn>
<userloginMethodReturn>
<itemCode>BAI</itemCode>
<itemName>Baigan Bharta</itemName>
<itemPrice>0.0</itemPrice>
<loginStatusId>0</loginStatusId>
<loginid xsi:nil="true"/>
<messId xsi:nil="true"/>
<password xsi:nil="true"/>
<schoolId>0</schoolId>
<userId xsi:nil="true"/>
</userloginMethodReturn>
<userloginMethodReturn>
<itemCode>CHIKK</itemCode>
<itemName>Chicken 65</itemName>
<itemPrice>20.0</itemPrice>
<loginStatusId>0</loginStatusId>
<loginid xsi:nil="true"/>
<messId xsi:nil="true"/>
<password xsi:nil="true"/>
<schoolId>0</schoolId>
<userId xsi:nil="true"/>
</userloginMethodReturn>
</userloginMethodResponse>
</soapenv:Body>
</soapenv:Envelope>