java实现xml⽂件读取和写⼊import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
* xml边加载边读取:第⼀种读取⽅式
* @param file
* @throws Exception
*/
public static void XmlSAXReader(File file) throws Exception{
SAXReader reader = new SAXReader();
Document doc = ad(file);
Element root = RootElement();//xml 节点元素
List firstList = root.elements();//第⼆层节点元素 集合
System.out.println(firstList.size());
for (Iterator it = firstList.iterator(); it.hasNext();) {
Element secondelement = (Element) it.next();//第⼆层节点元素
List secondList = secondelement.elements();//第三层节点元素 集合
for (Iterator itsecond = secondList.iterator(); itsecond.hasNext();) {
Element thirdelement = (Element) ();第三层节点
XmlSAXReaderNext( thirdelement );
}
}
}
public static int XmlSAXReaderNext(Element thirdelement ) {
//第四层节点元素集合
List forthList = thirdelement.elements();
System.out.println(forthList==null?"0":forthList.size());
for (Iterator it = forthList.iterator(); it .hasNext();) {
Element prop = (Element) it.next();//第四层节点元素
List<DefaultAttribute> l=prop.attributes();//获取节点属性
Iterator t= l.iterator();
while(t.hasNext()) {
DefaultAttribute de=(DefaultAttribute) t.next();
System.out.Name() + ":" + de.getData());/打印属性
}
List l=prop.elements();
System.out.println(l==null?"0":l.size());
System.out.Name() + ":" + Data());//打印节点名称和内容
}
System.out.println();
return 1;
}
/**
* ⼀次性读取xml⽂件中所有内容到内存: 第⼆种读取⽅式
*/
public static void readXml(String pathName,Connection conn,String preSql){
InputStream inputStream=null;
try{
//创建xml编辑对象
org.jdom.input.SAXBuilder saxBuilder = new org.jdom.input.SAXBuilder(false);
inputStream= new FileInputStream(new File(pathName));//创建⽂件输⼊流对象
org.jdom.Element rootElement = RootElement();//获取xml根元素(⼀级节点)
// String tableId = AttributeValue("time").trim();//获取根属性
List<org.jdom.Element> rows = Children("row");//获取⼦元素集合(⼆级节点)
org.jdom.Element row;
if(rows != null){
for(int m = 0;m<rows.size();m++){//遍历⼆级节点
row = (m);
List<org.jdom.Element> cols = Children("col");//获取三级节点集合
org.jdom.Element col;
if(cols != null){
for(int k = 3;k<cols.size();k++){
col = (k);
String value= ChildText("value").trim();
System.out.println("value:"+value);
}
}
}
}
inputStream.close();
}catch(Exception e){
try{
if(inputStream!=null)inputStream.close();
}catch(Exception e1){
e1.printStackTrace();
}
e.printStackTrace();
}
}
public static void closeWriteXml(org.dom4j.io.XMLWriter out ,OutputStreamWriter osw,FileOutputStream fos,Statement stmt,ResultSet rs ){
try{
if (out != null) out.close();
if (osw != null) osw.close();
if (fos != null) fos.close();
if (rs != null) rs.close();
if (stmt != null) stmt.close();
}catch(Exception e){
e.printStackTrace();
}
}
//Java实现xml⽂件数据写⼊
public static void writeXml(String path,Connection conn,String sql){
org.dom4j.Element row = null;
org.dom4j.io.XMLWriter out = null;
OutputStreamWriter osw = null;
FileOutputStream fos = null;
Statement stmt=null;
ResultSet rs =null;
try {
ateStatement();
rs = uteQuery(sql);//查询数据
File xmlFile = new File(path);// 指定xml⽂件全路径,不存在则⽣成,重复名称则覆盖
fos = new FileOutputStream(xmlFile);
osw = new OutputStreamWriter(fos, "UTF-8");// 指定编码,防⽌写中⽂乱码
// 输出格式化
org.dom4j.io.OutputFormat format = org.dom4j.atePrettyPrint();
format.setEncoding("UTF-8");
out = new org.dom4j.io.XMLWriter(fos, format);//创建xml输出对象
out.startDocument();
org.dom4j.Document document = org.ateDocument();//创建⽂档对象 // 添加元素 xml
org.dom4j.Element xmlElement = document.addElement("table");//增加⽂档根节点
xmlElement.addAttribute("time", new Date().getTime()+"");//增加根节点属性
out.writeOpen(xmlElement);//增加根节点到xml输出对象中
/**
* 加载数据
*/
if (rs != null) {
ResultSetMetaData md = rs.getMetaData(); // 循环结果集(rs)
while (rs.next()) {
row = org.ateElement("row");
for(int i=1;i<=md.getColumnCount();i++){
String columnName = md.getColumnName(i);
Object values = rs.getObject(columnName);
row.addAttribute(columnName, values != null ? String() : "");
}
使用dom4j解析xml文件out.write(row);//增加⼆级节点到xml输出对象中
row.clearContent();
}
}
out.writeClose(xmlElement);
xmlElement.clearContent();
closeWriteXml( out , osw, fos, stmt, rs );
} catch (Exception e) {
closeWriteXml( out , osw, fos, stmt, rs );
e.printStackTrace();
}
}
注意:xml⽂件较⼤的时候,使⽤边加载边读取的⽅式,读取速度会明显加快
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论