例子二:透過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

DependencyInjection(DI)inPHPenhancescodeflexibilityandtestabilitybydecouplingdependencycreationfromusage.ToimplementDIeffectively:1)UseDIcontainersjudiciouslytoavoidover-engineering.2)Avoidconstructoroverloadbylimitingdependenciestothreeorfour.3)Adhe

到Improveyourphpwebsite的實力,UsEthestertate:1)emplastOpCodeCachingWithOpcachetCachetOspeedUpScriptInterpretation.2)優化的atabasequesquesquesquelies berselectingOnlynlynnellynnessaryfields.3)usecachingsystemssslikeremememememcachedisemcachedtoredtoredtoredsatabaseloadch.4)

是的,ItispossibletosendMassemailswithp.1)uselibrarieslikeLikePhpMailerorSwiftMailerForeffitedEmailsending.2)enasledeLaysBetenemailstoavoidSpamflagssspamflags.3))

DependencyInjection(DI)inPHPisadesignpatternthatachievesInversionofControl(IoC)byallowingdependenciestobeinjectedintoclasses,enhancingmodularity,testability,andflexibility.DIdecouplesclassesfromspecificimplementations,makingcodemoremanageableandadapt

使用PHP發送電子郵件的最佳方法包括:1.使用PHP的mail()函數進行基本發送;2.使用PHPMailer庫發送更複雜的HTML郵件;3.使用SendGrid等事務性郵件服務提高可靠性和分析能力。通過這些方法,可以確保郵件不僅到達收件箱,還能吸引收件人。

計算PHP多維數組的元素總數可以使用遞歸或迭代方法。 1.遞歸方法通過遍歷數組並遞歸處理嵌套數組來計數。 2.迭代方法使用棧來模擬遞歸,避免深度問題。 3.array_walk_recursive函數也能實現,但需手動計數。

在PHP中,do-while循環的特點是保證循環體至少執行一次,然後再根據條件決定是否繼續循環。 1)它在條件檢查之前執行循環體,適合需要確保操作至少執行一次的場景,如用戶輸入驗證和菜單系統。 2)然而,do-while循環的語法可能導致新手困惑,且可能增加不必要的性能開銷。

在PHP中高效地哈希字符串可以使用以下方法:1.使用md5函數進行快速哈希,但不適合密碼存儲。 2.使用sha256函數提高安全性。 3.使用password_hash函數處理密碼,提供最高安全性和便捷性。


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

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

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

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。