0

我有一个仅包含 .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();
        } 
    }
}
4

2 回答 2

1

如果文件具有您可以使用的 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);  
}

从这里改编的代码

于 2013-03-27T15:23:11.803 回答
0

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));
于 2013-03-27T16:22:08.800 回答