Parsing a log4j configuration file

T

thelemmings

Hello,

I've been trying to parse a log4j configuration file using
java.xml.xpath.XPath, to no avail.

The XML looks something like (shortened), I'm trying to get the value
attribute "log/Statistics.log", from the param tag:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="StatisticsFileAppender"
class="org.apache.log4j.FileAppender">
<param name="file" value="log/Statistics.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m%n"/>
</layout>
</appender>
</log4j:configuration>

The code I'm using follows, the problem is that statsLogFile is always
an empty string:

// Prevents the external DTD from being loaded (Daniel Lemire)
DocumentBuilderFactory dbfact = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dbfact.newDocumentBuilder();

// Reads the filename into which the statistics are recorded
XPathFactory factory = XPathFactory.newInstance();
XPath xPath = factory.newXPath();
Document inputSource = builder.parse(new FileInputStream(log4jConfig));
String statsLogFile =
xPath.evaluate("//log4j:configuration/appender[@name='StatisticsFileAppender']/param[@name='file']/@value", inputSource);
System.out.println("Statistics log file: " + statsLogFile);

What am I doing wrong? Is it ok to mention a namespace with a colon in
an XPath expression? I've used this code to get values from other XML
files (using other xpath expressions) and it works fine. But it just
won't work for the case I just exposed.

Thanks in advance for your help,
Luc
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

The XML looks something like (shortened), I'm trying to get the value
attribute "log/Statistics.log", from the param tag:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="StatisticsFileAppender"
class="org.apache.log4j.FileAppender">
<param name="file" value="log/Statistics.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m%n"/>
</layout>
</appender>
</log4j:configuration>
String statsLogFile =
xPath.evaluate("//log4j:configuration/appender[@name='StatisticsFileAppender']/param[@name='file']/@value", inputSource);

Try without log4j: !

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top