我有一個只包含.xml文件的文件夾。我的程序需要讀取每個文件,然後返回標籤之間爲'false'的文件的名稱。我在想:讀取文件夾中所有.xml文件以及特定標記之間的值的java代碼
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();
}
}
}
解析HTML,通常是XML,通常幾乎總是一個壞主意。如果你必須檢查標籤之間的內容,你可能會考慮實際解析XML(使用SAX或更有用的東西)。 – TC1 2013-03-27 15:18:15