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对gzip文件或者字符串解压实例参考
Jul 25 PHP
php 清除网页病毒的方法
Dec 05 PHP
PHP 获取MSN好友列表的代码(2009-05-14测试通过)
Sep 09 PHP
PHP 数据结构 算法描述 冒泡排序 bubble sort
Jul 10 PHP
关于php程序报date()警告的处理(date_default_timezone_set)
Oct 22 PHP
PHP使用DOMDocument类生成HTML实例(包含常见标签元素)
Jun 25 PHP
linux下安装php的memcached客户端
Aug 03 PHP
PHP连接操作access数据库实例
Mar 30 PHP
完美解决在ThinkPHP控制器中命名空间的问题
May 05 PHP
PHP对象的浅复制与深复制的实例详解
Oct 26 PHP
php常用的工具开发整理
Sep 26 PHP
php 利用socket发送GET,POST请求的实例代码
Jul 04 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相当简单的分页类
2008/10/02 PHP
ini_set的用法介绍
2014/01/07 PHP
php技巧小结【推荐】
2017/01/19 PHP
接收键盘指令的脚本
2006/06/26 Javascript
有趣的JavaScript数组长度问题代码说明
2011/01/20 Javascript
JS小游戏之极速快跑源码详解
2014/09/25 Javascript
JavaScript实现倒计时代码段Item1(非常实用)
2015/11/03 Javascript
轻松实现jquery选项卡切换效果
2016/10/10 Javascript
jquery处理checkbox(复选框)是否被选中实例代码
2017/06/12 jQuery
JavaScript之class继承_动力节点Java学院整理
2017/07/03 Javascript
基于Swiper实现移动端页面图片轮播效果
2017/12/28 Javascript
VUE中v-on:click事件中获取当前dom元素的代码
2018/08/22 Javascript
微信小程序使用map组件实现路线规划功能示例
2019/01/22 Javascript
vue本地打开build后生成的dist文件夹index.html问题
2019/09/04 Javascript
小程序实现锚点滑动效果
2019/09/23 Javascript
[01:01:25]DOTA2上海特级锦标赛B组资格赛#2 Fnatic VS Spirit第三局
2016/02/27 DOTA
Python 2.7.x 和 3.x 版本的重要区别小结
2014/11/28 Python
在Django中同时使用多个配置文件的方法
2015/07/22 Python
python制作一个桌面便签软件
2015/08/09 Python
Python基于matplotlib绘制栈式直方图的方法示例
2017/08/09 Python
Python对List中的元素排序的方法
2018/04/01 Python
Python实现的拟合二元一次函数功能示例【基于scipy模块】
2018/05/15 Python
Python面向对象之类和对象属性的增删改查操作示例
2018/12/14 Python
python找出一个列表中相同元素的多个索引实例
2019/06/11 Python
解决pyinstaller打包运行程序时出现缺少plotly库问题
2020/06/02 Python
python 如何用urllib与服务端交互(发送和接收数据)
2021/03/04 Python
Css3+Js制作漂亮时钟(附源码)
2013/04/24 HTML / CSS
销售人员个人求职信
2013/09/26 职场文书
岗位职责风险防控
2014/02/18 职场文书
物资采购方案
2014/06/12 职场文书
2014医学院领导干部四风对照检查材料思想汇报
2014/09/16 职场文书
2014年乡镇工作总结
2014/11/21 职场文书
2015年计划生育责任书
2015/05/08 职场文书
2016党员干部政治学习心得体会
2016/01/23 职场文书
python实现Thrift服务端的方法
2021/04/20 Python
微信小程序APP的事件绑定以及传递参数时的冒泡和捕获
2022/04/19 Javascript