我有一个仅包含 .xml 文件的文件夹。我的程序需要读取每个文件,然后返回标签之间具有“假”的文件的名称。我刚在想:
final Pattern pattern = Pattern.compile("<isTest>(.+?)</isTest>");
final Matcher matcher = pattern.matcher("<isTest>false</isTest>");
matcher.find();
System.out.println(matcher.group(1));
我是java新手,所以任何帮助都将不胜感激。
你能告诉我哪里出错了吗?
public class FileIO
{
public static void main(String[] args)
{
File dir = new File("d:\temp");
List<String> list = new ArrayList<String>();
//storing the names of the files in an array.
if (dir.isDirectory())
{
String[] fileList = dir.list();
Pattern p = Pattern.compile("^(.*?)\\.xml$");
for (String file : fileList)
{
Matcher m = p.matcher(file);
if (m.matches())
{
list.add(m.group(1));
}
}
}
try
{
XPathFactory xPathFactory = XPathFactory.newInstance( );
XPath xpath = xPathFactory.newXPath( );
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance( );
DocumentBuilder builder = docBuilderFactory.newDocumentBuilder( );
//Loop over files
for (int i = 0; i < fileList.length; i++)
{
Document doc = builder.parse(fileList[i]);
boolean matches = "false".equals(xpath.evaluate("//isTest/text()", doc));
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}