Parsing a log4j configuration file



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

.



Relevant Pages

  • RE: XQuery using a Parameter with .nodes
    ... The workaround would be to build the entire SQL query dynamically ... There is no way to build the XPath expression from a variable like you did. ... in the XML datatype's methods. ... Declare @FormXMLXPath nvarchar, ...
    (microsoft.public.sqlserver.xml)
  • Re: text/string finder
    ... >> I'm new to xml and sometimes gives me a hardtime. ... >> that returns a particular string I'm looking for? ... > This is not a syntactically correct XPath expression. ... > containing the string 'foo', ...
    (microsoft.public.dotnet.xml)
  • Re: xml parser and working with prefix, name and attributes.
    ... semantics of xml. ... You should have been able to come up with the right XPath expression ... you're using the XmlDocument or XmlReader API, ... class can help you define namespaces used in the XPath expressions). ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How to use XPath expression to parse XML document and using XSL - i.e. do a selective XSLT
    ... convert that XPathNodeIterator back into an XPathNavigator, ... Dim doc As XmlDocument = New XmlDocument ... XPathNodeIterator) because an XML document ... and then inside of the xsl refer to this in the XPath expression like so: ...
    (microsoft.public.dotnet.xml)