当前位置:首页 » 存储配置 » javaxml存储

javaxml存储

发布时间: 2023-07-17 14:53:52

❶ 如何用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();
}
}

❷ java可以用xml存储数据吗

可以到是可以
但是如果存储的是正儿八经的数据的话
建议你用ACCESS
XML用来做配置文件倒是蛮合适的

❸ 如何用java解析xml文档,然后将数据存到数据库

package test11;
import javax.xml.parsers.*;
import org.w3c.dom.*;

import java.io.*;
public class XMLUtil
{
//该方法用于从XML配置文件中提取具体类类名,并返回一个实例对象
public static Object getBean()
{
try
{
//创建文档对象
DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dFactory.newDocumentBuilder();
Document doc;
doc = builder.parse(new File("config.xml"));

//获取包含类名的文本节点
NodeList nl = doc.getElementsByTagName("className");
Node classNode=nl.item(0).getFirstChild();
String cName=classNode.getNodeValue();

//通过类名生成实例对象并将其返回
Class c=Class.forName(cName);
Object obj=c.newInstance();
return obj;
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
}
}

<?xml version="1.0"?>
<config>
<className>test11.CatAdapter</className>
</config>
然后你吧解析处理的值放到数组或LIST或其他的你能存放的对象中。再写SQL插入到数据库就好了啊。主要数据库事务处理或用批处理

❹ xml在java项目中起到的作用具体是什么

java项目中,xml文件一般都是用来存储一些配置信息
一般的编程, 多数用来存储配置信息 . 拿JDBC来说,可以把数据库连接字符串写到xml,如果要修改数据源,只需要改xml就可以了,没必要再去重新编译java文件,而且,这些配置信息放在一起,别的人来读你写的代码的时候,就方便了很多
框架中的xml , 除了配置信息 , 还可以写一些对应关系,其实也是一种配置信息 .拿struts来说,xml配置的是页面url对应后台java类(action)的关系,在配置和修改的时候,只需要改一个xml文件就可以了,没必要一个个的查找java代码
java项目完成之后,每个模块应该都是独立的,模块之间的关系都可以使用xml来进行维护,spring就是这样的一个框架

一个好的项目,需要有良好的可拓展性,如果把所有的逻辑关系还有配置信息都写入代码中,会使程序的可拓展性变差,为了解决这个问题,xml就可以对整个项目进行调度(spring)

还有使用xml作为数据储存,不过用起来很少,多数还是用来存放配置信息

❺ java 修改了 XML 文件 如何保存文件

在修改document后,尘察亮可以使用jdk提供的Transformer讲dom树转换成xml。
Source xmlSource = new DOMSource(document);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
Result result = new StreamResult(new File("..."));
transformer.transform(xmlSource, result); // 保存dom至目的没源文件

希望派宽能帮上你。

❻ 如何在java里长期存储数据 不要数据库的那种

长期存储数据,即把数据(如内存中的)保存到可永久保存的存储设备中(如硬盘、U盘),也就是人们常说的持久化。

常用持久化的方案有数据库、XML文件和文件存储。

数据库是按照数据结构来存储和管理数据的仓库,后文不再做详细介绍。

XML是可扩展标记语言,最早是为了简化Internet的文档数据传输,它提供统一的语法格式来描述数据的结构,通常XML文件用于一些少量且无特殊类型要求的文本存储。示例代码使用W3C标准的接口生成XML:

importjava.io.FileOutputStream;
importjava.io.PrintWriter;

importjavax.xml.parsers.DocumentBuilderFactory;
importjavax.xml.transform.OutputKeys;
importjavax.xml.transform.Transformer;
importjavax.xml.transform.TransformerFactory;
importjavax.xml.transform.dom.DOMSource;
importjavax.xml.transform.stream.StreamResult;

importorg.w3c.dom.Document;
importorg.w3c.dom.Element;

publicclass${
publicstaticvoidmain(String[]args)throwsException{
Documentdocument=DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
//创建根节点为students的XML文件
Elementstudents=document.createElement("students");
document.appendChild(students);

//在根节点下创建一个子节点学生
Elementstudent=document.createElement("student");
students.appendChild(student);
//创建节点学生姓名,值为张三
Elementname=document.createElement("name");
name.appendChild(document.createTextNode("张三"));
student.appendChild(name);
//创建节点学生年龄,值为18
Elementage=document.createElement("age");
age.appendChild(document.createTextNode("18"));
student.appendChild(age);
//创建节点学生编号,值为150101
Elementnumber=document.createElement("number");
number.appendChild(document.createTextNode("150101"));
student.appendChild(number);

//在根节点下创建第二个子节点学生
student=document.createElement("student");
students.appendChild(student);
//创建节点学生姓名,值为李四
name=document.createElement("name");
name.appendChild(document.createTextNode("李四"));
student.appendChild(name);
//创建节点学生年龄,值为20
age=document.createElement("age");
age.appendChild(document.createTextNode("20"));
student.appendChild(age);
//创建节点学生编号,值为150102
number=document.createElement("number");
number.appendChild(document.createTextNode("150102"));
student.appendChild(number);

//将XML文件保存到硬盘
Transformertransformer=TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING,"utf-8");
transformer.setOutputProperty(OutputKeys.INDENT,"yes");
PrintWriterwriter=newPrintWriter(newFileOutputStream("/home/test.xml"));
transformer.transform(newDOMSource(document),newStreamResult(writer));
}
}

无论是数据库还是XML文件,它们都使用了能让数据快速方便进出的标准规范。其它文件如propeties、json,都可以使用类似XML的方式来打包数据,然后通过Java丰富的io流接口保存到磁盘中。

❼ Java编写图书管理系统,使用XML存储

importjava.io.File;
importjava.io.FileOutputStream;
importjava.util.ArrayList;
importjava.util.List;
importjava.util.Scanner;
importorg.dom4j.Document;
importorg.dom4j.DocumentHelper;
importorg.dom4j.Element;
importorg.dom4j.io.OutputFormat;
importorg.dom4j.io.SAXReader;
importorg.dom4j.io.XMLWriter;

publicclassBook{

privateintno;
privateStringname;
privatedoublevalue;

publicBook(){
}

publicBook(intno,Stringname,doublevalue){
this.no=no;
this.name=name;
this.value=value;
}

publicdoublegetValue(){
returnvalue;
}

publicvoidsetValue(doublevalue){
this.value=value;
}

publicStringgetName(){
returnname;
}

publicvoidsetName(Stringname){
this.name=name;
}

publicintgetNo(){
returnno;
}

publicvoidsetNo(intno){
this.no=no;
}
}

classBookList{

privateList<Book>bookList;

publicBookList(){
bookList=readXML();
}

publiclonggetCount(){
returnbookList.size();
}

publicList<Book>getBookList(){
returnbookList;
}

publicvoidsetBookList(List<Book>bookList){
this.bookList=bookList;
}

publicvoidadd(Bookbook){
bookList.add(book);
}

publicbooleandelete(Stringname){
Bookbook=query(name);
returnbookList.remove(book);
}

publicvoipdate(BookbookBefore,BookbookAfter){
bookList.remove(bookBefore);
add(bookAfter);
}

publicBookquery(Stringname){
Booktemp=null;
for(Bookbook:bookList){
if(book.getName().equals(name)){
temp=book;
}
}
returntemp;
}

(Bookbook){
try{
Filefile=newFile("D:\book.xml");
Documentdocument=null;
Elementroot=null;
if(!file.exists()){
//新建student.xml文件并新增内容
document=DocumentHelper.createDocument();
root=document.addElement("Books");//添加根节点
}else{
SAXReadersaxReader=newSAXReader();
document=saxReader.read(file);
root=document.getRootElement();//获得根节点
}
ElementsecondRoot=root.addElement("Book");//二级节点
//为二级节点添加属性,属性值为对应属性的值
secondRoot.addElement("no").setText(book.getNo()+"");
secondRoot.addElement("name").setText(book.getName()+"");
secondRoot.addElement("value").setText(book.getValue()+"");

OutputFormatformat=OutputFormat.createPrettyPrint();
format.setEncoding("GBK");
XMLWriterwriter=newXMLWriter(newFileOutputStream("D:\book.xml"),format);
writer.write(document);
writer.close();
document.clearContent();
}catch(Exceptione){
e.printStackTrace();
}
}

publicsynchronizedList<Book>readXML(){
List<Book>list=newArrayList<Book>();//创建list集合
Filefile=null;
try{
file=newFile("D:\book.xml");//读取文件
if(file.exists()){
SAXReadersaxReader=newSAXReader();
Documentdocument=saxReader.read(file);
ListnodeList=document.selectNodes("Books/Book");
for(inti=0;i<nodeList.size();i++){
Elementel=(Element)nodeList.get(i);
Bookbook=newBook();
book.setNo(Integer.parseInt(el.elementText("no")));
book.setName(el.elementText("name"));
book.setValue(Double.parseDouble(el.elementText("value")));
list.add(book);
}
}
}catch(Exceptione){
e.printStackTrace();
}
returnlist;
}
}

classTest{

publicstaticvoidmain(Stringargs[]){
BookListbl=newBookList();
booleanbBreak=true;
while(bBreak){
System.out.println("请输入操作代码:");
System.out.println("1:添加2:删除3:修改4:查询5:书籍统计6:退出");
Scannersc=newScanner(System.in);
intcode=sc.nextInt();
if(code==1){
System.out.println("请输入编号");
intno=sc.nextInt();
System.out.println("请输入书名");
Stringname=sc.next();
System.out.println("请输入售价");
doublevalue=sc.nextDouble();
Bookbook=newBook(no,name,value);
bl.add(book);
bl.writeXmlDocument(book);
}elseif(code==2){
System.out.println("请输入要删除的书籍名");
Stringname=sc.next();
if(bl.delete(name)){
System.out.println("删除成功");
}else{
System.out.println("书籍不存在");
}
}elseif(code==3){
System.out.println("请输入要修改的书籍名");
Stringname=sc.next();
BookbookBefore=bl.query(name);

System.out.println("请输入新的编号");
intnewNo=sc.nextInt();
System.out.println("请输入新的书名");
StringnewName=sc.next();
System.out.println("请输入新的售价");
doublevalue=sc.nextDouble();
BookbookAfter=newBook(newNo,newName,value);
bl.update(bookBefore,bookAfter);
}elseif(code==4){
System.out.println("请输入要查询的书籍名");
Stringname=sc.next();
Bookbook=bl.query(name);
System.out.println("编号:"+book.getNo()+"书名:"+book.getName()+"售价:"+book.getValue());
}elseif(code==5){
List<Book>list=bl.getBookList();
System.out.println("总书籍数:"+bl.getCount());
for(Bookbook:list){
System.out.println("编号:"+book.getNo()+"书名:"+book.getName()+"售价:"+book.getValue());
}
}elseif(code==6){
bBreak=false;
}
}
}
}

jar 包 dom4j.jar jaxen-1.1.4.jar

热点内容
滑板鞋脚本视频 发布:2025-02-02 09:48:54 浏览:433
群晖怎么玩安卓模拟器 发布:2025-02-02 09:45:23 浏览:557
三星安卓12彩蛋怎么玩 发布:2025-02-02 09:44:39 浏览:744
电脑显示连接服务器错误 发布:2025-02-02 09:24:10 浏览:537
瑞芯微开发板编译 发布:2025-02-02 09:22:54 浏览:147
linux虚拟机用gcc编译时显示错误 发布:2025-02-02 09:14:01 浏览:240
java驼峰 发布:2025-02-02 09:13:26 浏览:652
魔兽脚本怎么用 发布:2025-02-02 09:10:28 浏览:538
linuxadobe 发布:2025-02-02 09:09:43 浏览:212
sql2000数据库连接 发布:2025-02-02 09:09:43 浏览:726