php&java(三)
程序员文章站
2022-03-14 09:50:01
例子二:通过xalan 1.2,使用xslt转换xml 做为第二个例子,我们使用了xalan-java的xslt引擎,这个引擎来自于apache的xml项目,使用这个程序,...
例子二:通过xalan 1.2,使用xslt转换xml
做为第二个例子,我们使用了xalan-java的xslt引擎,这个引擎来自于apache的xml项目,使用这个程序,我们能够使用xsl转换xml源文件。这将极大的方便我们处理文档和进行内容管理。
开始之前,我们需要将xerces.jar 和 xalan.jar文件放入java.class.path目录下(这两个文件包含在xalan-java 1.2 中,可以从xml.apache.org处下载)。
php程序如下:
函数xslt_transform()以xml和xsl文件为参数,形式可为文件名(如:foo.xml)或url(如: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);
}
?>
函数定义好后,我们就可以调用它了,在下面的例程中,变量$xml指向一个url字符串,$xsl也是如此。这个例子将显示5个最新的phpbuilder.com文章标题。
<?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;
?>
如果你在本地机上运行程序,必须确保你的函数参数指向正确的文件名。
<?php
$xml = "/web/htdocs/xml_java/rss_feed.xml";
$xsl = "/web/htdocs/xml_java/rss_html.xsl";
$out = xslt_transform($xml,$xsl);
echo $out;
?>
虽然这种效果我们可以通过其它方法实现,或许那些方法更好,但这个例子能让你对php调用java类有一个更好的了解。
教程结束了,希望你能够从这篇教程中学到点东西,以下是一些你用得到的链接:
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
做为第二个例子,我们使用了xalan-java的xslt引擎,这个引擎来自于apache的xml项目,使用这个程序,我们能够使用xsl转换xml源文件。这将极大的方便我们处理文档和进行内容管理。
开始之前,我们需要将xerces.jar 和 xalan.jar文件放入java.class.path目录下(这两个文件包含在xalan-java 1.2 中,可以从xml.apache.org处下载)。
php程序如下:
函数xslt_transform()以xml和xsl文件为参数,形式可为文件名(如:foo.xml)或url(如: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);
}
?>
函数定义好后,我们就可以调用它了,在下面的例程中,变量$xml指向一个url字符串,$xsl也是如此。这个例子将显示5个最新的phpbuilder.com文章标题。
<?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;
?>
如果你在本地机上运行程序,必须确保你的函数参数指向正确的文件名。
<?php
$xml = "/web/htdocs/xml_java/rss_feed.xml";
$xsl = "/web/htdocs/xml_java/rss_html.xsl";
$out = xslt_transform($xml,$xsl);
echo $out;
?>
虽然这种效果我们可以通过其它方法实现,或许那些方法更好,但这个例子能让你对php调用java类有一个更好的了解。
教程结束了,希望你能够从这篇教程中学到点东西,以下是一些你用得到的链接:
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
上一篇: SpringCloud
下一篇: php&java(一)