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和ACCESS写聊天室(八)
Oct 09 PHP
PHP 在5.1.* 和5.2.*之间 PDO数据库操作中的不同之处小结
Mar 07 PHP
php调用dll的实例操作动画与代码分享
Aug 14 PHP
php中的boolean(布尔)类型详解
Oct 28 PHP
md5 16位二进制与32位字符串相互转换示例
Dec 30 PHP
微信公众平台天气预报功能开发
Jul 06 PHP
YII实现分页的方法
Jul 09 PHP
php生成curl命令行的方法
Dec 14 PHP
PHP编程基本语法快速入门手册
Jan 07 PHP
php操作xml并将其插入数据库的实现方法
Sep 08 PHP
PHP实现导出excel数据的类库用法示例
Oct 15 PHP
PHP使用mysqli操作MySQL数据库的简单方法
Feb 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 日常开发小技巧
2009/09/23 PHP
ThinkPHP分组下自定义标签库实例
2014/11/01 PHP
thinkPHP自动验证、自动添加及表单错误问题分析
2016/10/17 PHP
php通过PHPExcel导入Excel表格到MySQL数据库的简单实例
2016/10/29 PHP
php 比较获取两个数组相同和不同元素的例子(交集和差集)
2019/10/18 PHP
有关javascript的性能优化 (repaint和reflow)
2013/04/12 Javascript
jQuery之选择组件的深入解析
2013/06/19 Javascript
查看大图功能代码jquery版
2013/11/05 Javascript
javascript打印输出json实例
2013/11/11 Javascript
javascript的 {} 语句块详解
2016/02/27 Javascript
JS/jQ实现免费获取手机验证码倒计时效果
2016/06/13 Javascript
JSONP跨域请求实例详解
2016/07/04 Javascript
Javascript 闭包详解及实例代码
2016/11/30 Javascript
bootstrap fileinput 上传插件的基础使用
2017/02/17 Javascript
微信小程序 图片加载(本地,网路)实例详解
2017/03/10 Javascript
three.js加载obj模型的实例代码
2017/11/10 Javascript
jQuery插件实现弹性运动完整示例
2018/07/07 jQuery
Vue中的transition封装组件的实现方法
2019/08/13 Javascript
js 获取扫码枪输入数据的方法
2020/06/10 Javascript
python中列表元素连接方法join用法实例
2015/04/07 Python
python GUI库图形界面开发之PyQt5树形结构控件QTreeWidget详细使用方法与实例
2020/03/02 Python
parser.add_argument中的action使用
2020/04/20 Python
Jupyter notebook如何修改平台字体
2020/05/13 Python
Python连接Mysql进行增删改查的示例代码
2020/08/03 Python
巧克力领导品牌瑞士莲美国官网:Lindt Chocolate美国
2016/08/25 全球购物
荷兰手表网站:Watch2Day
2018/07/02 全球购物
瑞士男士时尚网上商店:Babista
2020/05/14 全球购物
技校生自我鉴定
2013/12/08 职场文书
关于逃课的检讨书
2014/01/23 职场文书
2015年档案管理工作总结
2015/04/08 职场文书
个人向公司借款协议书
2016/03/19 职场文书
暑假开始了,你的暑假学习计划写好了吗?
2019/07/04 职场文书
Java实现斗地主之洗牌发牌
2021/06/14 Java/Android
Python实现学生管理系统(面向对象版)
2021/06/24 Python
nginx共享内存的机制详解
2022/03/21 Servers
Java 超详细讲解ThreadLocal类的使用
2022/04/07 Java/Android