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操作Memcache实例介绍
Jun 14 PHP
基于flush()不能按顺序输出时的解决办法
Jun 29 PHP
PHP判断远程图片或文件是否存在的实现代码
Feb 20 PHP
php+mysqli使用面向对象方式查询数据库实例
Jan 29 PHP
PHP常用的小程序代码段
Nov 14 PHP
让ThinkPHP的模板引擎达到最佳效率的方法详解
Mar 14 PHP
php封装db类连接sqlite3数据库的方法实例
Dec 19 PHP
PHP命名空间与自动加载类详解
Sep 04 PHP
PHP进阶学习之Geo的地图定位算法详解
Jun 19 PHP
Laravel基础-关于引入公共文件的两种方式
Oct 18 PHP
PHP Swoole异步MySQL客户端实现方法示例
Oct 24 PHP
laravel中Redis队列监听中断的分析
Sep 14 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 5.5 创建和验证哈希最简单的方法详解
2013/11/07 PHP
php操作mysql数据库的基本类代码
2014/02/25 PHP
2个Codeigniter文件批量上传控制器写法例子
2014/07/25 PHP
json跟xml的对比分析
2008/06/10 Javascript
jQuery div层的放大与缩小简单实现代码
2013/03/28 Javascript
基于javascript、ajax、memcache和PHP实现的简易在线聊天室
2015/02/03 Javascript
js实现可兼容IE、FF、Chrome、Opera及Safari的音乐播放器
2015/02/11 Javascript
原生js仿jquery animate动画效果
2016/07/13 Javascript
jQuery树形插件jquery.simpleTree.js用法分析
2016/09/05 Javascript
详解nodejs 文本操作模块-fs模块(四)
2016/12/22 NodeJs
Javascript中的prototype与继承
2017/02/06 Javascript
JS文件/图片从电脑里面拖拽到浏览器上传文件/图片
2017/03/08 Javascript
基于input框覆盖掉数字英文的实例讲解
2017/07/21 Javascript
原生JS实现循环Nodelist Dom列表的4种方式示例
2018/02/11 Javascript
详解使用jest对vue项目进行单元测试
2018/09/07 Javascript
微信小程序非swiper组件实现的自定义伪3D轮播图效果示例
2018/12/11 Javascript
Nodejs文件上传、监听上传进度的代码
2020/03/27 NodeJs
python使用wmi模块获取windows下硬盘信息的方法
2015/05/15 Python
解决Python传递中文参数的问题
2015/08/04 Python
Python+matplotlib实现填充螺旋实例
2018/01/15 Python
在python3.5中使用OpenCV的实例讲解
2018/04/02 Python
解决django 新增加用户信息出现错误的问题
2019/07/28 Python
Python使用matplotlib 画矩形的三种方式分析
2019/10/31 Python
浅析python表达式4+0.5值的数据类型
2020/02/26 Python
matplotlib基础绘图命令之bar的使用方法
2020/08/13 Python
python 实现网易邮箱邮件阅读和删除的辅助小脚本
2021/03/01 Python
HTML5 Canvas基本线条绘制的实例教程
2016/03/17 HTML / CSS
详解通过focusout事件解决IOS键盘收起时界面不归位的问题
2019/07/18 HTML / CSS
销售会计工作职责
2013/12/02 职场文书
共筑中国梦演讲稿
2014/04/23 职场文书
卫校毕业生个人自我鉴定
2014/04/28 职场文书
助人为乐模范事迹材料
2014/06/02 职场文书
病危通知书样本
2015/04/17 职场文书
小学教师暑期培训心得体会
2016/01/09 职场文书
小学美术教学反思
2016/02/17 职场文书
2019年年中工作总结讲话稿模板
2019/03/25 职场文书