以下程序是在jsp里写的修改和读取xml的程序,基于dom的。程序能正常运行和显示,但是为什么xml文件没有被修改?
因为要做设计,所以学了点儿xml,基本原理懂了,可是还不太会用。是不是我没有设置数据源的事,这个xml写的时候有什么要求么?为什么能运行,但是不能被修改呢??大家帮忙看一下,是哪里错了吗?谢谢了!我用jdom也写了代码,也是可以运行和读取,但就是修改不了xml源文件~~ <%
//Dom修改xml
InputStream is = pageContext.getServletContext().getResourceAsStream("ChatMsg/chat.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//忽略元素中的空格
dbf.setIgnoringElementContentWhitespace(true);
//建立解析器
DocumentBuilder db = dbf.newDocumentBuilder();
//获取文档对象
Document doc = db.parse(is);
doc.normalize();
String toa="aa";
String contenta="xxi";
Element to=doc.createElement("to");
Text textto=doc.createTextNode(toa);
to.appendChild(textto);
Element content=doc.createElement("content");
Text textcontent=doc.createTextNode(contenta);
content.appendChild(textcontent);
Element msg=doc.createElement("msg");
msg.appendChild(to);
msg.appendChild(content);
Element chatmsg=(Element)doc.getElementsByTagName("chatmsg").item(0);
chatmsg.appendChild(msg);
//获取节点列表
NodeList msglist = doc.getElementsByTagName("msg");
//获取节点下的所有数据列表
NodeList toList = doc.getElementsByTagName("to");
NodeList contentList = doc.getElementsByTagName("content");
//通过循环遍历所有nlist列表中的数据
for (int i = 0; i < msglist.getLength(); i++) {
out.println(toList.item(i).getFirstChild().getNodeValue());
out.println(contentList.item(i).getFirstChild().getNodeValue());
}
try{
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
//获得要保存的文档对象源和要保存的位置
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("ChatMsg/chat.xml"));
transformer.transform(source,result);
}catch(TransformerConfigurationException tce){
tce.printStackTrace();
}catch(TransformerException te){
te.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
%>xml文件如下: <?xml version="1.0" encoding="UTF-8" ?>
- <chatmsg>
- <msg>
<to>xiaona</to>
<content>i love you</content>
</msg>
- <msg>
<to>rrrr</to>
<content>hehe</content>
</msg>
- <msg>
<to>all</to>
<content>真好!</content>
</msg>
- <msg>
<to>all</to>
<content>你好!</content>
</msg>
</chatmsg>
解决方案 »
- 怎样将上传的图片文件和表单其它数据一起提交
- 向服务器端webservice,发送几十M的数据
- 关于Java File Delete
- strues2上传文件时,如何创建文件的保存路径
- 大侠请进
- 引用的简单问题求教解答,谢谢
- DAO模式是什么?
- 在服务器上运行portal,应该安装weblogic server还是weblogic portal还是platform?
- 用java编写笛卡尔积运算
- hibernate能不能 显式禁止多态查询
- struts2 java.lang.NullPointerException 错误 请教////////////////////////
- Hibernate报错Could not parse mapping document from resource
请在准备修改之前,先关闭is这个流。
呵呵!这个好像是网站的虚拟目录和我存储的xml文件的路径有关,可是该怎么修改呢?
我在项目下建的xml和实际读取的xml不是一个xml文件了,这个该怎么办呢?
怎么建立相应的映射呢?
这个网站的路径问题可真闹心,每次都映射来映射去的~~~
唉,原来错误这么简单,让我郁闷了一天了~~~
还看了好多Jdom的东东,都把我看转向了~~~~~
你把:
pageContext.getServletContext().getResourceAsStream("ChatMsg/chat.xml");
改成:
pageContext.getServletContext().getResourceAsStream("/ChatMsg/chat.xml");然后在WEB-INF/classes目录下创建ChatMsg目录,用于保存chat.xml。
在开发环境下,一般你的chat.xml应该在src/ChatMsg/目录下,程序才能读到,
而项目部署到tomcat后,应该就在WEB-INF/classes/ChatMsg/目录下。因为getResourceAsStream找的是classpath。
我把chat.xml放到classes的目录下,然后用
String addr=this.getClass().getResource("/ChatMsg/chat.xml").getFile()
读取classes的路径,呵呵,能行了!
谢谢你关注我的帖子,哈哈!
那个啥,用jsp做网站还是第一次,以前都是用asp.net(c#)做的,觉得没有这么麻烦,呵呵!
问点儿白痴的问题不要笑我~~
反正应该是读写目录一样了,大家自己调试吧,可能每个人不一样,我放到classes下也不行~~
不过现在可以了,就是改一下路径~~