我正在制作一个Web应用程序,我需要创建一个日志。我将此日志存储为xml文件。
这些是我的路径:
log.xml - > org.codealizer.quizme.resources
Servlet - > org.codealizer.quizme.servlets
当我的应用程序运行时,它会给我一个成功的结果,但不会修改xml文件。
这是我修改xml文件的代码:
private void createLog(HttpServletRequest request,
HttpServletResponse response, PrintWriter out) {
String ipAddress = request.getRemoteAddr();
int port = request.getRemotePort();
String clientInformation = request.getHeader("user-agent");
try {
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder dBuilder = factory.newDocumentBuilder();
Document doc = dBuilder.parse(getClass().getResourceAsStream(
"/org/codealizer/quizme/resources/log.xml"));
doc.getDocumentElement().normalize();
Node log = doc.getFirstChild();
Element item = doc.createElement("item");
Element time = doc.createElement("time");
time.appendChild(doc.createTextNode("" + System.currentTimeMillis()));
item.appendChild(time);
Element access = doc.createElement("access");
access.appendChild(doc.createTextNode(getClass().getName()));
item.appendChild(access);
Element address = doc.createElement("address");
address.appendChild(doc.createTextNode(ipAddress + ":" + port));
item.appendChild(address);
Element client = doc.createElement("client");
client.appendChild(doc.createTextNode(clientInformation));
item.appendChild(client);
log.appendChild(item);
// Update
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(getClass().getResource(
"/org/codealizer/quizme/resources/log.xml").getFile());
// Output to console for testing
// StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
} catch (SAXException | IOException | ParserConfigurationException
| TransformerException e) {
out.println(e.getMessage());
}
这是我当前的log.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<log></log>
有人可以帮我解决这个问题吗?很多:)