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读取文件夹里的所有xml文件内容
最新推荐文章于 2024-04-30 17:36:52 发布
该Java代码示例展示了如何读取指定文件夹下所有的XML文件,并解析文件中名为'Item'的元素,提取出Service_code, User_name等属性信息。程序会遍历文件夹,对每个XML文件进行解析,并在捕获可能的异常后删除已处理的文件。"
112260152,8250505,NoSQL数据库:阿里云技术创新与生态开放,"['NoSQL数据库', '云数据库', '图数据库', '内存数据库', '云原生']
开发板推荐:天空星STM32F407VET6开发板
超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印
开发板推荐:天空星STM32F407VET6开发板
超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

1147

被折叠的 条评论
为什么被折叠?



