从数据库Mysql读取数据转化为xml文档的Java类
IDE:Myeclipse6.0,数据库MySql(MySql开发与管理工具:Navicat8.0for MySQL);
一、建立数据文件
二、建立连接数据库的类DBConnection.java
package com.ychk;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBConnection
{
StringDriver="com.mysql.jdbc.Driver";
StringUrl="jdbc:mysql://localhost:3306/xml";//xml为数据源
String Name="Mysql用户名";
StringPwd="Mysql密码";
public ConnectiongetConnection() throws SQLException, ClassNotFoundException
{
Connectionconn=null;
try
{
Class.forName(Driver);
conn=DriverManager.getConnection(Url,Name,Pwd);
}catch(Exception e)
{
e.printStackTrace();
}
returnconn;
}
public ResultSet sqlUser()throws SQLException, ClassNotFoundException{
getConnection();
Stringsql="select * from testxml";
ResultSetrs=null;
PreparedStatementps=null;
Connectionconn=null;
conn=DriverManager.getConnection(Url,Name,Pwd);
ps =conn.prepareStatement(sql);
rs =ps.executeQuery(sql);
returnrs;
}
}
三、建立创建xml文件的Java类TestXml.java
package com.ychk;
import java.io.File;
import java.io.FileOutputStream;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class TestXml
{
public staticvoid main(String[] args)
{
Listresult = new ArrayList();
Stringline[]= null;
try{
DBConnectioncon=new DBConnection();
ResultSetrs=con.sqlUser();
while(rs.next())
{
line= new String[2];
line[0]=rs.getString(1);
line[1]=rs.getString(2);
result.add(line);
}
DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance();
DocumentBuilderbuilder=factory.newDocumentBuilder();
Documentdocument=builder.newDocument();//Creat xml document
document.setXmlVersion("1.0");
Elementroot=document.createElement_x("chart");//rootname of xmldocument
document.appendChild(root);//creatroot of xml document
for(intk=1;k<=result.size();k++)
{
root.appendChild(document.createElement_x("set"));//elementnameof xml document
}
NodeListnodeList=document.getElementsByTagName_r("set");
intsize=nodeList.getLength();
for(intk=0;k<size;k++)
{
Nodenode=nodeList.item(k);
if(node.getNodeType()==Node.ELEMENT_NODE)
{
String[]temp = (String[])result.get(k);
ElementelementNode=(Element)node;
elementNode.appendChild(document.createElement_x("label")).appendChild(document.createTextNode(temp[0]));
elementNode.appendChild(document.createElement_x("value")).appendChild(document.createTextNode(temp[1]));
}
}
TransformerFactorytransFactory=TransformerFactory.newInstance();
Transformertransformer=transFactory.newTransformer();
DOMSourcedomSource=new DOMSource(document);
Filefile=new File("testxml.xml");//save xml document as url
FileOutputStreamout=new FileOutputStream(file);
StreamResultxmlResult=new StreamResult(out);
transformer.transform(domSource,xmlResult);
}
catch(Exceptione)
{
System.out.println(e);
}
}
}
四、用浏览器打开xml文件效果如下:
五、如何实现如下效果:--正在改进,请各位指教!谢谢!