当前位置:编程学习 > JAVA >>

求助!!怎么在生成xml文件时加上<?xml-stylesheet type="text/xsl" href="abc.xsl"?>

public class Dbtoxml {
private static final DocumentBuilderFactory DocumentBuilderFactory = null;


public static void main(String[] args)
{

String url="jdbc:odbc:jhd";
String user="";
String password="";
Connection conn;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
conn=DriverManager.getConnection(url, user, password);
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery("select * from notifications");
if(rs!=null)
{
writeXml(rs);


}else{System.out.println("no data");}

}catch(Exception e){e.printStackTrace();}
}

private static  void writeXml(ResultSet rs) {
// TODO Auto-generated method stub
ResultSet rst=rs;
boolean flag = false;
    String s="WebRoot/notifications.xml";

try{
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();

DocumentBuilder builder=factory.newDocumentBuilder();
Document document=builder.newDocument();

PrintWriter pWriter=new PrintWriter(new java.io.FileOutputStream(s));
       XMLSerializer serl=new XMLSerializer(pWriter,new OutputFormat("xml","gb2312",true));//这里的参数(TRUE)表示格式化为自动换行
            serl.processingInstruction("xml:stylesheet","type=\"text/xsl\" href=\"abc.xsl\"");
            serl.serialize(document);

           pWriter.close();

Element rootElement=document.createElement("notifications");
   document.appendChild(rootElement);
while(rst.next()){
Element selement=document.createElement("notification");
selement.appendChild(document.createTextNode(rst.getString("notification")));
rootElement.appendChild(selement);

}

flag=SaveXmlFile(document,s); 

if(flag){System.out.println("succes");}
else {System.out.println("fail");}
}catch(Exception e){e.printStackTrace();}

}

private static boolean SaveXmlFile(Document document, String string) {
// TODO Auto-generated method stub
boolean flag=true;
try{

TransformerFactory tfactory=TransformerFactory.newInstance();

Transformer trans=tfactory.newTransformer();

DOMSource source=new DOMSource(document);

StreamResult result=new StreamResult(string);

trans.transform(source, result);

}
catch(Exception e){
flag=false;
e.printStackTrace();}



return flag;


}
--------------------编程问答-------------------- color=#FF0000]等待着好心人的回复。。。[[/color] --------------------编程问答-------------------- 好心人在哪里???
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,