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 相关文章推荐
用Socket发送电子邮件
Oct 09 PHP
PHP的FTP学习(四)
Oct 09 PHP
PHP Header用于页面跳转要注意的几个问题总结
Oct 03 PHP
用php实现的下载css文件中的图片的代码
Feb 08 PHP
php绝对路径与相对路径之间关系的的分析
Mar 03 PHP
PHP垃圾回收机制简单说明
Jul 22 PHP
php合并数组中相同元素的方法
Nov 13 PHP
jQuery Mobile + PHP实现文件上传
Dec 12 PHP
php实现判断访问来路是否为搜索引擎机器人的方法
Apr 15 PHP
PHP实现抓取迅雷VIP账号的方法
Jul 30 PHP
PHP批量删除jQuery操作
Jul 23 PHP
PHP7内核之Reference详解
Mar 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
德生1994机评
2021/03/02 无线电
在字符串中把网址改成超级链接
2006/10/09 PHP
iis下php mail函数的sendmail配置方法(官方推荐)
2012/04/25 PHP
解密ThinkPHP3.1.2版本之独立分组功能应用
2014/06/19 PHP
PHP自带函数给数字或字符串自动补齐位数
2014/07/29 PHP
PHP浮点数的一个常见问题
2016/03/10 PHP
js关闭当前页面(窗口)的几种方式总结
2013/03/05 Javascript
jquery快捷动态绑定键盘事件的操作函数代码
2013/10/17 Javascript
JS中类或对象的定义说明
2014/03/10 Javascript
JavaScript fontsize方法入门实例(按照指定的尺寸来显示字符串)
2014/10/17 Javascript
通用无限极下拉菜单的实现代码
2016/05/31 Javascript
AngularJs Javascript MVC 框架
2016/06/20 Javascript
可输入文字查找ajax下拉框控件 ComBox的实现方法
2016/10/25 Javascript
javascript简单进制转换实现方法
2016/11/24 Javascript
详解node中创建服务进程
2017/05/09 Javascript
Bootstrap Table使用整理(二)
2017/06/09 Javascript
vuex操作state对象的实例代码
2018/04/25 Javascript
JavaScript中的Proxy对象
2020/11/27 Javascript
[04:51]TI10典藏宝瓶Ⅱ外观视频展示
2020/08/15 DOTA
python实现字符串连接的三种方法及其效率、适用场景详解
2017/01/13 Python
ubuntu系统下使用pm2设置nodejs开机自启动的方法
2018/05/12 NodeJs
python根据list重命名文件夹里的所有文件实例
2018/10/25 Python
Python中的heapq模块源码详析
2019/01/08 Python
对YOLOv3模型调用时候的python接口详解
2019/08/26 Python
浅谈python print(xx, flush = True) 全网最清晰的解释
2020/02/21 Python
Python3 xml.etree.ElementTree支持的XPath语法详解
2020/03/06 Python
详解如何在pyqt中通过OpenCV实现对窗口的透视变换
2020/09/20 Python
Python如何急速下载第三方库详解
2020/11/02 Python
婚礼证婚人证婚词
2014/01/08 职场文书
党的生日演讲稿
2014/09/10 职场文书
2014年终个人工作总结
2014/11/07 职场文书
2014年医德医风工作总结
2014/11/13 职场文书
2014年机关后勤工作总结
2014/12/16 职场文书
新郎婚礼答谢词
2015/01/04 职场文书
简单总结SpringMVC拦截器的使用方法
2021/06/28 Java/Android
springboot入门 之profile设置方式
2022/04/04 Java/Android