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