XSL 指扩展样式表语言(EXtensible Stylesheet Language),前面一篇博客介绍了使用XSL即可直接将XML输出为HTML片段被浏览器解析,但是这样在web应用中浏览器的解析量就比较大,会显得响应很慢,Java API提供了可以将XSL和XML文件直接解析为HTML文件的工具,这样可以后台解析,服务器端生成HTML传送至前台,提高响应体验。
创建一个XML文件
Empire BurlesqueBob Dylan
USA
Columbia
10.90
1985
Hide your heartBonnie Tyler
UK
CBS Records
9.90
1988
Greatest HitsDolly Parton
USA
RCA
9.90
1982
Still got the bluesGary Moore
UK
Virgin records
10.20
1990
ErosEros Ramazzotti
EU
BMG
9.90
1997
One night onlyBee Gees
UK
Polydor
10.90
1998
Sylvias MotherDr.Hook
UK
CBS
8.10
1973
Maggie MayRod Stewart
UK
Pickwick
8.50
1990
RomanzaAndrea Bocelli
EU
Polydor
10.80
1996
When a man loves a womanPercy Sledge
USA
Atlantic
8.70
1987
Black angelSavage Rose
EU
Mega
10.90
1995
1999 Grammy NomineesMany
USA
Grammy
10.20
1999
For the good timesKenny Rogers
UK
Mucik Master
8.70
1995
Big Willie styleWill Smith
USA
Columbia
9.90
1997
Tupelo HoneyVan Morrison
UK
Polydor
8.20
1971
SoulsvilleJorn Hoel
Norway
WEA
7.90
1996
The very best ofCat Stevens
UK
Island
8.90
1990
StopSam Brown
UK
A and M
8.90
1988
Bridge of SpiesT`Pau
UK
Siren
7.90
1987
Private DancerTina Turner
UK
Capitol
8.90
1983
Midt om nattenKim Larsen
EU
Medley
7.80
1983
Pavarotti Gala ConcertLuciano Pavarotti
UK
DECCA
9.90
1991
The dock of the bayOtis Redding
USA
Atlantic
7.90
1987
Picture bookSimply Red
EU
Elektra
7.20
1985
RedThe Communards
UK
London
7.80
1987
Unchain my heartJoe Cocker
USA
EMI
8.20
1987
创建一个XSL文件
XML2HTMLMy CD Collection
Title | Artist | country | company | price | year |
---|---|---|---|---|---|
Java解析输出HTML
JDK开始提供了javax.xml.transform.*这样一个xml解析的包,非常方便。
package test;
import java.io.File;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
/**
* The type Xml 2 html.
*/
public class XMLXSL2HTML {
public static void main(String[] args) {
String xmlFilePath = "src/test/test.xml";
String xslFilePath = "src/test/test.xsl";
String HtmlFileOutPath = "src/test/test.html";
transXML2HTML(xmlFilePath,xslFilePath,HtmlFileOutPath);
}
/**
* Trans xml to html.
*
* @param xmlFilePath the xml file path
* @param xslFilePath the xsl file path
* @param HtmlFileOutPath the html file out path
*/
public static void transXML2HTML(String xmlFilePath,String xslFilePath,String HtmlFileOutPath) {
try {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Source xslSource = new StreamSource(xslFilePath);
Transformer transformer = transformerFactory.newTransformer(xslSource);
File xmlFile = new File(xmlFilePath);
File htmlFile = new File(HtmlFileOutPath);
Source source = new StreamSource(xmlFile);
Result result = new StreamResult(htmlFile);
transformer.transform(source, result);
System.out.println("文件输出完成:"+htmlFile.getAbsolutePath());
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
}