php&java (3)_PHP tutorial

WBOY
Release: 2016-07-21 16:04:37
Original
705 people have browsed it

Example 2: Using XSLT to transform XML via Xalan 1.2

As the second example, we use the XSLT engine of XSL transforms XML source files. This will greatly facilitate our document processing and content management.

Before we start, we need to put the xerces.jar and xalan.jar files into the java.class.path directory (these two files are included in Xalan-Java 1.2 and can be downloaded from xml.apache.org download).
The PHP program is as follows:
The function xslt_transform() takes XML and XSL files as parameters, and the format can be a file name (such as: foo.xml) or a URL (such as: http://localhost/foo.xml).


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);
}

?>

function Once defined, we can call it. In the following routine, the variable $xml points to a URL string, as does $xsl. This example will display the 5 latest phpbuilder.com article titles.


$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 running the program on the local machine, you must ensure that your function parameters point to the correct file name.


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

?>

Although we can achieve this effect through other methods, maybe Those methods are better, but this example will give you a better understanding of PHP calling JAVA classes.

The tutorial is over. I hope you can learn something from this tutorial. Here are some links you can use:
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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/315894.htmlTechArticleExample 2: Using XSLT to transform XML via Xalan 1.2 As the second example, we used Xalan-java XSLT engine, this engine comes from the APACHE XML project, using this program, we...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!