RDBMS数据库读取数据写进xml

import java.io.File;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;

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.NamedNodeMap;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

import org.w3c.dom.Text;

public class Append {

public static void main(String[] args) throws Exception {

DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();

DocumentBuilder builder=factory.newDocumentBuilder();

Document doc=builder.parse(“MyXml.xml”);

doc.normalize();

Element root=doc.getDocumentElement();

acessDB(doc,root);

if(doc!=null)

printNode(root);

TransformerFactory tf=TransformerFactory.newInstance();

Transformer transformer=tf.newTransformer();

DOMSource source=new DOMSource(doc);

        StreamResult result=new StreamResult(new File(“MyXml.xml”));

        transformer.transform(source, result);

}

public static void acessDB(Document doc,Element root) throws Exception{

Class.forName(“com.mysql.jdbc.Driver”);

String url = “jdbc:mysql://127.0.0.1/xml useUnicode=true&characterEncoding=utf-8”;

Connection conn=DriverManager.getConnection(url, “”, “”);

Statement stmt=conn.createStatement();

ResultSet rs=stmt.executeQuery(“select * from users”);

createXML(doc,root,rs);

}

public static void createXML(Document doc,Element root,ResultSet rs) throws Exception{

boolean flag=rs.next();

while(flag){

Element Users=doc.createElement(“Users”);

Element userid=doc.createElement(“userid”);

Text userid_text=doc.createTextNode(“”+rs.getInt(“userid”));

userid.appendChild(userid_text);

Element username=doc.createElement(“usrrname”);

Text username_text=doc.createTextNode(rs.getString(“username”));

username.appendChild(userid_text);

Element password=doc.createElement(“password”);

Text password_text=doc.createTextNode(rs.getString(“password”));

password.appendChild(password_text);

Users.appendChild(userid);

Users.appendChild(username);

Users.appendChild(password);

root.appendChild(Users);

flag=rs.next();

}

}

public static void printNode(Element element){

NodeList children=element.getChildNodes();

NamedNodeMap attr=element.getAttributes();

int r=children.getLength();

if(attr!=null){

System.out.print(“<“+element.getNodeName());

for(int j=0;j<attr.getLength();j++){

System.out.print(” “+attr.item(j).getNodeName()+”=”+attr.item(j).getNodeName()+” “);

}

System.out.println(“>”);

}else if(attr==null){

System.out.println(“<“+element.getNodeName()+”>”);

}

if(element.hasChildNodes()){

for(int k=0;k<r;k++){

if(children.item(k).getNodeType()==Node.ELEMENT_NODE)

printNode((Element)children.item(k));

else if(children.item(k).getNodeType()==Node.TEXT_NODE){

System.out.println(children.item(k).getNodeValue());

}

}

System.out.println(“</”+element.getNodeName()+”>”);

}

}

}需要注意的是:导入mysql-connect-java.jar包,

             新建的 xml文件在项目的根目录下,

             需要包含一个标签和头文件

             如:< xml version=”1.0″ encoding=”UTF-8″ standalone=”no”  ><b></b> 否则会有如下异常:[Fatal Error] *.xml:1:1: Premature end of file.

发表评论

电子邮件地址不会被公开。 必填项已用*标注