分享
 
 
 

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

王朝other·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

//很多时候,为了避免多次访问/查询数据库重的数据或者便于察看,将需要的数据一次取出并写入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;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有