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高效率写法(详解原因)
Jun 20 PHP
163的邮件用phpmailer发送(实例详解)
Jun 24 PHP
解析PHP中VC6 X86和VC9 X86的区别及 Non Thread Safe的意思
Jun 28 PHP
win7 64位系统 配置php最新版开发环境(php+Apache+mysql)
Aug 15 PHP
ThinkPHP实现带验证码的文件上传功能实例
Nov 01 PHP
php根据一个给定范围和步进生成数组的方法
Jun 19 PHP
PHP使用array_merge重新排列数组下标的方法
Jul 22 PHP
php文件上传、下载和删除示例
Aug 28 PHP
Yii2语言国际化自动配置详解
Aug 22 PHP
PHP连接及操作PostgreSQL数据库的方法详解
Jan 30 PHP
PHP attributes()函数讲解
Feb 03 PHP
layui数据表格自定义每页条数limit设置
Oct 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
php mysql Errcode: 28 终极解决方法
2009/07/01 PHP
PHP header()函数使用详细(301、404等错误设置)
2013/04/17 PHP
php 修改、增加xml结点属性的实现代码
2013/10/22 PHP
PHP利用MySQL保存session的实现思路及示例代码
2014/09/09 PHP
php实现按照权重随机排序数据的方法
2015/01/09 PHP
老生常谈PHP面向对象之标识映射
2017/06/21 PHP
ThinkPHP框架实现的MySQL数据库备份功能示例
2018/05/24 PHP
PHP pthreads v3在centos7平台下的安装与配置操作方法
2020/02/21 PHP
HTTP头隐藏PHP版本号实现过程解析
2020/12/09 PHP
AppBaseJs 类库 网上常用的javascript函数及其他js类库写的
2010/03/04 Javascript
JavaScript获取onclick、onchange等事件值的代码
2013/07/22 Javascript
JavaScript判断数组是否包含指定元素的方法
2015/07/01 Javascript
JS搜狐面试题分析
2016/12/16 Javascript
vue-resource调用promise取数据方式详解
2017/07/21 Javascript
React BootStrap用户体验框架快速上手
2018/03/06 Javascript
vue树形结构获取键值的方法示例
2018/06/21 Javascript
使用async await 封装 axios的方法
2018/07/09 Javascript
全面分析JavaScript 继承
2019/05/30 Javascript
Vue.js数字输入框组件使用方法详解
2019/10/19 Javascript
react quill中图片上传由默认转成base64改成上传到服务器的方法
2019/10/30 Javascript
详解Vue之事件处理
2020/07/10 Javascript
node koa2 ssr项目搭建的方法步骤
2020/12/11 Javascript
python每隔N秒运行指定函数的方法
2015/03/16 Python
pandas ix &amp;iloc &amp;loc的区别
2019/01/10 Python
python实现字符串加密 生成唯一固定长度字符串
2019/03/22 Python
Python使用lambda抛出异常实现方法解析
2020/08/20 Python
用css3实现当鼠标移进去时当前亮其他变灰效果
2014/04/08 HTML / CSS
CSS3中的display:grid,网格布局介绍
2019/10/30 HTML / CSS
韩国11街:11STREET
2018/03/27 全球购物
法国二手手袋、手表和奢侈珠宝购物网站:Collector Square
2018/07/05 全球购物
String s = new String(“xyz”);创建了几个String Object?
2015/08/05 面试题
会计电算化专业毕业生自荐信
2013/12/20 职场文书
铲车司机岗位职责
2014/03/15 职场文书
复兴之路观后感
2015/06/02 职场文书
超级实用的公文标题大全!
2019/07/19 职场文书
SQL SERVER中的流程控制语句
2022/05/25 SQL Server