Php & amp; java (3 ). Example 2: Use Xalan1.2 to convert XML using XSLT as the second example. We use the Xalan-java XSLT engine, which comes from the apache xml project and uses this program, example 2: Use Xalan 1.2 and XSLT to convert XML
As the second example, we use the Xalan-java XSLT engine, which comes from the apache xml project. using this program, we can use XSL to convert XML source files. This greatly facilitates document processing and content management.
Before starting, we need to put the xerces. jar and xalan. jar files under 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 effect_transform () function takes XML and XSL files as parameters and can be a file name (for example, foo. xml) or a URL (for example, http: // localhost/foo. xml ).
Function effect_transform ($ xml, $ xsl ){
// Create a jsontprocessorfactory object. jsontprocessorfactory is a Java
// Class which manufactures the processor for processing Ming transformations.
$ Export tprocessorfactory = new java ("org. apache. xalan. xslt. XSLTProcessorFactory ");
// Use the specified tprocessorfactory method getProcessor () to create
// New jsontprocessor object.
$ Effectprocessor = $ effectprocessorfactory-> getProcessor ();
// Use your tinputsource objects to provide input to the specified tprocessor
// Process () method for transformation. Create objects for both
// Xml source as well as the XSL input source. Parameter
// Using tinputsource 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. small tinputsource", $ xml );
$ StylesheetID = new java ("org. apache. xalan. xslt. small tinputsource", $ xsl );
// Create a stringWriter object for the output.
$ StringWriter = new java ("java. io. StringWriter ");
// Create a ResultTarget object for the output with the specified tresulttarget
// Class. Parameter of Parameter tresulttarget is (in this case) a 'character
// Stream ', which is the stringWriter object.
$ ResultTarget = new java ("org. apache. xalan. xslt. parse tresulttarget", $ stringWriter );
// Process input with the specified tprocessors 'method process (). This
// Method uses the XSL stylesheet to transform the XML input, placing
// The result in the result target.
$ Effectprocessor-> process ($ xmlID, $ stylesheetID, $ resultTarget );
// Use the stringWriters 'method toString ()
// Return the buffer's current value as a string to get
// 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 is $ xsl. This example shows the title of five latest phpbuilder.com articles.
$ Xml = "http://www.phpbuilder.com/rss_feed.php? Type = articles & limit = 5 ";
$ Xsl = "http://www.soeterbroek.com/code/xml/rss_html.xsl ";
$ Out = effect_transform ($ xml, $ xsl );
Echo $ out;
?>
If you run a program on a local machine, make sure that your function parameter points to the correct file name.
$ Xml = "/web/htdocs/xml_java/rss_feed.xml ";
$ Xsl = "/web/htdocs/xml_java/rss_html.xsl ";
$ Out = effect_transform ($ xml, $ xsl );
Echo $ out;
?>
Although this effect can be achieved through other methods, maybe those methods are better, this example gives 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
Example 1.2: use XSLT to convert XML as the second example. We use the Xalan-java XSLT engine. this engine comes from the apache xml project. use this program. we...