從資料庫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檔案效果如下: