Home > Backend Development > PHP Tutorial > justin bieber baby mv php&java(三)

justin bieber baby mv php&java(三)

WBOY
Release: 2016-07-29 08:33:39
Original
1350 people have browsed it

Example 2: Using XSLT to transform XML via . 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. apache.xalan.xslt.XSLTProcessorFactory");
// Use the XSLTProcessorFactory method getProcessor() to create a
// new XSLTProcessor object.
$XSLTProcessor = $XSLTProcessorFactory->getProcessor();
// Use XSLTInput Source 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);
}
?>
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 make sure 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 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

The above introduces justin bieber baby mv php&java (3), including the content of justin bieber baby mv. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
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