导航:首页 > 编程语言 > 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保存相关的资料

热点内容
ipad建文件夹怎么弄 浏览:833
iphone13对wap3加密 浏览:553
pdf文件打开失败 浏览:911
dubbo怎么调用不同服务器接口 浏览:38
全能解压王app历史版本 浏览:73
优先队列与拓扑排序算法 浏览:279
pdf转换formacbook 浏览:869
pdf文件内容怎么编辑 浏览:46
134压缩机排气温度多少 浏览:254
unity等待编译后 浏览:804
黑鲨手机锁屏视频在哪个文件夹 浏览:779
wow地图解压后怎么压缩 浏览:819
有pdf却打不开 浏览:460
七星彩软件app怎么下载 浏览:217
32单片机的重映射哪里改 浏览:816
为什么前端不用刷算法题 浏览:708
对称加密系统和公钥加密系统 浏览:428
历史地理pdf 浏览:606
物联网云服务器框架 浏览:648
sybaseisql命令 浏览:183