dom4j读取XML数据问题

cs_kill_you 2008-11-17 09:46:37
大家好,我想用DOM4J读取下面的XML文档的那些中文的信息,但我这样子做却不能实现,哪位高手能帮我解决一下啊.

<?xml version="1.0" encoding="UTF-8"?>
<report xmlns="http://www.eclipse.org/birt/2005/design" version="3.2.15" id="1">
<property name="author">陈</property>
<property name="createdBy">上海XX科技有限公司</property>
<text-property name="title">常用查询</text-property>
<html-property name="description">这是我的个人报表。</html-property>


我是这样子来读取的
Map request = (Map)ActionContext.getContext().get("request");

SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new File("c:/test.rptdesign"));

list = document.selectNodes("/report/property/@name");
Iterator it = list.iterator();
while(it.hasNext()){
Attribute attribute = (Attribute)it.next();
if(attribute.getValue().equals("author")){
Element element = (Element)it.next();
request.put("reportauthor", element.getText());
}
...全文
150 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
llliiiuuu 2008-11-18
  • 打赏
  • 举报
回复
selectSingleNode("report/property[@name='author']").getText()=陈
selectSingleNode("report/property[@name='createdBy']").getText()=上海XX科技有限公司
selectSingleNode("report/text-property[@name='title']").getText()=常用查询
selectSingleNode("report/html-property[@name='description']").getText()=这是我的个人报表。
boboo_2000_0 2008-11-17
  • 打赏
  • 举报
回复
支持2楼的说法,楼主要得到的是节点的文本值,而不是节点的属性值.
zidasine 2008-11-17
  • 打赏
  • 举报
回复
可以用list = document.selectNodes("/report/property[@name]");
获得包括name属性的property的Element 集合进行操作
zidasine 2008-11-17
  • 打赏
  • 举报
回复
          if(attribute.getValue().equals("author")){ 
Element element = (Element)it.next();
request.put("reportauthor", element.getText());
}


你的list里放的是所有name 的Attribute 怎么可能得到Element

67,537

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧