加入收藏 | 设为首页 | 会员中心 | 我要投稿 | RSS
您当前的位置:首页 > 阅读中心 > 软件学习

用jdom????建xml文件

时间:2014-05-04 10:30:04  来源:JSP天空网  作者:未知
一句??,jdom的好??就是????.

?@是我??要建的xml:

<?xml version="1.0" encoding="UTF-8"?>
<car vin="123fhg5869705iop90">
<!--Description of a car-->
<make>Toyota</make>
<model>Celica</model>
<year>1997</year>
<color>green</color>
<license state="CA">1ABC234</license>
</car>

???浜??访矗?let us go!

第一步,??建根元素

Element carElement = new Element("car");
Document myDocument = new Document(carElement);

因??一?? XML 文?n必??一直有一??唯一的根元素,所以 Document ?? Element 放在它的??造器中。

第二步,添加一?? Attribute

carElement.addAttribute(new Attribute("vin", "123fhg5869705iop90"));

第三步,添加其他元素

carElement.addContent(new Element("make").addContent("Toyota"));

carElement.addContent(new Element("model").addContent("Celica"));
carElement.addContent(new Element("year").addContent("1997"));
carElement.addContent(new Element("color").addContent("green"));
carElement.addContent(new Element("license").addContent("1ABC234").addAttribute("state", "CA"));

第四步,添加一?l?]??

carElement.addContent(new Comment("Description of a car"));

第五步,?x一??子元素

Element yearElement = carElement.getChild("year");

第六步,?h除子元素

boolean removed = carElement.removeChild("year");

第七步,存?P

FileWriter writer = new FileWriter("/some/directory/myFile.xml");
outputter.output(myDocument, writer);
writer.close(); 
来顶一下
返回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
推荐资讯
后牧工业区厂房1500平出租
后牧工业区厂房1500平
现实的童话公主艾米·罗森
现实的童话公主艾米·
科比专为大场面而生
科比专为大场面而生
“最美清洁工”原是《赤壁》宫女
“最美清洁工”原是《
相关文章
    无相关信息
栏目更新
栏目热门