Home >Backend Development >PHP Tutorial >php&java (3)
Example 2: Using XSLT to convert xml through Source File. 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).
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. "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. ("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);
}
?>
After the function is 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 can give you a better understanding of PHP calling JAVA classes .
The above is the content of php&java (3). For more related articles, please pay attention to the PHP Chinese website (www.php.cn)!