java中用jdom 如何创建xml文档/将数据写入XML中
JDomOutput.java代碼如下
----------------------------------------------------------------------------------------------------------------------
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.jdom.Attribute;
import org.jdom.Comment;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class JDomOutput
{
?? ?public static void main(String[] args) throws IOException
?? ?{
?? ??? ?//創(chuàng)建文檔
?? ??? ?Document document = new Document();
?? ??? ?//創(chuàng)建根元素
?? ??? ?Element people = new Element("people");
?? ??? ?//把根元素加入到document中
?? ??? ?document.addContent(people);?
?? ??? ?
?? ??? ?//創(chuàng)建注釋
?? ??? ?Comment rootComment = new Comment("將數(shù)據(jù)從程序輸出到XML中!");
?? ??? ?people.addContent(rootComment);
?? ??? ?
?? ??? ?//創(chuàng)建父元素
?? ??? ?Element person1 = new Element("person");
?? ??? ?//把元素加入到根元素中
?? ??? ?people.addContent(person1);
?? ??? ?//設(shè)置person1元素屬性
?? ??? ?person1.setAttribute("id", "001");
?? ??? ?
?? ??? ?Attribute person1_gender = new Attribute("gender", "male");
?? ??? ?person1.setAttribute(person1_gender);
?? ??? ?
?? ??? ?Element person1_name = new Element("name");
?? ??? ?person1_name.setText("劉德華");
?? ??? ?person1.addContent(person1_name);
?? ??? ?
?? ??? ?Element person1_address = new Element("address");
?? ??? ?person1_address.setText("香港");
?? ??? ?person1.addContent(person1_address);
?? ??? ?
?? ??? ?
?? ??? ?Element person2 = new Element("person");
?? ??? ?people.addContent(person2);
?? ??? ?
?? ??? ?person2.setAttribute("id", "002").setAttribute("gender","male");//添加屬性,可以一次添加多個屬性
?? ??? ?
?? ??? ?Element person2_name = new Element("name");
?? ??? ?person2_name.setText("林志穎");
?? ??? ?person2.addContent(person2_name);
?? ??? ?
?? ??? ?Element person2_address = new Element("address");
?? ??? ?person2_address.setText("臺灣");
?? ??? ?person2.addContent(person2_address);
?? ??? ?
?? ??? ?
?? ??? ?//設(shè)置xml輸出格式
?? ??? ?Format format = Format.getPrettyFormat();
?? ??? ?format.setEncoding("utf-8");//設(shè)置編碼
?? ??? ?format.setIndent("??? ");//設(shè)置縮進(jìn)
?? ??? ?
?? ??? ?
?? ??? ?//得到xml輸出流
?? ??? ?XMLOutputter out = new XMLOutputter(format);
?? ??? ?//把數(shù)據(jù)輸出到xml中
?? ??? ?out.output(document, new FileOutputStream("jdom.xml"));//或者FileWriter
?? ??? ?
?? ?}
}
---------------------------------------------------------------------------------------------------------------------------
生成的xml內(nèi)容如下:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<people>
??? <!--將數(shù)據(jù)從程序輸出到XML中!-->
??? <person id="001" gender="male">
??????? <name>劉德華</name>
??????? <address>香港</address>
??? </person>
??? <person id="002" gender="male">
??????? <name>林志穎</name>
??????? <address>臺灣</address>
??? </person>
</people>
總結(jié)
以上是生活随笔為你收集整理的java中用jdom 如何创建xml文档/将数据写入XML中的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql如何复制表结构,包括主键,索引
- 下一篇: 如何得到juniper的pe ip de