=_=? 話不多說!直接上代碼!=_=
1、XmlUtil.java? ? ?xml解析工具類
public class XmlUtil {
private static String dicName = "";
private static String dicValue = "";
// 用于存儲需要的數(shù)據(jù)
private static List<Map<String, String>> paramlist = new ArrayList();
/**
* 找到所有xml文件,并進行解析
* @param strPath 文件路徑
* @param name 檢索的屬性名稱
* @param value 檢索的屬性值
* @throws Exception
*/
public static void refreshFileList(String strPath, String name, String value){
// 設置需要檢索的值
dicName = name;
dicValue = value;
// 創(chuàng)建文件對象
File dir = new File(strPath);
File[] files = dir.listFiles();
if (files == null) {
return;
}
// 遍歷當前目錄下所有的文件和文件夾
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) { // 文件夾
refreshFileList(files[i].getAbsolutePath(), name, value);
} else { // 文件
// 判斷是否為xml文件
if (files[i].getAbsoluteFile().getName().split("\\.")[1].equals("xml")) {
// 解析xml文件
parseFile(files[i].getAbsoluteFile());
}
}
}
}
/**
* 將xml文件解析成Document對象
* @param filePath
*/
public static void parseFile(File filePath) {
// 1.創(chuàng)建DocumentBuilderFactory對象
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
// 2.創(chuàng)建DocumentBuilder對象
DocumentBuilder builder = factory.newDocumentBuilder();
// 3.將xml文件解析成Document對象
Document document = builder.parse(filePath);
// 4.獲取根節(jié)點 model:Model為標簽名稱
NodeList sList = document.getElementsByTagName("model:Model");
// 5.通過Element方式解析并獲取使用字典的字段
Map<String, String> parMap = element(sList);
// 6.存入需要的數(shù)據(jù)
if (!parMap.isEmpty()) {
paramlist.add(parMap);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 用Element方式獲取節(jié)點信息以及所有屬性信息
* @param list
*/
public static Map<String, String> element(NodeList list) {
Map<String, String> parMap = new HashMap<>();
for (int i = 0; i < list.getLength(); i++) {
Element element = (Element) list.item(i);
// 獲取所有attribute標簽
NodeList attribute = element.getElementsByTagName("attribute");
for (int j = 0; j < attribute.getLength(); j++) {
// 判斷是否有屬性
if (attribute.item(j).hasAttributes()) {
Map<String, String> map = new HashMap<>();
// 存在則獲取所有的屬性
NamedNodeMap attributes = attribute.item(j).getAttributes();
for (int k = 0; k < attributes.getLength(); k++) {
// 屬性名
String nodeName = attributes.item(k).getNodeName();
// 屬性值
String nodeValue = attributes.item(k).getNodeValue();
// 拿到屬性名稱和屬性值
if ("name".equals(nodeName)) {
map.put("name", nodeValue);
}
if ("value".equals(nodeName)) {
map.put("value", nodeValue);
}
}
// 判斷是否使用的院系字典
if (dicName.equals(map.get("name")) && dicValue.equals(map.get("value"))) {
// 獲取使用了字典的字段
Node parentNode = attribute.item(j).getParentNode();
if (parentNode.hasAttributes()) {
// 所有的屬性
NamedNodeMap attributes1 = parentNode.getAttributes();
for (int l = 0; l < attributes1.getLength(); l++) {
parMap.put(attributes1.item(l).getNodeName(), attributes1.item(l).getNodeValue());
}
}
// 文件路徑存入
parMap.put("url", parentNode.getOwnerDocument().getDocumentURI());
}
map.clear();
}
}
}
return parMap;
}
/**
* 講數(shù)據(jù)寫入txt文件中
* @param writePath 寫入的路徑
* @throws Exception
*/
public void writeData(String writePath) throws Exception {
// 拼接內(nèi)容
StringBuffer stringBuffer = new StringBuffer();
paramlist.forEach(e -> {
stringBuffer.append(e.get("url") + " ======= " + e.get("caption") + " ======= " + e.get("name") + "\n");
});
File file = new File(writePath);
File parentFile = file.getParentFile();
// 文件夾不存在,創(chuàng)建文件夾
if (!parentFile.exists()) {
parentFile.mkdirs();
}
// 創(chuàng)建文件
file.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(file);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fileOutputStream));
// 寫入數(shù)據(jù)
writer.write(String.valueOf(stringBuffer));
writer.close();
System.out.println("文件寫入完畢");
}
}
?2、測試代碼
public static void main(String[] args) throws Exception {
long a = System.currentTimeMillis();
XmlUtil xmlUtil = new XmlUtil();
// 設置需要檢索的屬性名和值
xmlUtil.refreshFileList("D:\\my\\file_test", "dic", "d-b122-sad-2d3q-12");
xmlUtil.writeData("D:\\my\\outfile\\file1.txt");
System.out.println("消耗時長:" + (System.currentTimeMillis() - a));
}
解析xml方式還有很多種,這只是其中一種,可以參考:Java XML解析 - 利用dom(org.w3c.dom)解析XML文章來源:http://www.zghlxwxcb.cn/news/detail-634446.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-634446.html
到了這里,關于解析xml文件,獲取需要的數(shù)據(jù)并寫入txt文件中的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!