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; } }
相关文章推荐
- Java 替换文件夹下所有文件中指定的内容
- java读取某个文件夹下的所有文件
- Java自动读取文件夹下的所有文件(包括子文件夹)
- Java实现读取文件夹下(包括子目录)所有文件的文件名
- java读取某个文件夹下的所有文件
- java读取某个文件夹下的所有文件
- java 读取某个文件夹下的所有文件
- 读取文件夹下所有文件内容
- java遍历读取xml文件内容
- java 遍历读取xml文件内容
- 【Java】读取其下所有文件夹与文件的路径
- java读取某个文件夹下的所有文件
- Java读取和删除文件夹下的所有文件
- Java中读取某个目录下的所有文件和文件夹
- python2.7学习之读取文件夹下所有文件名称及内容
- java读取某个文件夹下的所有文件
- java递归读取文件夹中的所有文件以及路径
- java 读取XML文件里的节点内容
- Java新建目录、新建文件、删除文件、删除文件夹、删除文件夹里面的所有文件、复制整个文件夹内容