【问题标题】:How to report a missing DTD files to the user?如何向用户报告丢失的 DTD 文件?
【发布时间】:2012-06-12 02:27:32
【问题描述】:

给定代码:

public class ModelHandler
{
//members
private DocumentBuilderFactory m_domFactory;
private DocumentBuilder m_builder;
private Document m_doc;
private XPathFactory m_factory;
private List<String> m_inputErrorLog;


public void openXmlFile(File file)  
{

    this.m_inputErrorLog = new LinkedList<String>() ;

    try 
    {
        m_builder = m_domFactory.newDocumentBuilder();
    } 

    catch (ParserConfigurationException ex) { m_inputErrorLog.add(ex.getMessage());}


       try 
       {
            m_doc = m_builder.parse(file);        
       } 
        catch (Exception ex) 
       {
           m_inputErrorLog.add(ex.getMessage());    
           m_domFactory.setValidating(false);
       }            

        try 
        {
            getNodesList("/"+m_doc.getDocumentElement().getNodeName());
        } 
        catch (XPathExpressionException ex) {
        m_inputErrorLog.add(ex.getMessage());
    }
}   

我想向用户展示 DTD 文件丢失,同时使用 GUI

在尝试打开XML 文件时,我该怎么做?

谢谢

【问题讨论】:

  • 你为什么不直接打开一个警报/对话框来达到这个效果?

标签: java xml dom error-handling dtd


【解决方案1】:

这里是解决方案,对于任何有一天可能需要它的人:

import java.io.IOException;
// DOM
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
// SAX
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.XMLReader;

import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.InputSource;

public class XMLUtils {

  private XMLUtils() {}

  // validate using DOM (DTD as defined in the XML)
  public static boolean validateWithDTDUsingDOM(String xml) 
    throws ParserConfigurationException, IOException
  {
    try {
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      factory.setValidating(true);
      factory.setNamespaceAware(true);

      DocumentBuilder builder = factory.newDocumentBuilder();

      builder.setErrorHandler(
          new ErrorHandler() {
            public void warning(SAXParseException e) throws SAXException {
              System.out.println("WARNING : " + e.getMessage()); // do nothing
            }

            public void error(SAXParseException e) throws SAXException {
              System.out.println("ERROR : " + e.getMessage());
              throw e;
            }

            public void fatalError(SAXParseException e) throws SAXException {
              System.out.println("FATAL : " + e.getMessage());
              throw e;
            }
          }
          );
      builder.parse(new InputSource(xml));
      return true;
    }
    catch (ParserConfigurationException pce) {
      throw pce;
    } 
    catch (IOException io) {
      throw io;
    }
    catch (SAXException se){
      return false;
    }
  }


  // validate using SAX (DTD as defined in the XML)
  public static boolean validateWithDTDUsingSAX(String xml) 
    throws ParserConfigurationException, IOException
  {
    try {

      SAXParserFactory factory = SAXParserFactory.newInstance();
      factory.setValidating(true);
      factory.setNamespaceAware(true);

      SAXParser parser = factory.newSAXParser();

      XMLReader reader = parser.getXMLReader();
      reader.setErrorHandler(
          new ErrorHandler() {
            public void warning(SAXParseException e) throws SAXException {
              System.out.println("WARNING : " + e.getMessage()); // do nothing
            }

            public void error(SAXParseException e) throws SAXException {
              System.out.println("ERROR : " + e.getMessage());
              throw e;
            }

            public void fatalError(SAXParseException e) throws SAXException {
              System.out.println("FATAL : " + e.getMessage());
              throw e;
            }
          }
          );
      reader.parse(new InputSource( xml ));
      return true;
    }
    catch (ParserConfigurationException pce) {
      throw pce;
    } 
    catch (IOException io) {
      throw io;
    }
    catch (SAXException se){
      return false;
    }
  }

  public static void main (String args[]) throws Exception{ 

    System.out.println(XMLUtils.validateWithDTDUsingDOM("c:/temp/howto.xml"));
    System.out.println(XMLUtils.validateWithDTDUsingSAX("c:/temp/howto.xml"));
    /*
      output :
               true
               true
    */           
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-09
    • 2020-02-16
    • 1970-01-01
    • 2021-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多