導航:首頁 > 編程語言 > javaxml保存

javaxml保存

發布時間:2023-03-24 02:52:15

A. 如何用java實現對xml文件的讀取和寫入以及保存

直接附源碼import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;import org.dom4j.*;
import org.dom4j.io.XMLWriter;
public class Dom4jSample { public static void main(String[] args) {
Dom4jSample dom4jSample = new Dom4jSample();
Document document = dom4jSample.createDocument();
try{
dom4jSample.FileWrite(document);

Document documentStr = dom4jSample.StringToXML("<China>I Love!</China>");
dom4jSample.XMLWrite(documentStr);

Element legend = dom4jSample.FindElement(document);
System.out.println(legend.getText());
}
catch(Exception e)
{

}
}

/*
* Create a XML Document
*/
public Document createDocument()
{
Document document = DocumentHelper.createDocument();

Element root = document.addElement("root");

Element author1 = root.addElement("Lynch");
author1.addAttribute("Age","25");
author1.addAttribute("Country","China");
author1.addText("I am great!");

Element author2 = root.addElement("Legend");
author2.addAttribute("Age","25");
author2.addAttribute("Country","China");
author2.addText("I am great!too!");

return document;
}

/*
* Create a XML document through String
*/
public Document StringToXML(String str) throws DocumentException
{
Document document = DocumentHelper.parseText(str);
return document;
}
public Element FindElement(Document document)
{
Element root = document.getRootElement();
Element legend = null;
for(Iterator i=root.elementIterator("legend");i.hasNext();)
{
legend = (Element)i.next();
}
return legend;
}

/*
* Write a XML file
*/
public void FileWrite(Document document) throws IOException
{
FileWriter out = new FileWriter("C:/Dom2jSample.xml");
document.write(out);
out.close();
}

/*
* Write a XML format file
*/
public void XMLWrite(Document document) throws IOException
{
XMLWriter writer = new XMLWriter(new FileWriter("C:/Dom2jSampleStr.xml"));
writer.write(document);
writer.close();
}
}

B. java可以用xml存儲數據嗎

可以到是可以
但是如果存儲的是正兒八經的數據的話
建議你用ACCESS
XML用來做配置文件倒是蠻合適的

閱讀全文

與javaxml保存相關的資料

熱點內容
如何緩解壓力開闊心理 瀏覽:661
解壓舞dj版動漫 瀏覽:942
編程模擬下雨 瀏覽:315
台式機如何改成伺服器 瀏覽:69
單片機超聲波程序 瀏覽:538
益陽微商分銷源碼咨詢客服 瀏覽:289
模塊化設計面向抽象編程 瀏覽:386
python訓練技巧 瀏覽:563
悅翔更換壓縮機軸承 瀏覽:1
加密格式圖片 瀏覽:555
什麼app注冊可以有現金 瀏覽:464
linuxperl命令替換 瀏覽:346
如何調試匯編程序 瀏覽:780
安卓機玩對峙二要需要准備什麼 瀏覽:48
sd卡解壓不了 瀏覽:781
毝38毝nn9翲源碼 瀏覽:25
mobi格式pdf 瀏覽:292
php游戲編程 瀏覽:862
excel開文件夾數量設置 瀏覽:25
為什麼玩游戲連不到伺服器 瀏覽:300