php&java(三)_PHP教程
例子二:透過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)。
function xslt_transform($xml,$xsl) {
// Create a XSLTProcessorFactory object。 the processor for performing transformations.
$XSLTProcessorFactory = new java("org.apache.xalan.xslt.XSLTProcessorFactory");
/ new XSLTProcessor object.
$XSLTProcessor = $XSLTProcessorFactory->getProcessor();
// Use XSLTInputSource s for both the
// xml source as well as the XSL input source. Parameter of
// XSLTInputSource is (in this case) a 'system identifier' (URI) which
或 filename. If the system identifier is an URL, it
// must be fully resolved.
$xmlID = new java("org.apache.xalan.xslt.XSLTInputSource", $kInputSource", $xml
// Create a stringWriter object for the output.
io.StringWriter");
// Create a ResultTarget object for the output with the XSLTResultTarget
// class. Parameter of XSLTResultTarget is (in this case) a ' , which is the stringWriter object.
$resultTarget = new java("org.apache.xalan.xslt.XSLTResultTarget", $stringWriter);
// method uses the XSL stylesheet to transform the XML input, placing
// the result in the result target.
$XSLTProcessor->process($ID,$,$,$0,$$);
// Use the stringWriters' method toString() to
// return the buffer's current value as a string to get the
o >toString();
$stringWriter->close();
return($result);
}
?>
函數定義好後,我們就定義好後,我們就定義好後,我們就定義好後,我們就定義好後,我們就定義好後,我們定義好可以呼叫它了,在下面的例程中,變數$xml指向一個URL字串,$xsl也是如此。這個範例將顯示5個最新的phpbuilder.com文章標題。
$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;
?>
如果你在本機上執行程序,必須確保你的函數參數指向正確的檔案名稱。
$xml = "/web/htdocs/xml_java/rss_feed.xml";
$xsl = "/web/htdocs/xml_xml/rss_html.java/rss_html.javasl";
$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

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

session_start()iscucialinphpformanagingusersessions.1)ItInitiateSanewsessionifnoneexists,2)resumesanexistingsessions,and3)setsasesessionCookieforContinuityActinuityAccontinuityAcconActInityAcconActInityAcconAccRequests,EnablingApplicationsApplicationsLikeUseAppericationLikeUseAthenticationalticationaltication and PersersonalizedContentent。

MySQL函數可用於數據處理和計算。 1.基本用法包括字符串處理、日期計算和數學運算。 2.高級用法涉及結合多個函數實現複雜操作。 3.性能優化需避免在WHERE子句中使用函數,並使用GROUPBY和臨時表。

Composer是PHP的依賴管理工具,通過composer.json文件管理項目依賴。 1)解析composer.json獲取依賴信息;2)解析依賴關係形成依賴樹;3)從Packagist下載並安裝依賴到vendor目錄;4)生成composer.lock文件鎖定依賴版本,確保團隊一致性和項目可維護性。

在MySQL中配置字符集和排序規則的方法包括:1.設置服務器級別的字符集和排序規則:SETNAMES'utf8';SETCHARACTERSETutf8;SETCOLLATION_CONNECTION='utf8_general_ci';2.創建使用特定字符集和排序規則的數據庫:CREATEDATABASEexample_dbCHARACTERSETutf8COLLATEutf8_general_ci;3.創建表時指定字符集和排序規則:CREATETABLEexample_table(idINT

MySQL中重命名數據庫需要通過間接方法實現。步驟如下:1.創建新數據庫;2.使用mysqldump導出舊數據庫;3.將數據導入新數據庫;4.刪除舊數據庫。

我們需要Composer因為它能有效管理PHP項目的依賴,避免版本衝突和手動管理庫的麻煩。 Composer通過composer.json聲明依賴,並使用composer.lock確保版本一致,簡化了依賴管理過程,提高了項目穩定性和開發效率。

Java適合開發跨服務器web應用。 1)Java的“一次編寫,到處運行”哲學使其代碼可在任何支持JVM的平台上運行。 2)Java擁有豐富的生態系統,包括Spring和Hibernate等工具,簡化開發過程。 3)Java在性能和安全性方面表現出色,提供高效的內存管理和強大的安全保障。

JavaplaysigantroleiniotduetoitsplatFormentence.1)itallowscodeTobewrittenOnCeandrunonVariousDevices.2)Java'secosystemprovidesuseusefidesusefidesulylibrariesforiot.3)
