有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

xml无法读取XMLFile,异常为:java。木卫一。FileNotFoundException:C:\Temp\logger。dtd(系统找不到指定的文件)

我在读取下面的类创建的XML文件时遇到问题。我相信这里有一些属性需要设置,以便使用正确的文件目录。使用以下命令生成的XMLfile:

<?xml version="1.0" encoding="WINDOWS-1252"?>
<!DOCTYPE log SYSTEM "logger.dtd">

如果删除包含“logger.dtd”的行,则可以读取顶部的。有人能解释一下发生了什么事吗?我正在读取使用SAXParser API设置的同一URI。我按照这里的SAX解析说明进行:http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/

    package logging;

    import java.io.IOException;
    import java.util.logging.FileHandler;
    import java.util.logging.Handler;
    import java.util.logging.Level;
    import java.util.logging.Logger;

    import java.util.logging.SimpleFormatter;
    import java.util.logging.XMLFormatter;

    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;

    import org.xml.sax.Attributes;
    import org.xml.sax.SAXException;
    import org.xml.sax.helpers.DefaultHandler;

    public class Log {
static private FileHandler fileTxt;
static private SimpleFormatter formatterTxt;
static private FileHandler fileXML;
static private XMLFormatter formatterXML;



static public void setup(Logger theLogger) throws IOException{
    Logger logger = theLogger;

    logger.setLevel(Level.ALL);

    fileTxt = new FileHandler("C:\\Temp\\logging.txt");
    fileXML = new FileHandler("C:\\Temp\\XMLLogging.xml");

    formatterTxt = new SimpleFormatter();
    fileTxt.setFormatter(formatterTxt);
    logger.addHandler(fileTxt);

    formatterXML = new XMLFormatter();
    fileXML.setFormatter(formatterXML);
    logger.addHandler(fileXML);

    for(Handler h: logger.getHandlers()){
        System.out.println(h.getFormatter());
    }


}

}

共 (2) 个答案

  1. # 1 楼答案

    ...at the top can be read if I remove the line containing "logger.dtd" . Can someone explain what is going on?

    引发此错误是因为SAX解析器默认为验证和加载外部DTD。如果不希望这样做,请禁用验证:

        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        SAXParser saxParser = factory.newSAXParser();
    

    SAX将不再查找DTD文件,也不会引发文件未找到异常

    否则,“logger.dtd”文件可以在3.0附录A:XMLFormatter输出的dtd下的Java Logging Overview中找到

    <!  DTD used by the java.util.logging.XMLFormatter  >
    <!  This provides an XML formatted log message.  >
    
    <!  The document type is "log" which consists of a sequence
    of record elements  >
    <!ELEMENT log (record*)>
    
    <!  Each logging call is described by a record element.  >
    <!ELEMENT record (date, millis, sequence, logger?, level,
    class?, method?, thread?, message, key?, catalog?, param*, exception?)>
    
    <!  Date and time when LogRecord was created in ISO 8601 format  >
    <!ELEMENT date (#PCDATA)>
    
    <!  Time when LogRecord was created in milliseconds since
    midnight January 1st, 1970, UTC.  >
    <!ELEMENT millis (#PCDATA)>
    
    <!  Unique sequence number within source VM.  >
    <!ELEMENT sequence (#PCDATA)>
    
    <!  Name of source Logger object.  >
    <!ELEMENT logger (#PCDATA)>
    
    <!  Logging level, may be either one of the constant
    names from java.util.logging.Level (such as "SEVERE"
    or "WARNING") or an integer value such as "20".  >
    <!ELEMENT level (#PCDATA)>
    
    <!  Fully qualified name of class that issued
    logging call, e.g. "javax.marsupial.Wombat".  >
    <!ELEMENT class (#PCDATA)>
    
    <!  Name of method that issued logging call.
    It may be either an unqualified method name such as
    "fred" or it may include argument type information
    in parenthesis, for example "fred(int,String)".  >
    <!ELEMENT method (#PCDATA)>
    
    <!  Integer thread ID.  >
    <!ELEMENT thread (#PCDATA)>
    
    <!  The message element contains the text string of a log message.  >
    <!ELEMENT message (#PCDATA)>
    
    <!  If the message string was localized, the key element provides
    the original localization message key.  >
    <!ELEMENT key (#PCDATA)>
    
    <!  If the message string was localized, the catalog element provides
    the logger's localization resource bundle name.  >
    <!ELEMENT catalog (#PCDATA)>
    
    <!  If the message string was localized, each of the param elements
    provides the String value (obtained using Object.toString())
    of the corresponding LogRecord parameter.  >
    <!ELEMENT param (#PCDATA)>
    
    <!  An exception consists of an optional message string followed
    by a series of StackFrames. Exception elements are used
    for Java exceptions and other java Throwables.  >
    <!ELEMENT exception (message?, frame+)>
    
    <!  A frame describes one line in a Throwable backtrace.  >
    <!ELEMENT frame (class, method, line?)>
    
    <!  an integer line number within a class's source file.  >
    <!ELEMENT line (#PCDATA)>
    

    您只需复制文本并将其放入名为“logger.dtd”的文件中,然后将其放入与xml日志文件相同的目录中。然后SAX解析器将能够找到文件,因为它存在

    您还可以通过搜索logging package summary找到指向此的链接

  2. # 2 楼答案

    虽然这是一个问题,但我遇到了这种情况,不得不努力寻找解决办法。我发现了这一点,如果它对其他人也有效的话

    @Override
    public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
     if (systemId.contains("log4j.dtd")) {
      return new InputSource(new StringReader(""));
     } else {
      return null;
     }
    }
    

    实现此方法并提及您的dtd名称,它应该毫无例外地继续进行