使用dom4j遇到的问题

在使用dom4j时遇到了一些不符合习惯的处理API,浪费了不少时间。在此进行罗列,以便查阅:
1、xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<document xmlns:xhtml="http://www.w3.org/1999/xhtml">
<properties>
 <property name="document_lang">zh-cn</property>
</properties>
<properties>
 <property name="title">DocNew Test...</property>
</properties>
<newobjects/>
</document>
需求:取出title的值“DocNew Test...”;程序代码:
 String title= "";
 List lists= doc.selectNodes("/document/properties");      //doc为一Document对象
 if(lists != null){
  //for(int i=0; i<lists.size(); i++){
  Element properties = (Element)list.get(list.size()-1);   
  Element property = properties.selectSingNodes("/document/properties/property");
  if(list != null)
   title = property.getText();       
  ------
 }
 System.out.println(title);    
 以上程序输出的将是:zh-cn

       
 String title= "";
 List lists= doc.selectNodes("/document/properties/property");      //doc为一Document对象
 if(lists != null){
  for(int i=0; i<lists.size(); i++){
   Element ele = (Element)list.get(i);
   if(ele.attribute("name").getValue().equals("title"))
    title = ele.getText();
  }
 }
 System.out.println(title);  
 以上程序输出的将是:DocNew Test...

总结:第一段代码Element properties = (Element)list.get(list.size()-1);取的是第二个properties节点,程序到这里还没问题,但执行properties.selectSingNodes("/document/properties/property")与执行doc.selectSingNodes("/document/properties/property")都是从新编历搜索整个稳当, 结果是一样的,获得的都是节点<property name="document_lang">zh-cn</property>。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值