Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » WebService/XML/JSON/SOAP/SOA  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Re:JDOM Beta 10 Release Candidate #1 [Re:Jove]
roson





发贴: 6
积分: 0
于 2004-12-02 22:09 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我也来贴贴,这是http://www.javaworld.com.tw的一个精华贴觉得不错噢
一句話,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 文檔必須一直有一個唯一的根元素,所以 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"));

第四步,添加一條註釋

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

第五步,讀一個子元素

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

第六步,刪除子元素

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

第七步,存盤

FileWriter writer = new FileWriter("/some/directory/myFile.xml");
outputter.output(myDocument, writer);
writer.close();




话题树型展开
人气 标题 作者 字数 发贴时间
11526 JDOM Beta 10 Release Candidate #1 Jove 8037 2004-02-10 15:39
9387 Re:JDOM Beta 10 Release Candidate #1 ditty 41 2004-02-10 19:23
9198 Re:JDOM Beta 10 Release Candidate #1 roson 1319 2004-12-02 22:09
10119 Re:JDOM Beta 10 Release Candidate #1 Jove 16 2004-12-03 09:50

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923