gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:16:51 26 4
gpt4 key购买 nike

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

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

和内联的 log.xml。我低于异常。我是所有这些技术的新手。请有人帮助解决这个问题。

Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"http://jakarta.apache.org/log4j/", local:"configuration"). Expected elements are <{}appender>,<{}appender-ref>,<{}category>,<{}categoryFactory>,<{}connectionSource>,<{}dataSource>,<{}errorHandler>,<{}filter>,<{}layout>,<{}level>,<{}logger>,<{}logger-ref>,<{}loggerFactory>,<{}param>,<{}plugin>,<{}priority>,<{}renderer>,<{}rollingPolicy>,<{}root>,<{}root-ref>,<{}triggeringPolicy> at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:558) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:211) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:206) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:83) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:965) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:401) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:382) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:113) at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown Source) at org.apache.xerces.impl.dtd.XMLDTDValidator.startElement(Unknown Source) at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source) at org.apache.xerces.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(Unknown Source) at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source) at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source) at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) at org.apache.xerces.parsers.XMLParser.parse(Unknown Source) at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source) at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:202) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:175) at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:148) at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:153) at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:162) at 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>

最佳答案

更新

我原来的答案的问题是由于你的问题中的格式错误。最初根元素不可见(现已更正),所以我认为您提供了 7 个示例文档,而不是一个包含 7 个子元素的文档:)。

log4j.dtd 定义了带有冒号的元素名称,如果其底层解析器设置为命名空间感知,这会混淆 JAXB 实现,因为它认为元素 log4j:configuration 实际上是一个命名空间 http://jakarta.apache.org/log4j/ 中名为 configuration 的元素。

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

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

}

以下是我所做的工作:

获取log4j.dtd

来自log4j download我注意到有 3 个 log4j.dtd 文件,我使用的那个在:

<log4j_home>/source/main/resources/org/apache/log4j/xml

运行 XJC

因为我们在 DTD 文件而不是 XML 模式上使用 XJC,所以我们需要指定 -dtd 标志:

xjc -p log4j -d out -dtd log4j.dtd

创建 JAXBContext

接下来,我在使用 XJC 生成的 log4j 包上创建 JAXBContext:

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

演示代码

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class Demo {

public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance("log4j");

processXML(jc,"src/log4j/file1.xml");
processXML(jc,"src/log4j/file2.xml");
processXML(jc,"src/log4j/file3.xml");
processXML(jc,"src/log4j/file4.xml");
processXML(jc,"src/log4j/file5.xml");
processXML(jc,"src/log4j/file6.xml");
processXML(jc,"src/log4j/file7.xml");
}

private static void processXML(JAXBContext jc, String fileName) throws Exception {
Unmarshaller unmarshaller = jc.createUnmarshaller();
Object o = unmarshaller.unmarshal(new File(fileName));

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

关于java - 通过 JAXB 生成的工件无法解析 log.xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6053256/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com