Ⅰ 5、通過對象生成XML文件
生成XML文件通過對象進行,運用java中的java.beans.XMLEncoder和java.beans.XMLDecoder類,這是在JDK1.4版本後出現的工具類,實現對象到XML文件的轉換。
具體步驟如下:
第一步:實例化XML編碼器。初始化一個BufferedOutputStream用於將數據輸出到文件中,隨後創建XMLEncoder對象,將編碼器與輸出流關聯。
代碼如下:
java
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("test.xml")); XMLEncoder xmlEncoder = new XMLEncoder(bos);
第二步:輸出對象。使用編碼器將對象轉化為XML格式並寫入文件中。
java
Person person = new Person("張三", 20); // 示例對象
xmlEncoder.writeObject(person);
第三步:關閉。確保資源釋放,避免內存泄露。
java
xmlEncoder.close();
其中,Person.java作為示例對象類,可以包含屬性和方法用於表示和操作實體數據。
通過以上步驟,成功將Java對象轉化為XML文件。此方法在數據交換、配置文件存儲等領域具有廣泛應用價值。
Ⅱ 如何用java語言生成xml文件,並將它返回
實例:
holen.xml
<?xml version="1.0" encoding="UTF-8"?>
<books>
<!--This is a test for dom4j, holen, 2004.9.11-->
<book show="yes">
<title>Dom4j Tutorials</title>
</book>
<book show="yes">
<title>Lucene Studing</title>
</book>
<book show="no">
<title>Lucene in Action</title>
</book>
<owner>O'Reilly</owner>
</books>
建立一個XML文檔:
/**
* 建立一個XML文檔,文檔名由輸入屬性決定
* @param filename 需建立的文件名
* @return 返回操作結果, 0表失敗, 1表成功
*/
public int createXMLFile(String filename){
/** 返回操作結果, 0表失敗, 1表成功 */
int returnValue = 0;
/** 建立document對象 */
Document document = DocumentHelper.createDocument();
/** 建立XML文檔的根books */
Element booksElement = document.addElement("books");
/** 加入一行注釋 */
booksElement.addComment("This is a test for dom4j, holen, 2004.9.11");
/** 加入第一個book節點 */
Element bookElement = booksElement.addElement("book");
/** 加入show屬性內容 */
bookElement.addAttribute("show","yes");
/** 加入title節點 */
Element titleElement = bookElement.addElement("title");
/** 為title設置內容 */
titleElement.setText("Dom4j Tutorials");
/** 類似的完成後兩個book */
bookElement = booksElement.addElement("book");
bookElement.addAttribute("show","yes");
titleElement = bookElement.addElement("title");
titleElement.setText("Lucene Studing");
bookElement = booksElement.addElement("book");
bookElement.addAttribute("show","no");
titleElement = bookElement.addElement("title");
titleElement.setText("Lucene in Action");
/** 加入owner節點 */
Element ownerElement = booksElement.addElement("owner");
ownerElement.setText("O'Reilly");
try{
/** 將document中的內容寫入文件中 */
XMLWriter writer = new XMLWriter(new FileWriter(new File(filename)));
writer.write(document);
writer.close();
/** 執行成功,需返回1 */
returnValue = 1;
}catch(Exception ex){
ex.printStackTrace();
}
return returnValue;
}
說明:
Document document = DocumentHelper.createDocument();
通過這句定義一個XML文檔對象。
Element booksElement = document.addElement("books");
通過這句定義一個XML元素,這里添加的是根節點。
Element有幾個重要的方法:
l addComment:添加註釋
l addAttribute:添加屬性
l addElement:添加子元素
Ⅲ 使用Java生成XML文件時,如何能讓文件自動換行
換行符" ",如是用dom4j之類的jar包操作,dom4j換行如下
/**
*通過org.dom4j.io.OutputFormat來設置XML文檔輸出格式
*/
OutputFormatformat=OutputFormat.createPrettyPrint();//設置XML文檔輸出格式
format.setEncoding("GB2312");//設置XML文檔的編碼類型
format.setSuppressDeclaration(true);
format.setIndent(true);//設置是否縮進
format.setIndent("");//以空格方式實現縮進
format.setNewlines(true);//設置是否換行
Ⅳ 如何用java生成一個XML文件,並且將該文件壓縮成ZIP格式後再寫到硬碟上
在你聲明ZipEntry的時候在name後加上.xml後綴就可以沖核了!!!
實例如下:
public static void main(String[] arg) throws Exception{
String xml;
/*
* 生成你的xml數據,存在String xml中。
*/散拿掘
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream("D://test.zip"));
//聲明ZipOutputStream,用來輸出zip文件。
ZipEntry entry = new ZipEntry("test.xml");
//聲明ZipEntry
zipOut.putNextEntry(entry);
//將entry加入到zipOut中。
DataOutputStream dataOs = new DataOutputStream(zipOut);
//利用DataOutputStream對ZipOutputStream進行包裝。敏宏
dataOs.writeUTF(gd);
//輸出zip文件。
dataOs.close();
}
運行後,在D盤里就有一個test.zip文件,里包含的就是一個test.xml文件了。