一個寫xml的問題,高手幫忙啊!

ldy604發表於2004-05-05
最近碰到這樣一個問題
我想將SQLSERVER裡的記錄取出來生成XML檔案,我的資料是這樣的
create table userinfo
(
username varchar(20),
[password] varchar(20),
email varchar(50),
homepage varchar(30),
regtime datetime,
[money] integer
)
select * from userinfo

insert into userinfo values('Bill Clinton','343434','bill@usa.org','www.whitehouse.org','2002-06-18 17:08:01.0',350)
insert into userinfo values('Tony Blair','2323323','blair@everywhere.com','www.everywhere.com','2002-06-18 17:07:01.0',250);
我的JAVA程式是下面這樣的:
import java.sql.*;
import javax.xml.parsers.*;
import org.apache.crimson.tree.*;
import org.w3c.dom.*;
import java.io.*;

public class Userinfo{
static ResultSet results;
static Connection con;
static String username,password,email,homepage,regtime,money;
static String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
public static void main(String args[]){
Document doc;
ProcessingInstruction pi;
Element people=null;
Element person=null;
Element name=null;
Element passwd=null;
Element mail=null;
Element homepg=null;
Element reg=null;
Element mny=null;

try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
con=DriverManager.getConnection(url,"sa","");
Statement stmt=con.createStatement();
results=stmt.executeQuery("select * from userinfo");


DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=dbf.newDocumentBuilder();
doc=builder.newDocument();


pi=doc.createProcessingInstruction("xml-stylesheet","type=\"text/xsl\" href=\"userinfo.xsl\"");

doc.appendChild(pi);

people=doc.createElement("PEOPLE");
while(results.next()){
person=doc.createElement("PERSON");
people.appendChild(person);

name=doc.createElement("USERNAME");
name.appendChild(doc.createTextNode(results.getString("username")));
person.appendChild(name);

passwd=doc.createElement("PASSWORD");
passwd.appendChild(doc.createTextNode(results.getString("password")));
person.appendChild(passwd);

mail=doc.createElement("EMAIL");
mail.appendChild(doc.createTextNode(results.getString("EMAIL")));
person.appendChild(mail);

homepg=doc.createElement("HOMEPAGE");
homepg.appendChild(doc.createTextNode(results.getString("homepage")));
person.appendChild(homepg);

reg=doc.createElement("REGTIME");
reg.appendChild(doc.createTextNode(results.getString("REGTIME")));
person.appendChild(reg);

mny=doc.createElement("MONEY");
mny.appendChild(doc.createTextNode(results.getString("money")));
person.appendChild(mny);


}
doc.appendChild(people);

((XmlDocument)doc).write(new FileOutputStream(new File("userinfo.xml")));

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

}
這樣的話結果是正確的
但是我將SQL更新,如下
update userinfo set username='明明明' where [password]='343434'
然後生成的XML檔案就是不能表示出中文,
請教各位大嚇
如何才能支援中文????????
下面是生成的XML檔案:
<?xml version="1.0" encoding="UTF-8"?>

<?xml-stylesheet type="text/xsl" href="userinfo.xsl"?>
<PEOPLE>
<PERSON>
<USERNAME>寰烽?/USERNAME>
<PASSWORD>343434</PASSWORD>
<EMAIL>bill@usa.org</EMAIL>
<HOMEPAGE>www.whitehouse.org</HOMEPAGE>
<REGTIME>2002-06-18 17:08:01.0</REGTIME>
<MONEY>350</MONEY>
</PERSON>
<PERSON>
<USERNAME>Tony Blair</USERNAME>
<PASSWORD>2323323</PASSWORD>
<EMAIL>blair@everywhere.com</EMAIL>
<HOMEPAGE>www.everywhere.com</HOMEPAGE>
<REGTIME>2002-06-18 17:07:01.0</REGTIME>
<MONEY>250</MONEY>
</PERSON>
</PEOPLE>

相關文章