xml文件处理的好工具--产生xml文档篇--cnjsp.org

http://www.cnjbb.org/thread.jsp?boardid=46&threadid=43597&pageno=1&perpage=10&mode=block
在这里会让你们更快的学习java!不但定期给出好的教程,而且有java牛人给你制定计划和回答问题!cnjsp.org很好!

本文继续介绍jdom产生xml文件的简单使用方法:
(1)先用jdom产生Document对象,在用下面的代码写入文件中(如文件放在c:/car.xml中):
public void writeToFile(Document doc,String filePath){
  try{
   org.jdom.output.XMLOutputter outputter= new org.jdom.output.XMLOutputter();
            outputter.output(doc, System.out);
     FileWriter writer = new FileWriter(filePath);//filePath=c:/car.xml
            outputter.output(doc, writer);
            writer.close();
  }catch(java.io.IOException e){
            e.printStackTrace();
        }
 }
(2)下面产生一个如下简单的xml的Document对象:
<?xml version="1.0" encoding="UTF-8"?>
<car vin="123">
    <make>Toyota</make>
  <model>Celica</model>
  <year>1997</year>
  <color>green</color>
  <license state="CA">1ABC234</license>
</car>
创建一个 Document:
Element car= new Element("car");
Document doc= new Document(car);
在在这里面加Element就是了!
添加一个 Attribute:
car.addAttribute(new Attribute("vin", "123"));
元素和子元素:
Element make = new Element("make");
make.addContent("Toyota");
car.addContent(make);
添加其余的元素:
car.addContent(new Element("model").addContent("Celica"));
car.addContent(new Element("year").addContent("1997"));
car.addContent(new Element("color").addContent("green"));
car.addContent(new Element("license").addContent("1ABC234").addAttribute("state","CA"));

jdom是不是很简单啊!!!



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值