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

该Java代码示例展示了如何读取指定文件夹下所有的XML文件,并解析文件中名为'Item'的元素,提取出Service_code, User_name等属性信息。程序会遍历文件夹,对每个XML文件进行解析,并在捕获可能的异常后删除已处理的文件。" 112260152,8250505,NoSQL数据库:阿里云技术创新与生态开放,"['NoSQL数据库', '云数据库', '图数据库', '内存数据库', '云原生']

开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

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;
	}
}

开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值