導航:首頁 > 編程語言 > 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保存相關的資料

熱點內容
寧波程序員接私活小程序 瀏覽:491
小六壬pdf 瀏覽:13
怎麼通過快捷指令隱藏手機app 瀏覽:609
方舟手游如何辨別優質伺服器 瀏覽:116
pdf林徽因傳 瀏覽:527
國產區小電影網址 瀏覽:102
西班牙大尺度男男 瀏覽:609
消先的眼角膜電影 瀏覽:434
編譯原理課設項目簡介 瀏覽:921
日本《失樂園》無刪減 瀏覽:227
更新最快的電視劇網站 瀏覽:597
啟動app的廣告怎麼關 瀏覽:850
非sp文中的sp情節的小說 瀏覽:76
女主是被拐賣到大山的小說 瀏覽:858
哆啦a夢美人魚電影 瀏覽:597
舊版本蝌蚪短視頻app怎麼不能 瀏覽:458
電影劇情里有個老頭在雨里騎單車有防 瀏覽:950
日本歐美推理片電影 瀏覽:75
主角穿越到紅軍長征 瀏覽:915
《屍吻》拿走不謝 瀏覽:306