一个写xml的问题,高手帮忙啊! 

04-05-05 ldy604
最近碰到这样一个问题

我想将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>

猜你喜欢