国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php&java(三)

2024-05-04 23:00:34
字體:
來源:轉載
供稿:網友
例子二:通過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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 秭归县| 陆川县| 陈巴尔虎旗| 孝感市| 丰都县| 上杭县| 汽车| 兴海县| 临武县| 迁西县| 渑池县| 乌兰县| 化隆| 温宿县| 漠河县| 仪陇县| 阿拉善盟| 德兴市| 凤山市| 长宁县| 威海市| 台州市| 包头市| 鄯善县| 横峰县| 泾阳县| 鄂托克前旗| 鹤山市| 龙胜| 望城县| 治多县| 宝鸡市| 沁水县| 泸水县| 玉龙| 祁阳县| 天津市| 高唐县| 泽州县| 孝昌县| 长汀县|