说明:此办法只适用于固定格式XML文件与WEB前端进行对接。
以下是待解析的XML文件代码test.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<test>
<storyinfo>
<author>OCEAN</author>
<datewritten>1-13-2015</datewritten>
<keyword>TEST FOR JSON </keyword>
<DeviceNumber>20150113</DeviceNumber>
</storyinfo>
<storyinfo>
<author>OCEAN</author>
<datewritten>1-13-2015</datewritten>
<keyword>TEST FOR JSON </keyword>
<DeviceNumber>20150113</DeviceNumber>
</storyinfo>
</test>
以下是解析XML的C语言代码:
/*************************************************************************************
* > File Name: get_xml_info.c
* > INSTRUCTION: get the information from the .xml files
* > Author: ocean.y.yang
* > Mail: ocean.y.ocean@microcore.com
* > Created Time: 2015-1-12 10:41:15
*************************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include "json.h"
#include "get_xml_info.h"
#include "cgic.h"
xmlNodePtr currentNode;
/*
* Function : parse the xml file
* Param : @docname :the file
* Return : a pointer which point the xmlNodePtr
*/
xmlNodePtr parseDoc(char *docname)
{
xmlDocPtr doc;
xmlNodePtr cur;
xmlKeepBlanksDefault(0);
doc = xmlParseFile(docname);
if (doc == NULL )
{
fprintf(stderr,"Document not parsed successfully. \n");
return;
}
cur = xmlDocGetRootElement(doc);
if (cur == NULL)
{
fprintf(stderr,"empty document\n");
xmlFreeDoc(doc);
return;
}
return cur;
}
/*
* Function : parse xml file to json.
* Param : @JsonSaveInfo:Save information into the Json
@doc :the file
@cur :current node
* Return : none
*/
void ParseXmltoJson(json_object *JsonSaveInfo,xmlDocPtr doc,xmlNodePtr cur)
{
xmlChar *key;
int ItemNum = 0;
json_object *jarray = json_object_new_array();
json_object *item = json_object_new_object();
if (cur != NULL)
{
cur = cur->xmlChildrenNode;
xmlNodePtr node = cur;
xmlNodePtr nodeTwo = cur;
while(cur != NULL)
{
cur = cur->xmlChildrenNode;
//有多少条目,就循环多少次
for(;ItemNum < 4;ItemNum++)
{
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
if(key != NULL)
{
json_object_object_add(item,cur->name,json_object_new_string(key));
xmlFree(key);
}
cur = cur->next;
}
json_object_array_add(jarray,item);
cur = node->next;
if((cur != NULL) && (strcmp("storyinfo", cur->name) != 0))
{
break;
}
node = cur;
}
json_object_object_add(JsonSaveInfo,nodeTwo->name,jarray);
}
else
{
fprintf(stderr, "ERROR: Null Node!");
}
}
int cgiMain(void)
{
char *docname = "test.xml";
char *GetJsonInfo;
json_object *JsonSaveInfo = json_object_new_object();
json_object *JsonSave = json_object_new_object();
currentNode = parseDoc(docname);
ParseXmltoJson(JsonSaveInfo,docname,currentNode);
json_object_object_add(JsonSave,currentNode->name,JsonSaveInfo);
GetJsonInfo = json_object_to_json_string(JsonSave);
printf("%s\n",GetJsonInfo);
cgiHeaderContentType("text/html");
fprintf(cgiOut, "<title>Captured</title>\n");
fprintf(cgiOut, "<h1>Captured</h1>\n");
fprintf(cgiOut, "Your form submission was captured for use in\n");
fprintf(cgiOut, "debugging CGI code.\n");
fprintf(cgiOut, "<H1>%s</H1>",GetJsonInfo);
return 0;
}
{ "test": { "storyinfo": [ { "author": "OCEAN", "datewritten": "1-13-2015", "keyword": "TEST FOR JSON ", "DeviceNumber": "20150113" }, { "author": "OCEAN", "datewritten": "1-13-2015", "keyword": "TEST FOR JSON ", "DeviceNumber": "20150113" } ] } }
Content-type: text/html
<title>Captured</title>
<h1>ok Captured</h1>
Your form submission was captured for use in
debugging CGI code.
<H1>{ "test": { "storyinfo": [ { "author": "OCEAN", "datewritten": "1-13-2015", "keyword": "TEST FOR JSON ", "DeviceNumber": "20150113" }, { "author": "OCEAN", "datewritten": "1-13-2015", "keyword": "TEST FOR JSON ", "DeviceNumber": "20150113" } ] } }</H1>
红色部分为解析出来的XML文件,可通过JSON检测。
开启BOA服务器,输入IP进行访问页面;http://192.168.1.233:801/cgi/get_xml_info.cgi
能够显示页面,说明解析正确,可供WEB前端开发人员调用。
由于水平有限,所以我的办法是一层一层的遍历,欢迎大侠前来指导,在遍历完XML树后,需要讲其层次体现出来,不能是平铺内容。