解析XML文件后生成JSON格式,供WEB前端调用

说明:此办法只适用于固定格式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树后,需要讲其层次体现出来,不能是平铺内容。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值