通过JAXB生成的工件无法解析log.xml

时间:2011-05-19 02:52:19

标签: java jaxb log4j

我正在尝试使用log4jJAXB XML解析生成工件。我正在使用dtd

  

http://logging.apache.org/log4j/1.2/apidocs/org/ap.../log4j/xml/doc-files/log4j.dtd

并内联log.xml。我正在低于例外。我是所有这些技术的新手。请有人帮忙解决这个问题。

  

线程“main”中的异常   javax.xml.bind.UnmarshalException:   意外的元素   (URI: “http://jakarta.apache.org/log4j/”   本地: “配置”)。预期   元素是   < {}附加器>,< {}附加器-REF>,< {}类别>,< {} categoryFactory>,< {} connectionSource>,< {}的dataSource>,< {}的ErrorHandler> ,< {}滤光器>,< {}布局>,< {}水平>,< {}记录器>,< {}记录器-REF>,< {}的LoggerFactory>,< {} PARAM&GT ;,< {}插件>,< {}优先>,< {}渲染>,< {} rollingPolicy>,< {}根>,< {}根REF>,< {} triggeringPolicy>   在   com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:558)   在   com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:211)   在   com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:206)   在   com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:83)   在   com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext $ DefaultRootLoader.childElement(UnmarshallingContext.java:965)   在   com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:401)   在   com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:382)   在   com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:113)   在   org.apache.xerces.parsers.AbstractSAXParser.startElement(未知   来源)at   org.apache.xerces.impl.dtd.XMLDTDValidator.startElement(未知   来源)at   org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(未知   来源)at   org.apache.xerces.impl.XMLNSDocumentScannerImpl $ NSContentDispatcher.scanRootElementHook(未知   来源)at   org.apache.xerces.impl.XMLDocumentFragmentScannerImpl $ FragmentContentDispatcher.dispatch(未知   来源)at   org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(未知   来源)at   org.apache.xerces.parsers.XML11Configuration.parse(未知   来源)at   org.apache.xerces.parsers.XML11Configuration.parse(未知   来源)at   org.apache.xerces.parsers.XMLParser.parse(未知   来源)at   org.apache.xerces.parsers.AbstractSAXParser.parse(未知   来源)at   org.apache.xerces.jaxp.SAXParserImpl $ JAXPSAXParser.parse(未知   来源)at   com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:202)   在   com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:175)   在   javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:148)   在   javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:153)   在   javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:162)   在   javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:180)

log4jXML

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="false" xmlns:log4j="http://jakarta.apache.org/log4j/">

    <appender name="ASYNC" class="org.apache.log4j.AsyncAppender">
        <appender-ref ref="DEFAULT"/>
    </appender>


    <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d [%t] %-5p %c %x- %m%n"/>
        </layout>
    </appender>

    <appender name="DEFAULT" class="org.apache.log4j.RollingFileAppender">
        <param name="File"   value="/home/default.log" />
        <param name="Append" value="true" />
        <param name="maxFileSize" value="10MB" />
        <param name="maxBackupIndex" value="2" />
        <param name="encoding" value="UTF-8" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d [%t] %-5p %c %x- %m%n"/>
        </layout>
    </appender>


    <appender name="DB_EXCEP" class="org.apache.log4j.FileAppender">
        <param name="File"   value="/home/db_exception.log" />
        <param name="Append" value="true" />
        <param name="encoding" value="UTF-8" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d [%t] %-5p %c %x- %m%n"/>
        </layout>
    </appender>

    <category name="com.common.db.ScriptRunner" additivity="false">
        <priority value="info" />
        <appender-ref ref="DB_EXCEP" />
    </category>


    <category name="com.common.appsvr.current.admin" additivity="false">
        <priority value="info"/>
        <appender-ref ref="DEFAULT"/>
    </category>

    <!-- ROOT CATEGORY -->
    <root>
        <priority value="debug"/>
        <appender-ref ref="DEFAULT"/>
        <!-- <appender-ref ref="STDOUT"/>  -->
    </root>

</log4j:configuration>

1 个答案:

答案 0 :(得分:1)

<强>更新

原始答案的问题是由于您的问题中的格式错误。最初根元素不可见(现在更正),所以我认为你提供了7个样本文档而不是一个带有7个子元素的文档:)。

log4j.dtd定义了带冒号的元素名称,如果它的底层解析器设置为名称空间感知,这会使JAXB实现混乱,因为它认为元素log4j:configuration实际上是一个名为{{1的元素在命名空间configuration中。

解决方案是强制JAXB实现使用非命名空间感知的解析器:

http://jakarta.apache.org/log4j/

以下是我的工作:

获取log4j.dtd

log4j download我注意到有3个log4j.dtd文件,我使用的是:

package log4j;

import java.io.FileInputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.UnmarshallerHandler;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

public class Demo {

    public static void main(String[] args) throws Exception {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();

        JAXBContext jc = JAXBContext.newInstance("log4j");
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        UnmarshallerHandler unmarshallerHandler = unmarshaller.getUnmarshallerHandler();
        xr.setContentHandler(unmarshallerHandler);

        FileInputStream xmlStream = new FileInputStream("src/log4j/file1.xml");
        InputSource xmlSource = new InputSource(xmlStream);
        xr.parse(xmlSource);

        Log4JConfiguration config = (Log4JConfiguration) unmarshallerHandler.getResult();

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(config, System.out);
    }

}

运行XJC

由于我们在DTD文件而不是XML模式上使用XJC,我们需要指定<log4j_home>/source/main/resources/org/apache/log4j/xml 标志:

-dtd

创建JAXBContext

接下来,我在使用XJC生成的xjc -p log4j -d out -dtd log4j.dtd 包上创建JAXBContext:

log4j

演示代码

JAXBContext jc = JAXBContext.newInstance("log4j");