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 相关文章推荐
dede全站URL静态化改造[070414更正]
Apr 17 PHP
php学习 函数 课件
Jun 15 PHP
MySql 按时间段查询数据方法(实例说明)
Nov 02 PHP
基于asp+ajax和数据库驱动的二级联动菜单
May 06 PHP
PHP动态生成javascript文件的2个例子
Apr 11 PHP
ThinkPHP3.1新特性之对页面压缩输出的支持
Jun 19 PHP
php生成随机数的三种方法
Sep 10 PHP
php异步多线程swoole用法实例
Nov 14 PHP
Yii不依赖Model的表单生成器用法实例
Dec 04 PHP
PHP获取ip对应地区和使用网络类型的方法
Mar 11 PHP
PHP自定义函数实现格式化秒的方法
Sep 14 PHP
PHP依赖注入原理与用法分析
Aug 21 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 定界符格式引起的错误
2011/05/24 PHP
学习php过程中的一些注意点的总结
2013/10/25 PHP
学习php设计模式 php实现装饰器模式(decorator)
2015/12/07 PHP
[原创]PHPCMS遭遇会员投稿审核无效的解决方法
2017/01/11 PHP
thinkphp框架类库扩展操作示例
2019/11/26 PHP
Javascript Select操作大集合
2009/05/26 Javascript
Javascript设置对象的ReadOnly属性(示例代码)
2013/12/25 Javascript
jQuery中prepend()方法使用详解
2015/08/11 Javascript
jQuery实现仿QQ在线客服效果的滚动层代码
2015/10/15 Javascript
JavaScript 消息框效果【实现代码】
2016/04/27 Javascript
jQuery 的 ready()的纯js替代方法
2016/11/20 Javascript
js 文字超出长度用省略号代替,鼠标悬停并以悬浮框显示实例
2016/12/06 Javascript
Jquery实现跨域异步上传文件总结
2017/02/03 Javascript
jQuery EasyUI 组件加上“清除”功能实例详解
2017/04/11 jQuery
浅谈对Angular中的生命周期钩子的理解
2017/07/31 Javascript
利用JS制作万年历的方法
2017/08/16 Javascript
jquery中有哪些api jQuery主要API
2017/11/20 jQuery
Vue中使用的EventBus有生命周期
2018/07/12 Javascript
微信小程序实现自定义加载图标功能
2018/07/19 Javascript
微信小程序学习笔记之函数定义、页面渲染图文详解
2019/03/28 Javascript
vue $set 给数据赋值的实例
2019/11/09 Javascript
NodeJS多种创建WebSocket监听的方式(三种)
2020/06/04 NodeJs
Python的字典和列表的使用中一些需要注意的地方
2015/04/24 Python
python在linux系统下获取系统内存使用情况的方法
2015/05/11 Python
横向对比分析Python解析XML的四种方式
2016/03/30 Python
django解决跨域请求的问题详解
2019/01/20 Python
Python查找最长不包含重复字符的子字符串算法示例
2019/02/13 Python
nginx+uwsgi+django环境搭建的方法步骤
2019/11/25 Python
Python中的 ansible 动态Inventory 脚本
2020/01/19 Python
浅谈python处理json和redis hash的坑
2020/07/16 Python
Web时代变迁及html5与html4的区别
2016/01/06 HTML / CSS
HTML5的Video标签有部分MP4无法播放的问题解析(多图)
2017/08/18 HTML / CSS
欧洲顶级体育电子商务网站:SportsShoes.com
2018/03/27 全球购物
N.Peal官网:来自伦敦的高档羊绒品牌
2018/10/29 全球购物
新年寄语大全
2014/04/12 职场文书
多表查询、事务、DCL
2021/04/05 MySQL