xml文件格式:
java代码:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class JXml {
private String path;
private File resultFolder;
private String childName;
private String attributeName;
DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
public JXml(String path, String childName, String attributeName) {
super();
this.path = path;
this.childName = childName;
this.attributeName = attributeName;
}
public void go(){
File directory = new File(path);
if (!isDirectory(directory)) {
System.out.println("path路径错误或者不是文件夹");
return;
}
File[] files = getChildFiles(directory);
if (!hasFiles(files)) {
System.out.println("文件夹中没有文件");
return;
}
createDirectory(path);
for (int i = 0; i < files.length; i++) {
// 判断是否为xml文件
// 判断xml文件是否为空
readXml(files[i]);
}
}
public void readXml(File file) {
DocumentBuilder builder = null;
Document doc = null;
FileWriter fw = null;
BufferedWriter bw = null;
File resultFile = new File(resultFolder.getPath().replace("\\", "/")
+ "/" + file.getName() + "结果" + ".txt");
try {
resultFile.createNewFile();
fw = new FileWriter(resultFile);
bw = new BufferedWriter(fw);
builder = fac.newDocumentBuilder();
doc = builder.parse(new InputSource(new BufferedReader(new InputStreamReader(new FileInputStream(file),"GB18030"))));
Element root = doc.getDocumentElement();
NodeList nodes = root.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
if (node.getNodeName().equals(childName)) {
bw.write(node.getAttributes().getNamedItem(attributeName)
.getNodeValue());
bw.newLine();
}
}
bw.flush();
bw.close();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public boolean isDirectory(File directory) {
if (directory.isDirectory()) {
return true;
}
return false;
}
public File[] getChildFiles(File directory) {
return directory.listFiles();
}
public void createDirectory(String path) {
Date data = new Date();
SimpleDateFormat tempDate = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
String datetime = tempDate.format(data);
String resultPath = path + "结果" + datetime;
File folder = new File(resultPath);
if (!folder.exists()) {
folder.mkdir();
}
System.out.println("结果保存在" + resultPath + "文件夹下");
this.resultFolder = folder;
}
public boolean hasFiles(File[] files){
if (!(files.length == 0)){
return true;
}
return false;
}
public static void main(String[] args) {
String path = "D:/rulexml";
String childName = "rule";
String attributeName = "name";
JXml jxml = new JXml(path, childName, attributeName);
jxml.go();
}
}
说明:代码的目的是将xml文件中的rule节点的name的值输出
将xml文件放在文件夹下,我将xml文件放在D:/rulexml文件夹下,path=文件夹路径,注意正反斜杠。文中两行注释是未完善的地方。