package myProject;//创建包,将功能相似或相关联的类放入其中方便使用/**
* 导入需要使用的工具包
*/
import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.dom.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import org.xml.sax.helpers.*;
import org.xml.sax.*;
import com.sun.org.apache.xml.internal.serialize.*;/**
*class GenerateHtml根据XML 和XSLT文件生成HTML文件
*/
public class GenerateHtmlMethod
{
/**
*根据XML 和XSLT文件生成HTML文件并返回该HTML文件的源码,参数xml xslt分别保存XML 和XSLT文件的路径
*/
public String getHtml(String xml,String xslt){
String str,data="";
try{
//创建DocumentBuilderFactory对象
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//获得DOM解析器
DocumentBuilder builder = factory.newDocumentBuilder();
//解析XML并返回解析内容
Document document = builder.parse(new InputSource(new FileReader(xml)));
// 准备转换的格式
StreamSource xsltSource = new StreamSource(new FileInputStream(xslt));
//被转换的文件
Source source = new DOMSource(document);
// 创建一个样式对象工厂
TransformerFactory tf = TransformerFactory.newInstance();
//把XSLT读入一个样式对象
Templates transformation = tf.newTemplates(xsltSource);
// 根据样式对象创建一个转换对象
Transformer transformer = transformation.newTransformer(); //创建输出的对象
File file=new File("s.html");
FileOutputStream out=new FileOutputStream(file);
StreamResult result=new StreamResult(out);// 输出的结果集
transformer.transform(source, result); //完成转换
//将生成的html文件读入字符串data中
BufferedReader reader = new BufferedReader(new FileReader(file));
while(true){
str = reader.readLine();
if (str==null) break;
data += str;
}
reader.close();//关闭文件
}catch(FileNotFoundException e){System.out.println("Exception:"+e);}
catch(Exception e){System.out.println("Exception:"+e);}
return data;//返回html源代码字符串
}
public static void main(String args[])
{
GenerateHtmlMethod yang = new GenerateHtmlMethod();
yang.getHtml("1.xml","1.xslt");
}
}
* 导入需要使用的工具包
*/
import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.dom.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import org.xml.sax.helpers.*;
import org.xml.sax.*;
import com.sun.org.apache.xml.internal.serialize.*;/**
*class GenerateHtml根据XML 和XSLT文件生成HTML文件
*/
public class GenerateHtmlMethod
{
/**
*根据XML 和XSLT文件生成HTML文件并返回该HTML文件的源码,参数xml xslt分别保存XML 和XSLT文件的路径
*/
public String getHtml(String xml,String xslt){
String str,data="";
try{
//创建DocumentBuilderFactory对象
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//获得DOM解析器
DocumentBuilder builder = factory.newDocumentBuilder();
//解析XML并返回解析内容
Document document = builder.parse(new InputSource(new FileReader(xml)));
// 准备转换的格式
StreamSource xsltSource = new StreamSource(new FileInputStream(xslt));
//被转换的文件
Source source = new DOMSource(document);
// 创建一个样式对象工厂
TransformerFactory tf = TransformerFactory.newInstance();
//把XSLT读入一个样式对象
Templates transformation = tf.newTemplates(xsltSource);
// 根据样式对象创建一个转换对象
Transformer transformer = transformation.newTransformer(); //创建输出的对象
File file=new File("s.html");
FileOutputStream out=new FileOutputStream(file);
StreamResult result=new StreamResult(out);// 输出的结果集
transformer.transform(source, result); //完成转换
//将生成的html文件读入字符串data中
BufferedReader reader = new BufferedReader(new FileReader(file));
while(true){
str = reader.readLine();
if (str==null) break;
data += str;
}
reader.close();//关闭文件
}catch(FileNotFoundException e){System.out.println("Exception:"+e);}
catch(Exception e){System.out.println("Exception:"+e);}
return data;//返回html源代码字符串
}
public static void main(String args[])
{
GenerateHtmlMethod yang = new GenerateHtmlMethod();
yang.getHtml("1.xml","1.xslt");
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货