import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.util.ArrayList; 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.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class XmlReader { /** * 读取xml文件指定节点内容并导出到csv文件中 * @param path * 指定文件夹路径 * @param destNode * 目标节点 * @param fileName * 出力csv文件名 */ public void readXmlFile(String path, String destNode, String fileName) { File file = new File(fileName); FileOutputStream out; try { // 建立csv输出文件流 out = new FileOutputStream(file); OutputStreamWriter osw = new OutputStreamWriter(out); BufferedWriter bw = new BufferedWriter(osw); // 初始化csv文件 initCSV(bw); // 得到指定文件夹下的所有xml文件 List fileList = getXmlFiles(path); // 创建xml文件 DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = null; String xmlFileName; Node root; NodeList links; for (int k = 0; k < fileList.size(); k++) { xmlFileName = fileList.get(k).toString(); doc = builder.parse(xmlFileName); doc.normalize(); // xml文件根节点 root = doc.getDocumentElement(); // xml文件中所有指定节点 links = doc.getElementsByTagName(destNode); for (int i = 0; i < links.getLength(); i++) { Node link = links.item(i); // 指定节点的所有子节点 NodeList childNodes = link.getChildNodes(); StringBuffer sb = new StringBuffer(); // 遍历子节点 for (int j = 0; j < childNodes.getLength(); j++) { Node childNode = childNodes.item(j); String src = childNode.toString(); // 过滤回车换行 if (src.indexOf("/n") != -1) { src = src.replaceAll("/n", ""); } if (src.indexOf("/r") != -1) { src = src.replaceAll("/r", ""); } sb.append(src); } // 按规定格式导出csv exportCsv(xmlFileName, root.getNodeName(), destNode, sb.toString(), bw); } } bw.close(); osw.close(); out.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } 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(); } } /** * 获得指定路径下的所有xml文件 * @param path * 指定文件夹 * @return * xml list */ public static List getXmlFiles(String path) { List xmlFiles = new ArrayList(); File filePath = new File(path); File[] fileList = filePath.listFiles(); for (int i = 0; i < fileList.length; i++) { if (fileList[i].isFile()) { // 对获取的文件全名进行拆分 String[] arrName = fileList[i].getName().split("//."); if ((arrName != null) && (arrName.length > 0)) { if (arrName[arrName.length - 1].equalsIgnoreCase("xml")) { xmlFiles.add(fileList[i].getPath()); } } } else if (fileList[i].isDirectory()) { xmlFiles.addAll(getXmlFiles(fileList[i].getPath())); } } return xmlFiles; } public static void exportCsv(String fileName, String root, String destNode, String nodesValue, BufferedWriter bw) throws IOException { bw.write(fileName); bw.write(","); bw.write(root); bw.write(","); bw.write(destNode); bw.write(","); bw.write(nodesValue); bw.newLine(); } public static void initCSV(BufferedWriter bw) throws IOException { bw.write("XmlFileName"); bw.write(","); bw.write("XmlFileRootNode"); bw.write(","); bw.write("XmlFileDestNode"); bw.write(","); bw.write("XmlFileNodesValue"); bw.newLine(); } public static void main(String args[]) { XmlReader reader = new XmlReader(); String path = "D://test"; String destNode = "listener"; String fileName = "D://test.csv"; reader.readXmlFile(path, destNode, fileName); } }