try
{
TransformerFactory tf = TransformerFactory.newInstance();
Source xml = new DOMSource(doc);
Source xsl = new StreamSource(xslfile);
Result html = new StreamResult(htmlfile);
Transformer tran = tf.newTransformer(xsl);
tran.transform(xml,html);
}
catch (TransformerConfigurationException e)
{
}
catch (TransformerException e)
{
}
{
TransformerFactory tf = TransformerFactory.newInstance();
Source xml = new DOMSource(doc);
Source xsl = new StreamSource(xslfile);
Result html = new StreamResult(htmlfile);
Transformer tran = tf.newTransformer(xsl);
tran.transform(xml,html);
}
catch (TransformerConfigurationException e)
{
}
catch (TransformerException e)
{
}
解决方案 »
- 利用Spring+iBATIS,只能查出表中字段与类属性相同的字段,其他字段查出为空null和0???
- jQuery+struts2 json表单校验问题
- ssh框架怎么处理操作后的提示比较合适呢?
- struts2异常捕获
- 关于struts1.1中配置数据源的问题
- 关于显示文章中包含html标签的处理
- webwork根strtus差不多嘛!!!!!!!!!!
- 早早的醒来,突然想起一个关于ejb里的一些细节问题,发帐号讨教
- tomcat 难题求助
- S2SH项目执行过程
- 请教关于在sun的j2sdkee中所带的cloudscape数据库怎么使用。
- 请问大家,除了JB有没有占用资源比较小的工具开发J2EE啊?
import java.util.Locale;
import java.net.*;
import org.w3c.dom.*;
import oracle.xml.parser.v2.*;public class XSLTSample {
public static void main (String args[]) throws Exception {
try {
DOMParser parser = new DOMParser();
URL xmlURL = createURL("sample1.xml");
URL xslURL = createURL("sample1.xsl");
parser.parse(xmlURL);
XMLDocument xmldoc = parser.getDocument();
parser.parse(xslURL);
XMLDocument xsldoc = parser.getDocument(); XSLStylesheet stylesheet = new XSLStylesheet(xsldoc, xslURL); XSLProcessor processor = new XSLProcessor(); processor.setErrorStream(System.err); processor.processXSL(stylesheet, xmldoc, System.out);
}
catch (Exception e) {
e.printStackTrace();
}
}
static URL createURL(String fileName) {
URL url = null;
try {
url = new URL(fileName);
}
catch (MalformedURLException ex) {
File f = new File(fileName);
try {
String path = f.getAbsolutePath();
String fs = System.getProperty("file.separator");
if (fs.length() == 1) {
char sep = fs.charAt(0);
if (sep != '/')
path = path.replace(sep, '/');
if (path.charAt(0) != '/')
path = '/' + path;
}
path = "file://" + path;
url = new URL(path);
}
catch (MalformedURLException e) {
System.out.println("Cannot create url for: " + fileName);
System.exit(0);
}
}
return url;
}
}
<?xml-stylesheet href="scores.xsl" type="text/xsl"?>
<?xml-stylesheet href="scores_wap.xsl" type="text/xsl" media="phone"?>
<?xml-stylesheet href="scores_print.xsl" type="text/xsl" media="print"?>
<results showName="New Port Richey Open" location="New Port Richey, FL" showDate="7/31/01">
...
</results>
import java.io.File;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.Source;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.Element;public class ChooseStyleSheet {
public static void main (String args[]) {
String rootName = null;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse("scores.xml");
Element root = doc.getDocumentElement();
rootName = root.getNodeName();
} catch (Exception e) {
e.printStackTrace();
} try { TransformerFactory transformerFactory =
TransformerFactory.newInstance(); StreamSource source = new StreamSource("scores.xml");
StreamResult result = new StreamResult("result.xml"); String styleName = null;
if (rootName.equals("results")){
styleName = "scores.xsl";
} else if (rootName.equals("entries")){
styleName = "entries.xsl";
} Transformer transformer = null;
if (styleName == null) { transformer = transformerFactory.newTransformer(); } else { StreamSource style = new StreamSource(styleName);
transformer = transformerFactory.newTransformer(style); } transformer.transform(source, result); } catch (Exception e) {
e.printStackTrace();
}
}
}
http://expert.csdn.net/Expert/TopicView1.asp?id=1562645
public static String transXML2HTML(String xml,String xslfile){
StringWriter sw=null;
if(xml.equals("")||xml==null) return "<H1>无该满足条件的记录</H1>"; try {
String strOut = new String(xml.getBytes("gbk"),"iso8859-1");
StringBufferInputStream sbis=new StringBufferInputStream(strOut); StreamSource sXML=new StreamSource();
sXML.setInputStream(sbis);
File file=new File(xslfile);
StreamSource sSTL=new StreamSource(file); TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(sSTL); sw=new StringWriter();
StreamResult sr=new StreamResult(sw);
transformer.setOutputProperty("method","html");
transformer.transform(sXML,sr);
}
catch (Exception e) {
System.out.println("TransXML2HTML error:"+e);
}
return sw.getBuffer().toString();
}