网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > Java
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.JXTA Java标准版本 2..
.Java面向对象的思维方法.
.MIDP1.0游戏完整实现-双人扫雷1..
.对初学者有用的文章.
.教你如何编写Java类(二).
.使用JAVA建立稳定的多线程服务器.
.Java的网络功能与编程4.
.由基本概念开始全面认识JAVA.
.用 struts 向数据库中储存图片.
.Hibernate 3.0 beta版本已经发布.
.Programming a Spider in Java.
.JDBC3.0 新特性.
.传言乍起,谁将收购SUN?.
.WebWork的强大的验证器.
.地图的设计与绘制.
.tomcat的sql server数据源的配置.
.Java Applet编程小技巧之文字显示.
.实战SWT布局.
.对J2EE项目的一些体会.
.Webmail-0.7.7 系统源码浅析.

用jdom技术将数据库数据写入读出xml文件

发表日期:2008-1-5



  //很多时候,为了避免多次访问/查询数据库重的数据或者便于察看,将需要的数据一次取出并写入XML文件

//通过查询条件m_condition 按照xmlMapPath的模式/模板 将从库中的数据写到resultXml
//并返回记录条数

public int writeXML(String m_condtion,String xmlMapPath,String resultXml){
int recordNum=0;
String tableName = "table";
String tableCol = "*";
String sql = "select " + tableCol + " from " + tableName + " where " + m_condtion;

Document mapDoc = null;
Document dataDoc = null;
Document newDoc = null;
//开始预备工作
try {
DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
//Create the DocumentBuilder
DocumentBuilder docbuilder = dbfactory.newDocumentBuilder();
//Parse the file to create the Document
mapDoc = docbuilder.parse(xmlMapPath);

dataDoc = docbuilder.newDocument();
//Instantiate the new Document
newDoc = docbuilder.newDocument();
} catch (Exception e) {
System.out.println(e.getMassege());
}

//开始读取映射作用的template文件

Element mapRoot = mapDoc.getDocumentElement();
Node dataNode = mapRoot.getElementsByTagName("data").item(0);
Element dataElement = (Element)dataNode;

//Create a new element called "data"
Element dataRoot = dataDoc.createElement("data");

try {
/******** 这里创建一个连接conn,并创建statement
根据不同的数据源创建
********************************/
ResultSet resultset = stmt.query(sql);
//Get the ResultSet information
ResultSetMetaData resultmetadata = resultset.getMetaData();
int numCols = resultmetadata.getColumnCount();
Log.write("db_to_xml:numCols:" + numCols);

while (resultset.next()) {
//Create a new element called "row"
Element rowEl = dataDoc.createElement("row");

//为了便于浏览和读,创建列数,以id为标记
String colName = "id";
String colVal =Integer.toString(++recordNum);
Element dataEl = dataDoc.createElement(colName);
dataEl.appendChild(dataDoc.createTextNode(colVal));

rowEl.appendChild(dataEl);
for (int i=1; i <= numCols; i++) {

colName = resultmetadata.getColumnName(i);
//Get the column value
colVal = resultset.getString(i);

//Determine if the last column Accessed was null
if (resultset.wasNull()) {
colVal = "";
}

dataEl = dataDoc.createElement(colName);

dataEl.appendChild(dataDoc.createTextNode(colVal));

rowEl.appendChild(dataEl);
}
//Add the row to the root element
dataRoot.appendChild(rowEl);
}
} catch (Exception e) {
Log.write(e.getMessage());
} finally {
Log.write(" db_to_xml: Closing connections...");

}

//Add the root element to the document
dataDoc.appendChild(dataRoot);

Node node1= mapRoot.getElementsByTagName("root").item(0);
Element newRootInfo =(Element)node1;
Log.write("After got newRootInfo ...");
//Retrieve the root and row information

String newRootName = newRootInfo.getAttribute("name");

String newRowName= newRootInfo.getAttribute("rowName");

NodeList newNodesMap = mapRoot.getElementsByTagName("element");

//Create the final root element with the name from the mapping file
Element newRootElement=null;

newRootElement = newDoc.createElement(newRootName);

NodeList oldRows = dataRoot.getElementsByTagName("row");
for (int i=0; i < oldRows.getLength(); i++){

//Retrieve each row in turn
Element thisRow = (Element)oldRows.item(i);

//Create the new row
Element newRow = newDoc.createElement(newRowName);

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

//For each node in the new mapping, retrieve the information
//First the new information...
Element thisElement = (Element)newNodesMap.item(j);
String newElementName = thisElement.getAttribute("name");

//Then the old information
Element oldElement = (Element)thisElement.getElementsByTagName("content").item(0);
String oldField = oldElement.getFirstChild().getNodeValue();

//Get the original values based on the mapping information
Element oldValueElement = (Element)thisRow.getElementsByTagName(oldField).item(0);
String oldValue = oldValueElement.getFirstChild().getNodeValue();

Element newElement = newDoc.createElement(newElementName);
newElement.appendChild(newDoc.createTextNode(oldValue));

NodeList newAttributes = thisElement.getElementsByTagName("attribute");
for (int k=0; k < newAttributes.getLength(); k++) {
//Get the mapping information
Element thisAttribute = (Element)newAttributes.item(k);
String oldAttributeField = thisAttribute.getFirstChild().getNodeValue();
String newAttributeName = thisAttribute.getAttribute("name");

oldValueElement = (Element)thisRow.getElementsByTagName(oldAttributeField).item(0);
String oldAttributeValue = oldValueElement.getFirstChild().getNodeValue();

newElement.setAttribute(newAttributeName, oldAttributeValue);
}

//Add the new element to the new row
newRow.appendChild(newElement);
}
//Add the new row to the root
newRootElement.appendChild(newRow);
}
//Add the new root to the document
newDoc.appendChild(newRootElement);

//把生成的xml文档(newDoc)写到文件中(路径名为resultXml)
try{
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
Properties properties = transformer.getOutputProperties();
properties.setProperty(OutputKeys.ENCODING, "GB2312");//ISO8859_1,GB2312,UTF-8
properties.setProperty(OutputKeys.METHOD, "xml");
properties.setProperty(OutputKeys.VERSION, "1.0");
properties.setProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperties(properties);

DOMSource source = new DOMSource(newDoc);

StreamResult result = new StreamResult(new Java.io.File(resultXml));

transformer.transform(source, result);
//生成XML文件 完成
} catch (Exception e) {
System.out.println("XML file write:"+e.getMessage());
}
return recordNum;
}
上一篇:用JDOM简化XML编程 人气:663
下一篇:学习更多 人气:412
浏览全部Java的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐