以文本方式查看主题

-  W3CHINA.ORG讨论区 - 语义网·描述逻辑·本体·RDF·OWL  (http://bbs.xml.org.cn/index.asp)
--  『 DOM/SAX/XPath 』  (http://bbs.xml.org.cn/list.asp?boardid=11)
----  一个写xml的问题,高手帮忙啊!  (http://bbs.xml.org.cn/dispbbs.asp?boardid=11&rootid=&id=7217)


--  作者:ldy604
--  发布时间:5/5/2004 9:51:00 AM

--  一个写xml的问题,高手帮忙啊!
最近碰到这样一个问题
我想将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>


--  作者:likebbs
--  发布时间:5/5/2004 7:20:00 PM

--  
<?xml version="1.0" encoding="GB2312"?>
--  作者:xiaokelz
--  发布时间:8/5/2005 4:47:00 PM

--  
试了一下楼主给出的代码
xml开头一段
<?xml version="1.0" encoding="UTF-8"?>
好像是默认写上的,要怎么改才能在代码中把 utf-8 改成 gb2312
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
31.250ms