php&java(三)


Posted in PHP onOctober 09, 2006

例子二:通过Xalan 1.2,使用XSLT转换XML

做为第二个例子,我们使用了Xalan-java的XSLT引擎,这个引擎来自于APACHE的XML项目,使用这个程序,我们能够使用XSL转换XML源文件。这将极大的方便我们处理文档和进行内容管理。

开始之前,我们需要将xerces.jar 和 xalan.jar文件放入java.class.path目录下(这两个文件包含在Xalan-Java 1.2 中,可以从xml.apache.org处下载)。
PHP程序如下:
函数xslt_transform()以XML和XSL文件为参数,形式可为文件名(如:foo.xml)或URL(如:http://localhost/foo.xml)。

<?php

function xslt_transform($xml,$xsl) {

  // Create a XSLTProcessorFactory object. XSLTProcessorfactory is a Java
  // class which manufactures the processor for performing transformations.
  $XSLTProcessorFactory = new java("org.apache.xalan.xslt.XSLTProcessorFactory");

  // Use the XSLTProcessorFactory method getProcessor() to create a
  // new XSLTProcessor object.
  $XSLTProcessor = $XSLTProcessorFactory->getProcessor();

  // Use XSLTInputSource objects to provide input to the XSLTProcessor
  // process() method for transformation. Create objects for both the
  // xml source as well as the XSL input source. Parameter of
  // XSLTInputSource is (in this case) a 'system identifier' (URI) which
  // can be an URL or filename. If the system identifier is an URL, it
  // must be fully resolved.
  $xmlID = new java("org.apache.xalan.xslt.XSLTInputSource", $xml);
  $stylesheetID = new java("org.apache.xalan.xslt.XSLTInputSource", $xsl);

  // Create a stringWriter object for the output.
  $stringWriter = new java("java.io.StringWriter");

  // Create a ResultTarget object for the output with the XSLTResultTarget
  // class. Parameter of XSLTResultTarget is (in this case) a 'character
  // stream', which is the stringWriter object.  
  $resultTarget = new java("org.apache.xalan.xslt.XSLTResultTarget", $stringWriter);

  // Process input with the XSLTProcessors' method process(). This
  // method uses the XSL stylesheet to transform the XML input, placing
  // the result in the result target.
  $XSLTProcessor->process($xmlID,$stylesheetID,$resultTarget);

  // Use the stringWriters' method toString() to
  // return the buffer's current value as a string to get the
  // transformed result.
  $result = $stringWriter->toString();
  $stringWriter->close();
  return($result);
}

?>

函数定义好后,我们就可以调用它了,在下面的例程中,变量$xml指向一个URL字符串,$xsl也是如此。这个例子将显示5个最新的phpbuilder.com文章标题。

<?php

$xml = "http://www.phpbuilder.com/rss_feed.php?type=articles&limit=5";
$xsl = "http://www.soeterbroek.com/code/xml/rss_html.xsl";
$out = xslt_transform($xml,$xsl);
echo $out;

?>

如果你在本地机上运行程序,必须确保你的函数参数指向正确的文件名。

<?php

$xml  = "/web/htdocs/xml_java/rss_feed.xml";
$xsl  = "/web/htdocs/xml_java/rss_html.xsl";
$out = xslt_transform($xml,$xsl);
echo $out;

?>

虽然这种效果我们可以通过其它方法实现,或许那些方法更好,但这个例子能让你对PHP调用JAVA类有一个更好的了解。

教程结束了,希望你能够从这篇教程中学到点东西,以下是一些你用得到的链接:
http://www.php4win.de ~ A great Win32 distribution of PHP
http://www.javasoft.com ~ Sun's Java release
http://www.jars.com ~ Start searching for handy Java classes
http://www.gamelan.com ~ More Java classes
http://www.technetcast.com/tnc_play_stream.html?stream_id=400 ~ Sam Ruby about PHP and Java integration at Open Source Convention 2000 (audio)
http://xml.apache.org ~ Apache XML Project
http://www.phpbuilder.com/columns/justin20001025.php3 ~ Transforming XML with XSL using Sablotron

PHP 相关文章推荐
php smarty模版引擎中的缓存应用
Dec 02 PHP
php生成的html meta和link标记在body标签里 顶部有个空行
May 18 PHP
PHP可逆加密/解密函数分享
Sep 25 PHP
解析dedeCMS验证码的实现代码
Jun 07 PHP
php读取文件内容的几种方法详解
Jun 26 PHP
通过dbi使用perl连接mysql数据库的方法
Apr 16 PHP
PHP中常用的输出函数总结
Sep 22 PHP
PHP实现多图上传(结合uploadify插件)思路分析
Nov 30 PHP
使用PHP+MySql+Ajax+jQuery实现省市区三级联动功能示例
Sep 15 PHP
PHP PDOStatement::setAttribute讲解
Feb 01 PHP
php面试实现反射注入的详细方法
Sep 30 PHP
PHP实现长轮询消息实时推送功能代码实例讲解
Feb 26 PHP
一个用于mysql的数据库抽象层函数库
Oct 09 #PHP
教你如何把一篇文章按要求分段
Oct 09 #PHP
全文搜索和替换
Oct 09 #PHP
转换中文日期的PHP程序
Oct 09 #PHP
PHP网上调查系统
Oct 09 #PHP
PHP的ASP防火墙
Oct 09 #PHP
一个高ai的分页函数和一个url函数
Oct 09 #PHP
You might like
session 的生命周期是多长
2006/10/09 PHP
怎么样可以把 phpinfo()屏蔽掉?
2006/11/24 PHP
服务器端解压缩zip的脚本
2006/12/22 PHP
PHP乱码问题,UTF-8乱码常见问题小结
2012/04/09 PHP
php中is_null,empty,isset,unset 的区别详细介绍
2013/04/28 PHP
Memcached常用命令以及使用说明详解
2013/06/27 PHP
PHP实现将科学计数法转换为原始数字字符串的方法
2014/12/16 PHP
JavaScript 编写匿名函数的几种方法
2010/02/21 Javascript
asp.net下使用jquery 的ajax+WebService+json 实现无刷新取后台值的实现代码
2010/09/19 Javascript
使用Jquery来实现可以输入值的下拉选单 雏型
2011/12/06 Javascript
HTML,CSS,JavaScript速查表推荐
2014/12/02 Javascript
详解JavaScript中shift()方法的使用
2015/06/09 Javascript
js将滚动条滚动到指定位置的简单实现方法
2016/06/25 Javascript
jQuery fadeOut 异步实例代码详解
2016/08/18 Javascript
JS动态生成年份和月份实例代码
2017/02/04 Javascript
JS实现汉字与Unicode码相互转换的方法详解
2017/04/28 Javascript
Angular2关于@angular/cli默认端口号配置的问题
2017/07/15 Javascript
对于input 框限定输入值为浮点型的js代码
2017/09/25 Javascript
解决vue中无法动态修改jqgrid组件 url地址的问题
2018/03/01 Javascript
Vue 全局loading组件实例详解
2018/05/29 Javascript
vue+vue-router转场动画的实例代码
2018/09/01 Javascript
Vue CLI3 开启gzip压缩文件的方式
2018/09/30 Javascript
[55:44]OG vs NAVI 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/17 DOTA
Python文件夹与文件的相关操作(推荐)
2016/07/25 Python
python 全文检索引擎详解
2017/04/25 Python
pandas.DataFrame选取/排除特定行的方法
2018/07/03 Python
python将字符串转换成json的方法小结
2019/07/09 Python
python socket通信编程实现文件上传代码实例
2019/12/14 Python
tensorflow之读取jpg图像长和宽实例
2020/06/18 Python
详解python 内存优化
2020/08/17 Python
html5配合css3实现带提示文字的输入框(摆脱js)
2013/03/08 HTML / CSS
Bitiba意大利:在线宠物商店
2020/10/31 全球购物
如果有两个类A,B,怎么样才能使A在发生一个事件的时候通知B
2016/03/12 面试题
偷看我的初中毕业鉴定
2014/01/29 职场文书
铅球加油稿100字
2014/09/26 职场文书
2019XX公司员工考核管理制度!
2019/08/07 职场文书