1

我无法读取由下面的类创建的 XML 文件。我被引导相信这里有一些属性我需要设置才能使用正确的文件目录。生成的 XML 文件:

<?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());
    }


}

}
4

2 回答 2

0

尽管这个问题已经有一段时间了,但是,我遇到了这种情况,不得不努力寻找解决方案。我发现了这个,也许它也适用于其他人

@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 名称,它应该毫无例外地继续进行。

于 2018-04-04T21:22:08.227 回答
-1

...如果我删除包含 "logger.dtd" 的行,可以读取顶部的内容。有人可以解释发生了什么吗?

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

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

SAX 将不再查找 DTD 文件,并且不会抛出未找到文件的异常。

否则,可以在3.0 附录 A:XMLFormatter 输出的 DTD下的Java 日志记录概述中找到“logger.dtd”文件。

<!-- 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 解析器将能够找到文件,因为它存在。

您还可以通过搜索日志记录包摘要找到指向此内容的链接。

于 2014-04-18T20:27:59.680 回答