解析Java中的XML文件并输出所有错误

时间:2021-12-07 21:02:03

I am currently trying to parse an XML file in Java. I have done lots of research and tried using many of the available libraries such as DOM, SAX, etc. All of these are great and have lots of options. My issue though is I want to be able to parse an entire XML file then output any errors that may be there. With SAX for example, it will parse the file, but only output the first syntax error. Is there anyway to output all errors at once like a compiler can or am I out of luck?

我目前正在尝试用Java解析XML文件。我做了大量的研究,并尝试使用许多可用的库,如DOM、SAX等,这些都很好,并且有很多选择。但我的问题是,我希望能够解析整个XML文件,然后输出任何可能存在的错误。例如,对于SAX,它将解析文件,但只输出第一个语法错误。是否可以像编译器一样同时输出所有的错误,还是我运气不佳?

If this is not possible, I am attempting a work around to parse my XML file like a normal text file and grab all the opening and closing tags. Is there a simpler way to do this besides creating many conditional statements and rules?

如果这是不可能的,那么我正在尝试对XML文件进行像普通文本文件那样的解析,并获取所有的打开和关闭标记。除了创建许多条件语句和规则之外,是否还有更简单的方法来实现这一点?

Any help is greatly appreciated.

非常感谢您的帮助。

Thanks

谢谢

2 个解决方案

#1


-1  

I don't think you will have much success finding such type of XML parsers. By definition, an XML parser should throw an exception on first error it encounters. If it doesn't, it will have no way to identify the node type or correctness of any subsequent text ...

我认为您不会成功地找到这种类型的XML解析器。根据定义,XML解析器应该在遇到第一个错误时抛出异常。如果没有,它将无法识别任何后续文本的节点类型或正确性……

#2


0  

Just use a custom ErrorHandler (as the default ErrorHandler throws exception on first error).

只需使用一个定制的ErrorHandler(当默认的ErrorHandler在第一个错误时抛出异常)。

 public void validateXML(SAXSource source, Schema schema) throws SAXException, IOException
  {
    javax.xml.validation.Validator validator = schema.newValidator();
    validator.setErrorHandler(new ErrorHandler()
    {

      @Override
      public void warning(SAXParseException exception) throws SAXException
      {
        System.err.println("Notice: " + exception.toString());
      }

      @Override
      public void fatalError(SAXParseException exception) throws SAXException
      {
        error(exception);
      }

      @Override
      public void error(SAXParseException exception) throws SAXException
      {
        System.err.println("Error occured: " + exception.toString());
      }
    });
    validator.validate(source);
  }

#1


-1  

I don't think you will have much success finding such type of XML parsers. By definition, an XML parser should throw an exception on first error it encounters. If it doesn't, it will have no way to identify the node type or correctness of any subsequent text ...

我认为您不会成功地找到这种类型的XML解析器。根据定义,XML解析器应该在遇到第一个错误时抛出异常。如果没有,它将无法识别任何后续文本的节点类型或正确性……

#2


0  

Just use a custom ErrorHandler (as the default ErrorHandler throws exception on first error).

只需使用一个定制的ErrorHandler(当默认的ErrorHandler在第一个错误时抛出异常)。

 public void validateXML(SAXSource source, Schema schema) throws SAXException, IOException
  {
    javax.xml.validation.Validator validator = schema.newValidator();
    validator.setErrorHandler(new ErrorHandler()
    {

      @Override
      public void warning(SAXParseException exception) throws SAXException
      {
        System.err.println("Notice: " + exception.toString());
      }

      @Override
      public void fatalError(SAXParseException exception) throws SAXException
      {
        error(exception);
      }

      @Override
      public void error(SAXParseException exception) throws SAXException
      {
        System.err.println("Error occured: " + exception.toString());
      }
    });
    validator.validate(source);
  }