php&java(三)

2016-02-19 09:36 2 1 收藏

今天天气好晴朗处处好风光,好天气好开始,图老师又来和大家分享啦。下面给大家推荐php&java(三),希望大家看完后也有个好心情,快快行动吧!

【 tulaoshi.com - Web开发 】

例子二:通过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

来源:https://www.tulaoshi.com/n/20160219/1591387.html

延伸阅读
标签: PHP
  echo "<br";     echo "<TABLE width=85% border=0 cellPadding=0 cellSpacing=1 align=center bgcolor='#6CA6CD'";       echo "<tr<td";       echo "<TABLE width=100% cellPadding=1 cellS...
标签: PHP
  function sdatetime($str){     $str=ereg_replace("-","/",$str);     $str=substr($str,5,strlen($str)-8);     return $str; } function reAnum($sid){ //羆莱絞计     $DROOT=getenv('DOCUMENT_ROOT');  &nbs...
标签: PHP
  //此为bbs.sql CREATE TABLE bbs_area (    id int(10) unsigned DEFAULT '0' NOT NULL auto_increment,    master varchar(150) NOT NULL,    banner varchar(150) NOT NULL,    body text NOT NULL,    email varchar(50),    lo...
标签: Web开发
//第一个页面  name="index1.php" ? include "../signup/mysql.php"; $time1=time(); //$userid=983530409; echo $n; if($n==1){ $userid=time(); $nick="g".$userid; $last_time=$userid; $sql="insert into chat_user (userid,id,last_time) values ('$userid','$nick','$last_t...
标签: Web开发
第五个页面name="changenick.php" ?    include "../signup/mysql.php"; //echo $userid; if($action=="ok") { $sql="select * from chat_user_list where userid='$nick'"; $result=mysql_query($sql) or die(mysql_error()); if(!$result) echo "该呢称已被他人占用 请...

经验教程

781

收藏

57

精华推荐

聊天室php&amp;mysql(四)

聊天室php&amp;mysql(四)

博晓Lucky

聊天室php&amp;mysql(二)

聊天室php&amp;mysql(二)

快乐每一天_801

聊天室php&mysql(三)

聊天室php&mysql(三)

苏坡man〆

微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部