<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Soul Of Free Loop &#187; JSON</title>
	<atom:link href="https://zohead.com/archives/tag/json/feed" rel="self" type="application/rss+xml" />
	<link>https://zohead.com</link>
	<description>Uranus Zhou&#039;s Blog</description>
	<lastBuildDate>Sat, 19 Jul 2025 15:42:46 +0000</lastBuildDate>
	<language>zh-CN</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.8</generator>
	<item>
		<title>使用libexslt库将XML转换为JSON</title>
		<link>https://zohead.com/archives/libexslt-xml-json/</link>
		<comments>https://zohead.com/archives/libexslt-xml-json/#comments</comments>
		<pubDate>Tue, 25 Mar 2014 12:48:25 +0000</pubDate>
		<dc:creator><![CDATA[Uranus Zhou]]></dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[技术]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[libexslt]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[XSLT]]></category>

		<guid isPermaLink="false">http://zohead.com/?p=685</guid>
		<description><![CDATA[本文同步自（最佳显示效果请点击）：https://zohead.com/archives/libexslt-xml-json/ 最近在一个 C 程序中碰到需要将 XML 数据转换为 JSON 数据的问题，多番查找几种方法，觉得此程序刚好用到了 Linux 下的 libexslt XSLT 库，因此想直接通过 XSLT 将 XML 转为 JSON 数据。 网上已经有了现成的 XML 转 JSON 的 XSLT 程序： http://code.google.com/p/xml2json-xslt/ 下载下来的 xml2json.xslt 程序可以很方便的将标准的 XML 文件转换为 JavaScr [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>本文同步自（最佳显示效果请点击）：<a href="https://zohead.com/archives/libexslt-xml-json/" target="_blank">https://zohead.com/archives/libexslt-xml-json/</a></p>
<p>最近在一个 C 程序中碰到需要将 XML 数据转换为 JSON 数据的问题，多番查找几种方法，觉得此程序刚好用到了 Linux 下的 libexslt XSLT 库，因此想直接通过 XSLT 将 XML 转为 JSON 数据。</p>
<p>网上已经有了现成的 XML 转 JSON 的 XSLT 程序：</p>
<p><a href="http://code.google.com/p/xml2json-xslt/" target="_blank">http://code.google.com/p/xml2json-xslt/</a></p>
<p>下载下来的 xml2json.xslt 程序可以很方便的将标准的 XML 文件转换为 JavaScript 样式的 JSON 数据，但经过试用之后发现此程序还是有一些不足，例如：不支持转换 XML 属性，对数组支持不好等问题。</p>
<p>我对 xml2json.xslt 做了一些改进，包括将 XML 中的属性名转换为 JSON 子节点（节点名称为 @attr 这种特殊的样式），并且为需要明确转换为 JSON 数组的节点（即使该节点下面只包含一个同类的子节点）增加 ifArray 属性，如果 ifArray 属性值为 yes，则在转换为 JSON 强制生成数组。</p>
<p>这个是我修改过的 xml2json.xslt 文件：</p>
<p><a href="https://gist.github.com/zohead/9688858" target="_blank">https://gist.github.com/zohead/9688858</a></p>
<p>Linux 系统可以方便的使用 xsltproc 命令将 XML 转换为 JSON，运行下面的命令就会直接将转换出来的 JSON 数据打印到标准输出中：</p>
<p><strong>xsltproc xml2json.xslt test.xml</strong></p>
<p>下面主要介绍如何在 Linux 中编程使用 libexslt 库将 XML 转换为 JSON 数据，有关 libexslt 库的介绍请参考这里：<a href="http://xmlsoft.org/libxslt/EXSLT/" target="_blank">http://xmlsoft.org/libxslt/EXSLT/</a>，可惜 libexslt 并没有详细的介绍文档，连网上的例子都很少。</p>
<p>这个是我的 xslt.c 实例程序：</p>
<pre class="brush: cpp; title: xslt.c; notranslate">
#include &lt;string.h&gt;
#include &lt;libxml/xmlmemory.h&gt;
#include &lt;libxml/debugXML.h&gt;
#include &lt;libxml/HTMLtree.h&gt;
#include &lt;libxml/xmlIO.h&gt;
#include &lt;libxml/xinclude.h&gt;
#include &lt;libxml/catalog.h&gt;
#include &lt;libxml/parser.h&gt;
#include &lt;libxml/parserInternals.h&gt;
#include &lt;libxml/uri.h&gt;
#include &lt;libxslt/xslt.h&gt;
#include &lt;libxslt/xsltInternals.h&gt;
#include &lt;libxslt/transform.h&gt;
#include &lt;libxslt/xsltutils.h&gt;
#include &lt;libexslt/exslt.h&gt;
#include &lt;libexslt/exsltconfig.h&gt;
#include &lt;libexslt/exsltexports.h&gt;

int xslProc(const char *xslfile, const char *xmlfile, const char *outfile, const char *xmlstr, char *lpbuf, size_t bufsize)
{
	xsltStylesheetPtr cur = NULL;
	xmlDocPtr doc = NULL, res = NULL;
	int iRet = -1;

	if (xslfile == NULL || !xslfile[0] || ((xmlfile == NULL || !xmlfile[0]) &amp;&amp; (xmlstr == NULL || !xmlstr[0])) || ((outfile == NULL || !outfile[0]) &amp;&amp; lpbuf == NULL))
		return -1;

	exsltRegisterAll();
	xmlInitParser();
	xmlSubstituteEntitiesDefault(1);
	xmlLoadExtDtdDefaultValue = 1;

	cur = xsltParseStylesheetFile((const xmlChar *)xslfile);
	if (cur) {
		if (xmlfile)
			doc = xmlParseFile(xmlfile);
		else
			doc = xmlParseMemory(xmlstr, strlen(xmlstr));
		if (doc) {
			res = xsltApplyStylesheet(cur, doc, NULL);
			if (res) {
				if (outfile)
					iRet = xsltSaveResultToFilename(outfile, res, cur, 0);
				else {
					char *out = NULL;
					int outlen = 0;
					iRet = xsltSaveResultToString((xmlChar **)&amp;out, &amp;outlen, res, cur);
					if (out) {
						if (iRet == 0) strncpy(lpbuf, out, bufsize);
						free(out);
					}
				}
				xmlFreeDoc(res);
			} else
				iRet = 1;
			xmlFreeDoc(doc);
		} else
			iRet = 2;
		xsltFreeStylesheet(cur);
	} else
		iRet = 3;

	xsltCleanupGlobals();
	xmlCleanupParser();

	return iRet;
}
</pre>
<p>程序传入 XSLT 文件名，支持 XML 字符串、XML 文件以及输出到文件及保存到字符串的方式。</p>
<p>具体实现方法还是比较简单的，使用 Linux 的 libexslt 库解析 XSLT 文件（xsltParseStylesheetFile），libxml 库来解析 XML 文件（xmlParseFile 和 xmlParseMemory 函数），使用 libexslt 库应用 XSLT stylesheet（xsltApplyStylesheet），保存结果数据使用 xsltSaveResultToFilename 和 xsltSaveResultToString 函数，程序中需要特别注意的就是 libxml 和 libexslt 库中众多的初始化和释放操作。</p>
]]></content:encoded>
			<wfw:commentRss>https://zohead.com/archives/libexslt-xml-json/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
