选择显示字体大小

php&java(三)


   例子二:通过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(&#36;xml,&#36;xsl) {

// create a xsltprocessorfactory object. xsltprocessorfactory is a java
// class which manufactures the processor for performing transformations.
&#36;xsltprocessorfactory = new java(&quot;org.apache.xalan.xslt.xsltprocessorfactory&quot;);

// use the xsltprocessorfactory method getprocessor() to create a
// new xsltprocessor object.
&#36;xsltprocessor = &#36;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 &#39;system identifier&#39; (uri) which
// can be an url or filename. if the system identifier is an url, it
// must be fully resolved.
&#36;xmlid = new java(&quot;org.apache.xalan.xslt.xsltinputsource&quot;, &#36;xml);
&#36;stylesheetid = new java(&quot;org.apache.xalan.xslt.xsltinputsource&quot;, &#36;xsl);

// create a stringwriter object for the output.
&#36;stringwriter = new java(&quot;java.io.stringwriter&quot;);

// create a resulttarget object for the output with the xsltresulttarget
// class. parameter of xsltresulttarget is (in this case) a &#39;character
// stream&#39;, which is the stringwriter object.
&#36;resulttarget = new java(&quot;org.apache.xalan.xslt.xsltresulttarget&quot;, &#36;stringwriter);

// process input with the xsltprocessors&#39; method process(). this
// method uses the xsl stylesheet to transform the xml input, placing
// the result in the result target.
&#36;xsltprocessor->process(&#36;xmlid,&#36;stylesheetid,&#36;resulttarget);

// use the stringwriters&#39; method tostring() to
// return the buffer&#39;s current value as a string to get the
// transformed result.
&#36;result = &#36;stringwriter->tostring();
&#36;stringwriter->close();
return(&#36;result);
}

?>

函数定义好后,我们就可以调用它了,在下面的例程中,变量&#36;xml指向一个url字符串,&#36;xsl也是如此。这个例子将显示5个最新的phpbuilder.com文章标题。

<?php

&#36;xml = &quot;http://www.phpbuilder.com/rss_feed.php?type=articles&limit=5&quot;;
&#36;xsl = &quot;http://www.soeterbroek.com/code/xml/rss_html.xsl&quot;;
&#36;out = xslt_transform(&#36;xml,&#36;xsl);
echo &#36;out;

?>

如果你在本地机上运行程序,必须确保你的函数参数指向正确的文件名。

<?php

&#36;xml = &quot;/web/htdocs/xml_java/rss_feed.xml&quot;;
&#36;xsl = &quot;/web/htdocs/xml_java/rss_html.xsl&quot;;
&#36;out = xslt_transform(&#36;xml,&#36;xsl);
echo &#36;out;

?>

虽然这种效果我们可以通过其它方法实现,或许那些方法更好,但这个例子能让你对php调用java类有一个更好的了解。


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons