當前位置:首頁 » 存儲配置 » javaxml存儲數據

javaxml存儲數據

發布時間: 2022-08-09 14:13:30

java如何把數據寫入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插入到資料庫就好了啊。主要資料庫事務處理或用批處理

㈢ java可以用xml存儲數據嗎

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

㈣ 用java如何把xml里的數據解析出來並修改保存到資料庫

如果是要保存整個文件,可以轉2進制,然後把2進制字元串保存資料庫。如果是xml里的數據就1樓說的那樣

㈤ xml在java項目中起到的作用具體是什麼

java項目中,xml文件一般都是用來存儲一些配置信息
一般的編程, 多數用來存儲配置信息 . 拿JDBC來說,可以把資料庫連接字元串寫到xml,如果要修改數據源,只需要改xml就可以了,沒必要再去重新編譯java文件,而且,這些配置信息放在一起,別的人來讀你寫的代碼的時候,就方便了很多
框架中的xml , 除了配置信息 , 還可以寫一些對應關系,其實也是一種配置信息 .拿struts來說,xml配置的是頁面url對應後台java類(action)的關系,在配置和修改的時候,只需要改一個xml文件就可以了,沒必要一個個的查找java代碼
java項目完成之後,每個模塊應該都是獨立的,模塊之間的關系都可以使用xml來進行維護,spring就是這樣的一個框架

一個好的項目,需要有良好的可拓展性,如果把所有的邏輯關系還有配置信息都寫入代碼中,會使程序的可拓展性變差,為了解決這個問題,xml就可以對整個項目進行調度(spring)

還有使用xml作為數據儲存,不過用起來很少,多數還是用來存放配置信息

㈥ 如何用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什麼關系

xml(Extensible Markup Language)中文意思就是可擴展標記語言,用於存儲數據和描述數據,不同的平台可以同過xml文件建立起聯系。
在java開發中,許多的配置文件,都是xml的,比如web.xml,struts-config.xml,ibatis的sqlMapConfig.xml等。
XML作為全球通用的結構化語言,越來越受人們青睞,各種開發平台(比如Microsoft Studio系列、Oracle系列、Inprise Borland系列等)也都把支持XML開發作為宣傳口號之一 。由於筆者所從事的電子政務開發較早的引入了XML,所以嘗到了許多甜頭,在許多項目中利用XML數據交換信息,省去了許多麻煩事,不用制定繁鎖的數據格式,利用XML數據易於表達,也利於一線開發者跟蹤調試。

㈧ java如何向xml文件寫入內容

我以前學dom解析的時候寫了一個小例子,你參考參考

packagecom.lhx.test;

importjava.io.File;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;

importjavax.xml.parsers.DocumentBuilder;
importjavax.xml.parsers.DocumentBuilderFactory;
importjavax.xml.parsers.ParserConfigurationException;

importorg.w3c.dom.Attr;
importorg.w3c.dom.Document;
importorg.w3c.dom.Element;
importorg.w3c.dom.Text;


publicclassTest{
publicstaticvoidmain(String[]args){
DocumentBuilderFactoryfct=DocumentBuilderFactory.newInstance();
try{
DocumentBuilderbui=fct.newDocumentBuilder();
Documentdoc=bui.newDocument();
Elementps=doc.createElement("persons");
Elementp1=doc.createElement("person");
Elementp2=doc.createElement("person");
Attrid1=doc.createAttribute("id");
Attrid2=doc.createAttribute("id");
id1.setNodeValue("1");
id2.setNodeValue("2");
Elementname1=doc.createElement("name");
Textna1=doc.createTextNode("龍大哥");
Elementname2=doc.createElement("name");
Textna2=doc.createTextNode("龍大爺");
Elementsex1=doc.createElement("sex");
Textse1=doc.createTextNode("帥哥");
Elementsex2=doc.createElement("sex");
Textse2=doc.createTextNode("妹子");

doc.appendChild(ps);
ps.appendChild(p1);
p1.appendChild(name1);
p1.setAttributeNode(id1);
name1.appendChild(na1);
p1.appendChild(sex1);
sex1.appendChild(se1);
ps.appendChild(p2);
p2.appendChild(name2);
p2.setAttributeNode(id2);
name2.appendChild(na2);
p2.appendChild(sex2);
sex2.appendChild(se2);
try{
FileOutputStreamfos=newFileOutputStream(newFile("E:/longdada.xml"));

try{
((org.apache.crimson.tree.XmlDocument)doc)
.write(fos);
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
try{
fos.flush();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
try{
fos.close();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}


}catch(FileNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}





}catch(ParserConfigurationExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}

}
}

附圖:

這個例子有文本節點的創建,屬性的創建等等,基本上可以解決絕大多數XML內容了。無論你想創建什麼類型的XML,可以套用裡面的方法。

另外,注意:文件通過流創建的時候用到一個類,需要一個jar,這個類我已經用完整形式寫出來了,你去網上下載下來,添加進工程即可。



弱國覺得可行,望採納^_^

㈨ java怎麼在xml文件中保存和讀取字元串數組

package test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

public class XmlTest {
public String[] xml2Array(String fileName) throws JDOMException, IOException{
SAXBuilder b = new SAXBuilder();
Document xmlFile = b.build(new File(fileName));
Element root = xmlFile.getRootElement();
List ls = root.getChildren("week");
String weeks[]=new String[7];
for( int i = 0;i<ls.size();i++){
Element e = (Element)ls.get(i);
String week = e.getText();
weeks[i]=week;
}
return weeks;
}

public void array2Xml(String[] weekArray,String fileName) throws FileNotFoundException, IOException{
Element weeks = new Element("weeks");
Document doc = new Document(weeks);
for(int i=0;i<weekArray.length;i++){
Element week = new Element("week");
week.setText(weekArray[i]);
weeks.addContent(week);
}

XMLOutputter out = new XMLOutputter();
out.setFormat(out.getFormat().setEncoding("GBK"));
out.output(doc,new FileOutputStream( new File(fileName)));
}
/**
* @param args
* @throws IOException
* @throws JDOMException
*/
public static void main(String[] args) throws JDOMException, IOException {
XmlTest xml = new XmlTest();
String fileName = "week.xml";
String weeks[] ={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
xml.array2Xml(weeks,fileName);
String testWeeks[] = xml.xml2Array(fileName);
for(String s:testWeeks){
System.out.println(s);
}
}
}

熱點內容
兩氣體壓縮 發布:2024-03-29 20:18:09 瀏覽:993
易語言教程解壓 發布:2024-03-29 20:18:00 瀏覽:785
我老爸的密碼是什麼 發布:2024-03-29 20:03:50 瀏覽:247
資料庫定義實驗 發布:2024-03-29 19:52:20 瀏覽:578
如何除去安卓手機的馬賽克 發布:2024-03-29 19:52:16 瀏覽:584
網站緩存設置 發布:2024-03-29 19:47:20 瀏覽:798
在jsp中使用資料庫 發布:2024-03-29 19:29:01 瀏覽:786
dns伺服器江川區ip地址 發布:2024-03-29 18:47:53 瀏覽:328
sql統計百分比 發布:2024-03-29 18:47:14 瀏覽:692
javatoolsfor 發布:2024-03-29 18:17:55 瀏覽:900