I am trying to modify a xml file located in my apache tomcat directory.
I am using this code to modify:
String filePath = getServletContext().getRealPath("")
+ File.separator +"\\xml_server.xml";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new File(filePath));
nList = doc.getElementsByTagName("item");
Node menu = doc.getFirstChild();
Element item = doc.createElement("item");
menu.appendChild(item);
Element id = doc.createElement("id");
id.appendChild(doc.createTextNode(String.valueOf(nList.getLength()+2)));
item.appendChild(id);
Element name = doc.createElement("name");
name.appendChild(doc.createTextNode(fileName));
item.appendChild(name);
Element version = doc.createElement("version");
version.appendChild(doc.createTextNode("1"));
item.appendChild(version);
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(filePath));
transformer.transform(source, result);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Here is the error which I'm face while running this:
type Exception report
message Servlet execution threw an exception
description The server encountered an internal error that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Servlet execution threw an exception
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
java.lang.NoClassDefFoundError: org/w3c/dom/ElementTraversal
java.lang.ClassLoader.defineClass1(Native Method)
java.lang.ClassLoader.defineClass(Unknown Source)
java.security.SecureClassLoader.defineClass(Unknown Source)
java.net.URLClassLoader.defineClass(Unknown Source)
java.net.URLClassLoader.access$100(Unknown Source)
java.net.URLClassLoader$1.run(Unknown Source)
java.net.URLClassLoader$1.run(Unknown Source)
java.security.AccessController.doPrivileged(Native Method)
java.net.URLClassLoader.findClass(Unknown Source)
java.lang.ClassLoader.loadClass(Unknown Source)
java.lang.ClassLoader.loadClass(Unknown Source)
org.apache.xerces.parsers.AbstractDOMParser.startDocument(Unknown Source)
org.apache.xerces.impl.dtd.XMLDTDValidator.startDocument(Unknown Source)
org.apache.xerces.impl.XMLDocumentScannerImpl.startEntity(Unknown Source)
org.apache.xerces.impl.XMLVersionDetector.startDocumentParsing(Unknown Source)
org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
fr.studec.FileUploadServlet.updateXml(FileUploadServlet.java:131)
fr.studec.FileUploadServlet.doPost(FileUploadServlet.java:106)
javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)