用户登录  |  用户注册
首 页商业源码原创产品编程论坛
当前位置:PB创新网文章中心编程技巧编程其他

PHP & Java(3)

减小字体 增大字体 作者:佚名  来源:本站整理  发布时间:2009-03-16 19:41:49

Example 2: Using Xalan 1.2 to transform XML with XSLT

As another example of accessing Java objects in PHP, we will use the Xalan-java XSLT engine from the Apache XML project. With this application, we can transform XML source files using instructions in a XSL file. This allows for a great number of interesting scenarios in 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. 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);
}

?>

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.

<?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;

?>

If you are processing local files, make sure you use the full path name to pass to the Java class.

<?php

$xml    
"/web/htdocs/xml_java/rss_feed.xml";
$xsl    "/web/htdocs/xml_java/rss_html.xsl";
$out xslt_transform($xml,$xsl);
echo 
$out;

?>

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.


Tags:

作者:佚名

文章评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
PB创新网ourmis.com】Copyright © 2000-2009 . All Rights Reserved .
页面执行时间:25,203.13000 毫秒
Email:ourmis@126.com QQ:2322888 蜀ICP备05006790号