JSP生成XML,写入到内存中去了?高分请教!
写了一个动态生成xml树形菜单的东西,现在每次登录进来,可以按角色动态加载xml节点,只是加载完后,可能写入到内存中了,下次登录进来后,这些节点又被重新加载一次,成双份的了!再登录,三份了!
这不是session,关掉浏览器,再开也一样,郁闷,怎么解决?
我试了以下办法:
doc.normalize(); 没用
doc.normalizeDocument(); 报错
db.reset(); 报错
谁知道怎么解决???
以下是code:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
//获取绝对路径
String path = application.getRealPath( "/ ");
Document doc = db.parse(path+ "left\\XML\\top.xml ");
//获取根节点
Element tree=doc.getDocumentElement();
//插入到最后一个节点前
Element TreeNode=doc.createElement( "TreeNode ");
NodeList myElement =tree.getElementsByTagName( "TreeNode ");
Element LastElement=(Element)myElement.item(myElement.getLength()-1);
tree.insertBefore(TreeNode,LastElement);
//赋属性
TreeNode.setAttribute( "Title ", "系统管理员 ");
TreeNode.setAttribute( "NodeId ", "msdnlib3 ");
TreeNode.setAttribute( "NodeXmlSrc ", "feng1.xml ");
//写回到xml内存文件中去
DOMSource in2=new DOMSource(doc);
File xmlFile=new File(path+ "left\\XML\\top.xml ");
StreamResult outs=new StreamResult(xmlFile);
Transformer transformer=TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes ");
transformer.transform(in2,outs);
------解决方案--------------------先把文件清空