3
votes

I am trying to generate artifacts for log4j XML parsing using JAXB. I am using the dtd

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

and inlined log.xml. I am getting below exception. I am a newbie with all these tech. Please someone help with this issue.

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>
1

1 Answers

1
votes

UPDATE

The problem with my original answer was due to a formatting error in your question. Originally the root element was not visible (corrected now), so I thought you had provided 7 sample documents instead of one document with 7 sub elements :).

The log4j.dtd defines element names with a colon in them, this is confusing the JAXB implementation if its underlying parser is set to namespace aware since it thinks that the element log4j:configuration is really an element named configuration in the namespace http://jakarta.apache.org/log4j/.

The solution is to force the JAXB implementation to use a non-namespace-aware parser:

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

}

Here is what I did that worked:

Obtain log4j.dtd

From the log4j download I noticed that there were 3 log4j.dtd files, the one I used was in:

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

Run XJC

Since we are using XJC on a DTD file and not an XML schema we need to specify the -dtd flag:

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

Create the JAXBContext

Next I create the JAXBContext on the log4j package that I used XJC to generate:

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

Demo Code

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