I am trying to remove an element and child elements from an xml file. Specifically appender with the name Testlogging.
First this is how my xml file looks.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="null" threshold="null">
<appender class="DailyLogFileAppender" name="Testlogging">
<param name="encoding" value="UTF-8"/>
<param name="MaxFileSize" value="100MB"/>
<param name="MaxBackupIndex" value="2"/>
<param name="rootDir" value="c:\Logs"/>
<param name="componentId" value="Testlogging"/>
<param name="DatePattern" value="yyyyMMdd"/>
<layout class="SyslogPatternLayout">
<param ConversionPattern="%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} [%-5p] {%t} %c [%D] - %m%n"/>
</layout>
</appender>
Here is my java code:
DocumentBuilderFactory dbfact = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dbfact.newDocumentBuilder();
Document d = builder.parse(xmlFile);
XPath xp = XPathFactory.newInstance().newXPath();
NodeList nl = (NodeList)xp.compile("//appender").evaluate(d,XPathConstants.NODESET);
for(int i = 0; i < nl.getLength(); i++)
{
if(xp.compile("./@name").evaluate(nl.item(i)).equals("Testlogging"))
{
Node node = nl.item(i);
node.removeChild(nl.item(i));
}
}
I would like to remove everything for this appender but an exception is being thrown. It's probably something simple I am missing.
Any ideas?