您的位置:首页 > 编程语言 > Java开发

java读取文件夹里的所有xml文件内容

2013-02-19 14:31 411 查看
package com.easymap.util;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class ReadFile {
private static String filepath="E:/WB_DB_TMP";
public String getFileList(){
File filelist = new File(filepath);
List list = Arrays.asList(filelist.list());
StringBuffer sb = new StringBuffer();
File f = null;
for(int i=0;i<list.size();i++){
f = new File(filepath+"/"+list.get(i));
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dbf.newDocumentBuilder();
Document doc = builder.parse(f);
Element root = doc.getDocumentElement();
NodeList nls = root.getElementsByTagName("Item");
Element ss = null;
for(int length=0;length<nls.getLength();length++){
ss = (Element) nls.item(length);
sb.append(ss.getAttribute("Service_code")).append(",").append(ss.getAttribute("User_name")).append(",").append(ss.getAttribute("Certificate_type")).append(",").append(ss.getAttribute("Certificate_code")).append(",").append(ss.getAttribute("Online_time")).append(",").append(ss.getAttribute("Offline_time")).append(",").append(ss.getAttribute("Net_ending_name")).append(",").append(ss.getAttribute("Net_ending_ip")).append(",").append(ss.getAttribute("Net_ending_mac")).append(",").append(ss.getAttribute("Org_name")).append(",").append(ss.getAttribute("Country")).append(",").append(ss.getAttribute("Service_type")).append(",").append(ss.getAttribute("Card_id")).append("&");
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
f.delete();
}
String result = "";
if(sb.toString().length()==0){
result = "";
}else{
result = sb.toString().substring(0, sb.toString().length()-1);
}
return result;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: