php
时间:2007-02-17 来源:PHP爱好者
例子二:通过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类有一个更好的了解。
php爱好者站 http://www.phpfans.net 为phper提供一切资讯.
做为第二个例子,我们使用了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类有一个更好的了解。
php爱好者站 http://www.phpfans.net 为phper提供一切资讯.
相关阅读 更多 +