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应用程序来获取Web服务器的状态信息
Oct 09 PHP
xml+php动态载入与分页
Oct 09 PHP
PHP页面间参数传递的四种方法详解
Jun 09 PHP
php中autoload的用法总结
Nov 08 PHP
php获取网页中图片、DIV内容的简单方法
Jun 19 PHP
php递归删除指定文件夹的方法小结
Apr 20 PHP
php根据用户语言跳转相应网页
Nov 04 PHP
PHP登录验证码的实现与使用方法
Jul 07 PHP
php基于websocket搭建简易聊天室实践
Oct 24 PHP
通过php动态传数据到highcharts
Apr 05 PHP
PHP如何根据文件头检测文件类型实例代码
Oct 14 PHP
php设计模式之策略模式实例分析【星际争霸游戏案例】
Mar 26 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
全国FM电台频率大全 - 8 黑龙江省
2020/03/11 无线电
图片存储与浏览一例(Linux+Apache+PHP+MySQL)
2006/10/09 PHP
PHP获取和操作配置文件php.ini的几个函数介绍
2013/06/24 PHP
php 验证码(倾斜,正弦干扰线,黏贴,旋转)
2013/06/29 PHP
PHP实现读取一个1G的文件大小
2013/08/24 PHP
php导出word格式数据的代码实例
2013/11/25 PHP
jquery select(列表)的操作(取值/赋值)
2009/08/06 Javascript
动态加载外部javascript文件的函数代码分享
2011/07/28 Javascript
THREE.JS入门教程(3)着色器-下
2013/01/24 Javascript
jQuery实现复选框成对选择及对应取消的方法
2015/03/03 Javascript
js实现同一个页面多个渐变效果的方法
2015/04/10 Javascript
JS/Jquery判断对象为空的方法
2015/06/11 Javascript
javascript回到顶部特效
2016/07/30 Javascript
浅谈js函数的多种定义方法与区别
2016/11/29 Javascript
Vue 组件间的样式冲突污染
2017/08/31 Javascript
JavaScript中的this原理及6种常见使用场景详解
2020/02/14 Javascript
python连接MySQL、MongoDB、Redis、memcache等数据库的方法
2013/11/15 Python
python查询sqlite数据表的方法
2015/05/08 Python
举例讲解Python设计模式编程中的访问者与观察者模式
2016/01/26 Python
Python爬虫抓取代理IP并检验可用性的实例
2018/05/07 Python
Python 运行 shell 获取输出结果的实例
2019/01/07 Python
使用python serial 获取所有的串口名称的实例
2019/07/02 Python
python智联招聘爬虫并导入到excel代码实例
2019/09/09 Python
django实现支付宝支付实例讲解
2019/10/17 Python
python获取依赖包和安装依赖包教程
2020/02/13 Python
logging level级别介绍
2020/02/21 Python
基于python实现计算且附带进度条代码实例
2020/03/31 Python
python实现控制台输出颜色
2021/03/02 Python
CSS3 mask 遮罩的具体使用方法
2017/11/03 HTML / CSS
印尼在线精品店:Berrybenka.com
2016/10/22 全球购物
计算机应用与科学个人的自我评价
2013/11/15 职场文书
家长寄语大全
2014/04/02 职场文书
旅游与环境专业求职信
2014/06/05 职场文书
教师纪念9.18事件演讲稿范文
2014/09/14 职场文书
导游词之南京栖霞山
2019/10/18 职场文书
js实现自动锁屏功能
2021/06/02 Javascript