打印java中具有特定文件名的xml标记出现次数
我编写了java代码来计算xml标记出现的次数,从目录和子目录中检索文件和文件名。任何人都可以帮助我获取逻辑,以打印具有特定文件名的xml标记出现次数。 下面是我的代码片段
public void parseXml(List<File> xmlFiles, String tagName, List<String> names) {
for (File xmlFile : xmlFiles) {
dbFact = DocumentBuilderFactory.newInstance();
try {
docBuild = dbFact.newDocumentBuilder();
dom = docBuild.parse(xmlFile);
nl = dom.getElementsByTagName(tagName);
System.out.println("Total " + tagName + " tags: " + nl.getLength());
} catch (ParserConfigurationException pe) {
System.out.println(pe);
} catch (SAXException se) {
System.out.println(se);
} catch (IOException ie) {
System.out.println(ie);
}
}
public static List<File> getFiles(String path){
File folder = new File(path);
List<File> resultFiles = new ArrayList<File>();
File[] listOfFiles = folder.listFiles();
for(File file: listOfFiles){
if(file.isFile() && file.getAbsolutePath().endsWith(".xml") && !file.getAbsolutePath().contains("settings_")){
resultFiles.add(file);
}else if(file.isDirectory()){
resultFiles.addAll(getFiles(file.getAbsolutePath()));
}
}
return resultFiles;
}
public static List<String> readXmlName(String path){
File folder = new File(path);
List<String> names = new ArrayList<String>();
File[] listOfFiles = folder.listFiles();
for(File file: listOfFiles){
if(file.isFile() && file.getAbsolutePath().endsWith(".xml") && !file.getAbsolutePath().contains("settings_")){
names.add(file.getName());
}else if(file.isDirectory()){
names.addAll(readXmlName(file.getAbsolutePath()));
}
}
return names;
}
共 (0) 个答案