<?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; XML</title>
	<atom:link href="https://zohead.com/archives/tag/xml/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>
		<item>
		<title>修改XBMC LiveStreams Python插件以支持中文</title>
		<link>https://zohead.com/archives/xbmc-livestreams-cn-patch/</link>
		<comments>https://zohead.com/archives/xbmc-livestreams-cn-patch/#comments</comments>
		<pubDate>Mon, 03 Sep 2012 18:49:07 +0000</pubDate>
		<dc:creator><![CDATA[Uranus Zhou]]></dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[工具]]></category>
		<category><![CDATA[技术]]></category>
		<category><![CDATA[树莓派]]></category>
		<category><![CDATA[HTPC]]></category>
		<category><![CDATA[LiveStreams]]></category>
		<category><![CDATA[Openelec]]></category>
		<category><![CDATA[Raspbmc]]></category>
		<category><![CDATA[XBian]]></category>
		<category><![CDATA[XBMC]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[插件]]></category>

		<guid isPermaLink="false">http://zohead.com/?p=299</guid>
		<description><![CDATA[本文同步自（最佳显示效果请点击）：https://zohead.com/archives/xbmc-livestreams-cn-patch/ 最近在闲时捣鼓下 Raspberry Pi 微型电脑板上的开源 XBMC 应用准备看看 HTPC 媒体中心的效果，发现 Raspberry Pi 在安装了 XBian 系统之后可以比较好的实现 XBMC 的基本功能，虽然由于 Raspberry Pi 没有购买一些视频格式的软件解码授权而导致 WMV 或者 MMS 之类的格式无法播放，但对于常用的一些 H264 的高清视频已经足以应付，接上网线之后看高清在线视频点播和直播的效果都还可以。 有关 Rasp [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>本文同步自（最佳显示效果请点击）：<a href="https://zohead.com/archives/xbmc-livestreams-cn-patch/" target="_blank">https://zohead.com/archives/xbmc-livestreams-cn-patch/</a></p>
<p>最近在闲时捣鼓下 Raspberry Pi 微型电脑板上的开源 XBMC 应用准备看看 HTPC 媒体中心的效果，发现 Raspberry Pi 在安装了 XBian 系统之后可以比较好的实现 XBMC 的基本功能，虽然由于 Raspberry Pi 没有购买一些视频格式的软件解码授权而导致 WMV 或者 MMS 之类的格式无法播放，但对于常用的一些 H264 的高清视频已经足以应付，接上网线之后看高清在线视频点播和直播的效果都还可以。</p>
<p>有关 Raspberry Pi 请参考 [<a href="https://zohead.com/archives/raspberry-pi-start/" target="_blank">之前</a>] 的文章，有关 XBian 之一专门为 Raspberry Pi 优化的 XBMC 系统可以访问其官网：<a href="http://xbian.org/" target="_blank">http://xbian.org/</a>。</p>
<p>注：Raspberry Pi 上除了 XBian 之外，还有 <a href="http://www.raspbmc.com/" target="_blank">Raspbmc</a>、<a href="http://openelec.tv/" target="_blank">Openelec</a> 等其它合适的 XBMC 系统可供选择的。而且这些都是基于标准 XBMC 程序修改的，标准插件之类的基本可以通用。</p>
<p>前两天找到一个不错的在线电视直播的 XBMC 插件：LiveStreams，此插件可以由用户自己修改 XML 配置文件增加在线直播的地址，根据实际硬件配置不同，可支持 MMS、RTSP、RTMP 等各种不同的流媒体协议。有关 LiveStreams 的介绍和配置请参考这些链接（特别第二个链接中有详细的截图介绍）：</p>
<p><a href="http://forum.xbmc.org/showthread.php?tid=97116" target="_blank">http://forum.xbmc.org/showthread.php?tid=97116</a><br />
<a href="http://www.xbmchub.com/blog/2012/04/26/adding-custom-xml-files-to-the-live-streams-addon/" target="_blank">http://www.xbmchub.com/blog/2012/04/26/adding-custom-xml-files-to-the-live-streams-addon/</a></p>
<p>最新版本的 LiveStreams 插件可以到这里下载：<a href="http://code.google.com/p/divingmules-repo/" target="_blank">http://code.google.com/p/divingmules-repo/</a>。</p>
<p>在实际使用过程中发现由于 LiveStreams 由于是老外写的，不由自主的就碰到对中文的支持问题，如果添加的 XML 配置文件中节目名称或者节目目录名称包含中文，XBMC 系统中LiveStreams 插件将不能正常工作，直接会出现脚本错误。</p>
<p>简单看了下 LiveStreams 插件的代码，是用 Python 写的，凭着一些简单的 Python 基础，然后集合 Python 的 logging 模块来调试，终于发现 LiveStreams 插件对中文支持不佳的原因，作者在使用 BeautifulSoup（参考 [<a href="http://www.crummy.com/software/BeautifulSoup/" target="_blank">这里</a>]） 这一个非常知名的 HTML/XML 等解析的库时未考虑非英文环境下的问题，简单做了下修改之后，中文的直播节目和目录名称都可以正常显示了。</p>
<p>顺便再简单说明一下 LiveStreams XML 配置文件嵌套节目目录的方式，这是一个实例 XML 节目配置文件：</p>
<pre class="brush: xml; title: channels.xml; notranslate">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;yes&quot;?&gt;
&lt;channels&gt;
	&lt;channel&gt;
		&lt;name&gt;Channel 1&lt;/name&gt;
		&lt;thumbnail&gt;http://xxx/chn1.png&lt;/thumbnail&gt;
		&lt;items&gt;
			&lt;item&gt;
				&lt;title&gt;TV1&lt;/title&gt;
				&lt;link&gt;mms://xxx.xxx.xxx.xxx/cctv1&lt;/link&gt;
				&lt;thumbnail&gt;http://xxx/cctv1.jpg&lt;/thumbnail&gt;
			&lt;/item&gt;
			&lt;item&gt;
				&lt;title&gt;TV2&lt;/title&gt;
				&lt;link&gt;mms://xxx.xxx.xxx.xxx/cctv2&lt;/link&gt;
				&lt;thumbnail&gt;http://xxx/cctv2.jpg&lt;/thumbnail&gt;
			&lt;/item&gt;
		&lt;/items&gt;
	&lt;/channel&gt;

	&lt;channel&gt;
		&lt;name&gt;Channel 2&lt;/name&gt;
		&lt;thumbnail&gt;http://xxx/chn2.png&lt;/thumbnail&gt;
		&lt;subchannels&gt;
			&lt;subchannel&gt;
				&lt;name&gt;Sub 1&lt;/name&gt;
				&lt;thumbnail&gt;http://xxx/sub1.png&lt;/thumbnail&gt;
				&lt;subitems&gt;
					&lt;subitem&gt;
						&lt;title&gt;Sub TV1&lt;/title&gt;
						&lt;link&gt;mms://xxx.xxx.xxx.xxx/subtv1&lt;/link&gt;
						&lt;thumbnail&gt;http://xxx/subtv1.jpg&lt;/thumbnail&gt;
					&lt;/subitem&gt;
					&lt;subitem&gt;
						&lt;title&gt;Sub TV2&lt;/title&gt;
						&lt;link&gt;mms://xxx.xxx.xxx.xxx/subtv2&lt;/link&gt;
						&lt;thumbnail&gt;http://xxx/subtv2.jpg&lt;/thumbnail&gt;
					&lt;/subitem&gt;
				&lt;/subitems&gt;
			&lt;/subchannel&gt;
		&lt;/subchannels&gt;
	&lt;/channel&gt;
&lt;/channels&gt;
</pre>
<p>第一个节目目录 Channel 1 下面没有子目录，只有 TV1 和 TV2 这两个节目，因此 XML 层次是 <strong>channel/items/item</strong>。第二个节目目录 Channel 2 下有名为 Sub1 的子目录，Sub1 下又有 Sub TV1 和 Sub TV2 两个节目，这种的 XML 层次则是：<strong>channel/subchannels/subchannel/subitems/subitem</strong>。</p>
<p>需要注意的是 LiveStreams 的 XML 配置文件必须以 UTF-8 编码格式保存，否则非英文字符将无法正常显示。另外由于 XML 本身格式的原因，XML 内容中的这些字符需要转换（全部为纯英文字符，包括结束的分号）：</p>
<p><strong><span style="color: #ff0000;">&amp;</span></strong> 转换为 <strong><span style="color: #ff0000;">&amp;amp;</span></strong><br />
<strong><span style="color: #ff0000;">&lt;</span></strong> 转换为 <strong><span style="color: #ff0000;">&amp;lt;</span></strong><br />
<strong><span style="color: #ff0000;">&gt;</span></strong> 转换为 <strong><span style="color: #ff0000;">&amp;gt;</span></strong><br />
<strong><span style="color: #ff0000;">'</span></strong> 转换为 <strong><span style="color: #ff0000;">&amp;apos;</span></strong><br />
<strong><span style="color: #ff0000;">"</span></strong> 转换为 <strong><span style="color: #ff0000;">&amp;quot;</span></strong></p>
<p>最后附上我修改过的最新 LiveStreams 1.0.6 版本 XBMC 插件的下载地址：</p>
<p><a href="http://miseal.googlecode.com/files/plugin.video.live.streams-1.0.6.zip" target="_blank">http://miseal.googlecode.com/files/plugin.video.live.streams-1.0.6.zip</a></p>
<p>由于这插件只是随便修改的，有任何问题欢迎指正哦。 ^_^</p>
]]></content:encoded>
			<wfw:commentRss>https://zohead.com/archives/xbmc-livestreams-cn-patch/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
