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 相关文章推荐
VFP与其他应用程序的集成
Oct 09 PHP
php 更新数据库中断的解决方法
Jun 05 PHP
将一维或多维的数组连接成一个字符串的php代码
Aug 08 PHP
php中0,null,empty,空,false,字符串关系的详细介绍
Jun 20 PHP
解析关于java,php以及html的所有文件编码与乱码的处理方法汇总
Jun 24 PHP
如何修改和添加Apache的默认站点目录
Jul 05 PHP
PHPCMS2008广告模板SQL注入漏洞修复
Oct 11 PHP
php 生成签名及验证签名详解
Oct 26 PHP
PHP设置Cookie的HTTPONLY属性方法
Feb 09 PHP
php生成网页桌面快捷方式
May 05 PHP
Laravel 对某一列进行筛选然后求和sum()的例子
Oct 10 PHP
php判断数组是否为空的实例方法
May 10 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
PHP 数组遍历foreach语法结构及实例
2016/06/13 PHP
浅谈PHP中的数据传输CURL
2016/09/06 PHP
详解PHP素材图片上传、下载功能
2019/04/12 PHP
prototype与jquery下Ajax实现的差别
2009/09/13 Javascript
Firefox下提示illegal character并出现乱码的原因
2010/03/25 Javascript
利用腾讯的ip地址库做ip物理地址定位
2010/07/24 Javascript
获得所有表单值的JQuery实现代码[IE暂不支持]
2012/05/24 Javascript
JS的Document属性和方法小结
2013/09/17 Javascript
NodeJS学习笔记之Http模块
2015/01/13 NodeJs
javascript将数字转换整数金额大写的方法
2015/01/27 Javascript
jquery转盘抽奖功能实现
2015/11/13 Javascript
jQuery设置Easyui校验规则(推荐)
2016/11/21 Javascript
JS实现全屏的四种写法
2016/12/30 Javascript
浅谈js使用in和hasOwnProperty获取对象属性的区别
2017/04/27 Javascript
D3.js实现简洁实用的动态仪表盘的示例
2018/04/04 Javascript
vue input输入框模糊查询的示例代码
2018/05/22 Javascript
JavaScript中 ES6变量的结构赋值
2018/07/10 Javascript
vue.js 添加 fastclick的支持方法
2018/08/28 Javascript
原生js实现获取form表单数据代码实例
2019/03/27 Javascript
[45:52]完美世界DOTA2联赛PWL S3 Forest vs INK ICE 第二场 12.09
2020/12/12 DOTA
python通过exifread模块获得图片exif信息的方法
2015/03/16 Python
Linux RedHat下安装Python2.7开发环境
2017/05/20 Python
使用Python写一个量化股票提醒系统
2018/08/22 Python
Python日志处理模块logging用法解析
2020/05/19 Python
StubHub墨西哥:购买和出售您的门票
2016/09/17 全球购物
什么是触发器(trigger)? 触发器有什么作用?
2013/09/18 面试题
商场总经理岗位职责
2014/02/03 职场文书
电子装配专业毕业生求职信
2014/04/23 职场文书
信电学院毕业生自荐书
2014/05/24 职场文书
体育运动会广播稿
2014/10/05 职场文书
环境卫生工作汇报材料
2014/10/28 职场文书
共青团员自我评价
2015/03/10 职场文书
Go 自定义package包设置与导入操作
2021/05/06 Golang
Mysql超详细讲解死锁问题的理解
2022/04/01 MySQL
Python+Tkinter制作专属图形化界面
2022/04/01 Python
Java详细解析==和equals的区别
2022/04/07 Java/Android