Dom4j 使用简介【转】
作者:冰云 icecloud(AT)sina.com时间:2003.12.15?版权声明:本文由冰云完成,首发于CSDN,未经许可,不得使用于任何商业用途。文中代码部分引用自while(iter.hasNext()){????????????? Attribute attribute = (Attribute)iter.next();????????????? if(attribute.getValue().equals("yes")){????????????????? attribute.setValue("no");????????????? }?? ?????????? }?????????? ?????????? /**?????????? ?* 修改内容之二: 把owner项内容改为Tshinghua?????????? ?* 并在owner节点中加入date节点,date节点的内容为2004-09-11,还为date节点添加一个属性type?????????? ?*/?????????? list = document.selectNodes("/books/owner" );?????????? iter = list.iterator();?????????? if(iter.hasNext()){????????????? Element ownerElement = (Element)iter.next();????????????? ownerElement.setText("Tshinghua");????????????? Element dateElement = ownerElement.addElement("date");????????????? dateElement.setText("2004-09-11");????????????? dateElement.addAttribute("type","Gregorian calendar");?????????? }?????????? ?????????? /** 修改内容之三: 若title内容为5. 格式化输出和指定编码?默认的输出方式为紧凑方式,默认编码为UTF-8,但对于我们的应用而言,一般都要用到中文,并且希望显示时按自动缩进的方式的显示,这就需用到OutputFormat类。?
- XMLWriter?writer; ?? ????????try?{ ?? ????????????OutputFormat?format?=?OutputFormat.createPrettyPrint(); ?? ????????????//format.setEncoding("UTF-8"); ?? ????????????FileOutputStream?fos?=?new?FileOutputStream(xmlFile); ?? ????????????//writer?=?new?XMLWriter(new?FileWriter(xmlFile),?format); ?? ????????????writer?=?new?XMLWriter(fos,?format); ?? ????????????writer.write(document); ?? ????????????writer.close(); ?? ????????}?catch?(IOException?e)?{ ?? ????????????//?TODO?自动生成?catch?块 ?? ????????????logger.error("修改xml文件失败!"); ?? ????????????e.printStackTrace(); ?? ????????}???
?
?
?
?
?