读取文件夹中所有.xml文件和特定标记之间的值的java代码

读取文件夹中所有.xml文件和特定标记之间的值的java代码,java,xml,Java,Xml,我有一个只包含.xml文件的文件夹。我的程序需要读取每个文件,然后返回标记之间有“false”的文件名。我在想: final Pattern pattern = Pattern.compile("<isTest>(.+?)</isTest>"); final Matcher matcher = pattern.matcher("<isTest>false</isTest>"); matcher.fin

我有一个只包含.xml文件的文件夹。我的程序需要读取每个文件,然后返回标记之间有“false”的文件名。我在想:

        final Pattern pattern = Pattern.compile("<isTest>(.+?)</isTest>");
        final Matcher matcher = pattern.matcher("<isTest>false</isTest>");
        matcher.find();
        System.out.println(matcher.group(1));
final Pattern=Pattern.compile((.+?)”;
最终匹配器匹配器=pattern.Matcher(“false”);
matcher.find();
系统输出println(匹配器组(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();
        } 
    }
}
公共类文件IO
{
公共静态void main(字符串[]args)
{
文件目录=新文件(“d:\temp”);
列表=新的ArrayList();
//将文件名存储在数组中。
if(dir.isDirectory())
{
字符串[]fileList=dir.list();
模式p=Pattern.compile(“^(.*?\\\.xml$”);
for(字符串文件:文件列表)
{
Matcher m=p.Matcher(文件);
如果(m.matches())
{
列表。添加(m.组(1));
}
}
}
尝试
{
XPathFactory XPathFactory=XPathFactory.newInstance();
XPath=xPathFactory.newXPath();
DocumentBuilderFactory docBuilderFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder=docBuilderFactory.newDocumentBuilder();
//循环文件
for(int i=0;i
如果文件有一个可以使用的XSD,
JAXB
是您选择的解决方案。您不想在XML上使用正则表达式,因为
CDATA
和嵌套标记一样会毁了您的一天

像这样使用SAX是一种可能的解决方案:

public static void main(String[] args)
{
SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser saxParser = factory.newSAXParser();

    DefaultHandler handler = new DefaultHandler() {

    boolean isTest= false;

    public void startElement(String uri, String localName,String qName, 
                Attributes attributes) throws SAXException {

        System.out.println("Start Element :" + qName);

        if (qName.equalsIgnoreCase("isTest")) {
            isTest= true;
        }

    }

    public void endElement(String uri, String localName,
        String qName) throws SAXException {

        System.out.println("End Element :" + qName);

    }

    public void characters(char ch[], int start, int length) throws SAXException {

        if (isTest) {
            System.out.println("is test : " + new String(ch, start, length));
            isTest= false;
        }
    }

     };

       saxParser.parse("c:\\file.xml", handler);  
}

改编自Sax的代码可能效率更高(内存方面),但这里是xPath版本的一个片段,可能更短,行方面

XPathFactory xPathFactory = XPathFactory.newInstance( );
XPath xpath = xPathFactory.newXPath(  );
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(  );
DocumentBuilder builder = docBuilderFactory.newDocumentBuilder(  );

/* Loop over files */

Document doc =  builder.parse(file);
boolean matches = "false".equals(xpath.evaluate("//isTest/text()", doc));

解析HTML,通常是XML,几乎总是一个坏主意。如果您必须检查标签之间的内容,您可能需要考虑实际解析XML(使用SAX或更有用的东西)。