1

以下のクラスで作成された 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());
    }


}

}
4

2 に答える 2

-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 ファイルを検索しなくなり、ファイルが見つからないという例外をスローしなくなりました。

それ以外の場合、「logger.dtd」ファイルはJava Logging Overview3.0 Appendix A: DTD for XMLFormatter Output にあります。

<!-- 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を検索して、これへのリンクを見つけることもできます。

于 2014-04-18T20:27:59.680 に答える