JiveJdon Community Forums
在线237人 Home | 论坛 | 培训咨询 | 精华 | 查搜 | 注册 | 登陆 |
首页 » 论坛 » J2EE/JavaEE/JEE/EJB/JSF等技术讨论
???en_US.forumThreadPrev.name??? 上一主题
Go back to the topic listing   返回主题列表
???en_US.forumThreadNext.name??? 下一主题
这个主题共有 2 回复 / 1 页 [ ]  发表新帖子  回复该主题贴
perfectwxj

发表文章: 19
注册时间: 2006年06月28日 15:36
给他发消息
请教高手关于解析xml文件的问题 急~~ 发表: 2006年11月08日 09:24 回复
第一个jsp页面有一个浏览框,选择要上传的xml文件,提交到第二个jsp页面,
在第二个jsp页面中解析,下面是第二个jsp页面的解析代码,现在的问题是一直报空指针异常,
在Document document= builder.parse(new File(file)); 这一行 ,确定从页面可以得到xml文件的路径,如下:C:/Documents and Settings/dell/桌面/s.xml
请问这是什么原因
request.setCharacterEncoding("gb2312");
String file=request.getParameter("myfile");
file=file.replace('\\','/');
System.out.println("===="+file);
String plan_company="";
String plan_name="";
String remark="";
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
//解析XML文件
if (file==null){
System.out.println("null");
}
Document document= builder.parse(new File(file));
//获取根节点
Element root=document.getDocumentElement();
//获取所有的"Configuration"标记,它是一个NodeList
NodeList Configuration=root.getElementsByTagName("plan");
for(int i=0;i<Configuration.getLength();i++){
//获取Configuration的每一个元素
Element Configurations=(Element)Configuration.item(i);
plan_company=(String)Configurations.getElementsByTagName("plan_company").item(0).getFirstChild().getNodeValue();
//System.out.println("11111111"+plan_company);
plan_name=(String)Configurations.getElementsByTagName("plan_name").item(0).getFirstChild().getNodeValue();
//System.out.println("11111111"+plan_name);
remark=(String)Configurations.getElementsByTagName("remark").item(0).getFirstChild().getNodeValue();
}
out.println("plan_company="+plan_company);
out.println("plan_name"+plan_company);
out.println("remark"+remark);
banq

发表文章: 9074
注册时间: 2002年08月03日 17:08
给他发消息
Re: 请教高手关于解析xml文件的问题 急~~ 发表: 2006年11月08日 11:34 回复
在Jsp中嵌入Java代码,出错就报空指针,神仙都难帮你找出错误。

请使用MVC模式,将Java代码从jsp中清楚干净,再去调试你的Java代码。
perfectwxj

发表文章: 19
注册时间: 2006年06月28日 15:36
给他发消息
Re: 请教高手关于解析xml文件的问题 急~~ 发表: 2006年11月08日 16:30 回复
我这个是做测试用的,自己写的一个小东西,就想看看能不能解析出xml文件。
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
//解析XML文件
Document document=builder.parse(files);
//获取根节点
Element root=document.getDocumentElement();
NodeList Configuration=root.getElementsByTagName("plan");
for(int i=0;i<Configuration.getLength();i++){
Element Configurations=(Element)Configuration.item(i);
//NodeList list=Configurations.getChildNodes();
//for(int j=0;j<Configurations.;j++){
plan_company=Configurations.getAttribute("plan_company");
//plan_company=(String)Configurations.getElementsByTagName("value").item(j).getFirstChild().getNodeValue();
out.println("plan_company="+plan_company);//}
}

用 plan_company=Configurations.getAttribute("plan_company");
这个方法得不到节点值,不知道是什么原因。下面是xml文件

<?xml version="1.0" encoding="GB2312"?>

<issue>
<plan name1="plan">
<member name1="plan_company">
<value>限公司</value>
</member>
<member name1="plan_name">
<value>节目单 </value>
</member>
<member name1="remark">
<value>很多好看的节目啊!</value>
</member>
</plan>
</issue>
这个主题有 2 回复 / 1 页 [ ]
???en_US.forumThreadPrev.name??? 上一主题
Go back to the topic listing   返回主题列表    返回页首  返回页首
???en_US.forumThreadNext.name??? 下一主题
热点TAG: AOP cache DDD EJB 集群 设计模式 Hibernate IOC JiveJdon OO RBAC Spring Struts
查询本论坛内 回复超过的热门帖子
快速发表回复
标题
 
粗体 斜体 下划线 插入图片 插入代码 插入url链接 插入附件
内容
 

解惑之道在J道 ,打造中国最具影响力的的企业软件社区
OpenSource JIVEJDON v3.0 Powered by JdonFramework Code © 2002-08 jdon.com

anti spam