java中xml文件的解析列表
我试图编写java代码来解析xml文件列表。如何将这些xml文件列表传递到parse方法中 要分析文件的代码:java中xml文件的解析列表,java,xml,Java,Xml,我试图编写java代码来解析xml文件列表。如何将这些xml文件列表传递到parse方法中 要分析文件的代码: public void parseXml(String xmlPath, String tagName) { DocumentBuilderFactory dbFact = DocumentBuilderFactory.newInstance(); try { DocumentBuilder docBuild = dbFact.
public void parseXml(String xmlPath, String tagName) {
DocumentBuilderFactory dbFact = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder docBuild = dbFact.newDocumentBuilder();
Document dom = docBuild.parse(xmlPath);
NodeList nl = dom.getElementsByTagName(tagName);
System.out.println("Total tags: " + nl.getLength());
} catch (ParserConfigurationException pe) {
System.out.println(pe);
}catch (SAXException se){
System.out.println(se);
}catch (IOException ie){
System.out.println(ie);
}
}
从目录检索所有xml文件的代码:
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")){
resultFiles.add(file);
}else if(file.isDirectory()){
resultFiles.addAll(getFiles(file.getAbsolutePath()));
}
}
return resultFiles;
}
公共静态列表getFiles(字符串路径){
文件夹=新文件(路径);
List resultFiles=new ArrayList();
File[]listOfFiles=folder.listFiles();
对于(文件:listOfFiles){
if(file.isFile()&&file.getAbsolutePath().endsWith(“.xml”)){
添加(文件);
}else if(file.isDirectory()){
addAll(getFiles(file.getAbsolutePath());
}
}
返回结果文件;
}
使用过滤器
public static List<File> getFiles(String path) {
File folder = new File(path);
File[] listOfFiles = folder.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".xml") && !name.toLowerCase().contains("settings_");
}
});
return Arrays.asList(listOfFiles);
}
公共静态列表getFiles(字符串路径){
文件夹=新文件(路径);
File[]listOfFiles=folder.listFiles(新文件名过滤器(){
公共布尔接受(文件目录,字符串名称){
返回name.toLowerCase().endsWith(“.xml”)&&!name.toLowerCase().contains(“设置”);
}
});
返回Arrays.asList(listOfFiles);
}
并修改您的方法以接受文件列表
public void parseXml(List<File> xmlFiles, String tagName) {
for (File xmlFile : xmlFiles) {
DocumentBuilderFactory dbFact = DocumentBuilderFactory
.newInstance();
try {
DocumentBuilder docBuild = dbFact.newDocumentBuilder();
Document dom = docBuild.parse(xmlFile);
NodeList nl = dom.getElementsByTagName(tagName);
System.out.println("Total 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 void parseXml(列出xml文件,字符串标记名){
用于(文件xmlFile:xmlFiles){
DocumentBuilderFactory dbFact=DocumentBuilderFactory
.newInstance();
试一试{
DocumentBuilder docBuild=dbFact.newDocumentBuilder();
文档dom=docBuild.parse(xmlFile);
NodeList nl=dom.getElementsByTagName(标记名);
System.out.println(“总标记:+nl.getLength());
}捕获(ParserConfiguration异常pe){
系统输出打印LN(pe);
}捕获(SAXSE异常){
系统输出打印LN(se);
}捕获(IOIE){
系统输出打印(ie);
}
}
}
如何将此文件列表传递到parse()方法parseXml(getFiles(“xmlPath”),“myXmlTag”)代码>代码的目标是什么?要计算标记在一组xml文件中的出现次数?Syam,在我的第一个代码段中,我想从目录或子目录中检索特定的文件,并且我想忽略使用设置_xxxx命名的xml文件。你能帮我吗?你可以给过滤器添加任何条件。更新了我的答案。