JSP中读取XML的问题
WebRoot有XML文件:DataForm.xml在一个类中读取该XML文件
得到DOM;
public void linkDocument() throws ParserConfigurationException,
SAXException, IOException {
fatory = DocumentBuilderFactory.newInstance();
buidler = fatory.newDocumentBuilder();
url = this.getClass().getResource("").getPath().replaceAll("%20", " ");
path = url.substring(0, url.indexOf("WEB-INF"))
+ "WEB-INF/DataForm.xml";
document = buidler.parse(path);
}
修改方法:
public void updateId(String elemenStr, String idValue) {
try {
linkDocument();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Element element = (Element) document.getElementsByTagName(elemenStr)
.item(0);
NodeList list = (NodeList) element.getElementsByTagName("list");
Element lastID = (Element) list.item(list.getLength() - 1);
lastID.setTextContent(idValue);
finishDocument();
}
转义存档:
public void finishDocument() {
Element element = (Element) document.getElementsByTagName("OrderForm").item(0);
NodeList list = (NodeList) element.getElementsByTagName("list");
Element lastID = (Element) list.item(list.getLength() - 1);
System.out.println(lastID.getTextContent());
try {
transform = TransformerFactory.newInstance();
trans = transform.newTransformer();
DOMSource source = new DOMSource(document);
StreamResult result = null;
result = new StreamResult(new FileOutputStream(path));
trans.transform(source, result);
} catch (TransformerConfigurationException e) {
e.printStackTrace();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (TransformerException e) {
e.printStackTrace();
}
}
}
比如: BuyFormImpl ipmle=new BuyFormImpl(); ipmle.updateIDlist("OrderForm","10009");
在单元测试下能修改成功,但是在JSP服务器的类即WEB-INF/class下的类能读取改文件,但是无法修改,是怎么回事 JSP AJAX
补充:Java , Web 开发