PHP & Java(3) (轉)
Example 2: Using Xalan 1.2 to trano with T
As another example of accessing s in , we will use the Xalan-java XSLT engine from the XML project. With this application, we can transform XML files using instructions in a XSL file. This allows for a great number of interesting scenariin the field of document processing and content management.
To get started, we need to place both xerces.jar and xalan.jar files (included in Xalan-Java version 1.2 from xml.apache.org) in your java.class.path, as defined in your php.ini file.
The function xslt_transform() takes XML and XSL files as parameters and returns the transformed output in a string. XML and XSL parameters can be filenames (eg. foo.xml) or fully resolved URI's (eg. ).
function xslt_transform($xml,$xsl) {
// Create a XSLTProcessorFactory object. XSLTProcessorfactory is a Java
// class which manufactures the processor for perfong 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 prov 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);
}
?> ?php>
Then, you can call this function as shown in the example below. $xml contains a string with the fully resolved URL of XML file. $xsl contains string with a XSL stylesheet URL containing rules for conversion to generic HTML. $out will contain a string with output, as a result of calling xslt_transform described above. This example parses a XML newsfeed containing the 5 latest articles on phpbuilder.com. You are encouraged to also try other XML feeds and/or XSl stylesheets.
$xml = "";
$xsl = "";
$out = xslt_transform($xml,$xsl);
echo $out;
?> ?php>
If you are processing local files, make sure you use the full path name to pass to the Java class.
$xml = "//htdocs/xml_java/rss_feed.xml";
$xsl = "/web/htdocs/xml_java/rss_html.xsl";
$out = xslt_transform($xml,$xsl);
echo $out;
?> ?php>
Although there are a number of other ways in PHP to achieve the same results, the above example gives you a good idea of the possibilities of accessing Java objects in PHP.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-987826/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- PHP & Java(4) (轉)PHPJava
- PHP & Java(2) (轉)PHPJava
- PHP & Java(1) (轉)PHPJava
- 應該要PHP轉Java了。。。PHPJava
- MVC模式的PHP實現(3) (轉)MVC模式PHP
- 求 PHP 轉 java 的相關教程PHPJava
- php將arm轉換成mp3PHP
- PHP,21世紀的霸主(之3) (轉)PHP
- PHP 轉 JAVA 篇 小插曲(環境篇)PHPJava
- PHP呼叫java類的兩種方法(轉)PHPJava
- XML的JAVA 解析(一)(3) (轉)XMLJava
- Java Servlet和JSP教程(3)(轉)JavaServletJS
- 在C、JAVA、PHP中操作postgreSql資料庫 (轉)JavaPHPSQL資料庫
- Redhat linux 9下讓PHP支援java類(轉)RedhatLinuxPHPJava
- Java2HTML改造手記(3) (轉)JavaHTML
- PHP轉Java有沒有好的學習路線PHPJava
- 3. php資料型別、資料型別轉換PHP資料型別
- 用linux下的php3發E-MAIL (轉)LinuxPHPAI
- PHP物件程式設計實現3D餅圖 (轉)PHP物件程式設計3D
- App.php-3APPPHP
- PHP學習(3)PHP
- Java學習從入門到精通(3)(轉)Java
- PHP and COM (轉)PHP
- ssl + apache? 我的是RH7.2(3) ,APACHE,PHP和MYSQL(轉)ApachePHPMySql
- PHP中呼叫JAVA,PHPJava
- Validator.php-3PHP
- 3. PHP 引用解惑PHP
- java時間戳和PHP時間戳的轉換phptime()Java時間戳PHP
- PHP安全配置(轉)PHP
- php.ini (轉)PHP
- PHP:路在何方? (轉)PHP
- PHP 迭代器 (轉)PHP
- PHP安全(2) (轉)PHP
- Apache+php3 在WIN98下的安裝與配置 (轉)ApachePHP
- pws+php3在win98下的安裝與配置 (轉)PHP
- 我學習使用java的一點體會(3) (轉)Java
- JML起步---使用JML 改進你的Java程式(3) (轉)Java
- PHP學習3——陣列PHP陣列