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面向对象编程快速入门
Oct 09 PHP
php入门教程 精简版
Dec 13 PHP
彻底删除thinkphp3.1案例blog标签的方法
Dec 05 PHP
PHP实现的增强性mhash函数
May 27 PHP
smarty内部日期函数html_select_date()用法实例分析
Jul 08 PHP
遍历echsop的region表形成缓存的程序实例代码
Nov 01 PHP
php获取flash尺寸详细数据的方法
Nov 12 PHP
php pdo操作数据库示例
Mar 10 PHP
PHP实现执行外部程序的方法详解
Aug 17 PHP
thinkPHP框架实现的短信接口验证码功能示例
Jun 20 PHP
Laravel Validator 实现两个或多个字段联合索引唯一
May 08 PHP
使用laravel的migrate创建数据表的方法
Sep 30 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的access操作类
2008/04/09 PHP
PHP中array_merge和array相加的区别分析
2013/06/17 PHP
浅析PHP Socket技术
2013/08/02 PHP
PHP使用GETDATE获取当前日期时间作为一个关联数组的方法
2015/03/19 PHP
php爬取天猫和淘宝商品数据
2018/02/23 PHP
PHP的PDO事务与自动提交
2019/01/24 PHP
用Mootools获得操作索引的两种方法分享
2011/12/12 Javascript
jQuery ajax(复习)—Baidu ajax request分离版
2013/01/24 Javascript
jquery如何实现在加载完iframe的内容后再进行操作
2013/09/10 Javascript
Node.js 服务器端应用开发框架 -- Hapi.js
2014/07/29 Javascript
JavaScript判断文件上传类型的方法
2014/09/02 Javascript
AngularJS控制器之间的通信方式详解
2016/11/03 Javascript
Nodejs进阶:基于express+multer的文件上传实例
2016/11/21 NodeJs
React实践之Tree组件的使用方法
2017/09/30 Javascript
详解Vue用自定义指令完成一个下拉菜单(select组件)
2017/10/31 Javascript
javascript中toFixed()四舍五入使用方法详解
2018/09/28 Javascript
vue + typescript + video.js实现 流媒体播放 视频监控功能
2019/07/07 Javascript
详解如何在Vue项目中发送jsonp请求
2019/10/25 Javascript
ES11新增的这9个新特性,你都掌握了吗
2020/10/15 Javascript
[47:38]Optic vs VGJ.S 2018国际邀请赛小组赛BO2 第二场 8.17
2018/08/20 DOTA
Python selenium 三种等待方式解读
2016/09/15 Python
在cmder下安装ipython以及环境的搭建
2018/10/19 Python
Python使用LDAP做用户认证的方法
2019/06/20 Python
对python 树状嵌套结构的实现思路详解
2019/08/09 Python
在django模板中实现超链接配置
2019/08/21 Python
使用pickle存储数据dump 和 load实例讲解
2019/12/30 Python
python实现FTP循环上传文件
2020/03/20 Python
Python requests.post方法中data与json参数区别详解
2020/04/30 Python
Python的Django框架实现数据库查询(不返回QuerySet的方法)
2020/05/19 Python
英国家喻户晓的折扣商场:TK Maxx
2017/05/26 全球购物
欧洲、亚洲、非洲和拉丁美洲的度假套餐:Great Value Vacations
2019/03/30 全球购物
《鞋匠的儿子》教学反思
2014/03/02 职场文书
2014办公室副主任四风对照检查材料思想汇报
2014/09/20 职场文书
教师批评与自我批评材料
2014/10/16 职场文书
消防宣传语大全
2015/07/13 职场文书
红灯733-1型14管5波段半导体收音机
2021/04/22 无线电