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

熱點內容
編程珠璣筆記 瀏覽:274
結束命令行 瀏覽:268
力學原理pdf 瀏覽:729
宏定義編譯後不變 瀏覽:397
如何搞免費伺服器 瀏覽:206
神經系統pdf 瀏覽:665
如何查看伺服器上的資料庫伺服器 瀏覽:188
壓縮機型號v代表什麼 瀏覽:52
旅遊類源碼 瀏覽:862
電腦伺服器類型怎麼設置 瀏覽:230
pdf炒股 瀏覽:786
伺服器地址缺少埠號什麼意思 瀏覽:530
下載需要解壓的小說用哪個軟體 瀏覽:534
廣東分布式伺服器雲主機 瀏覽:583
伺服器忙打不開怎麼辦 瀏覽:15
tif壓縮軟體 瀏覽:413
程序員那麼可愛陸漓上班第1天 瀏覽:952
macbookair自帶什麼app 瀏覽:701
如何關了加密的軟體 瀏覽:581
程序員p2p待遇 瀏覽:923