程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> DOM4j--write,dom4j

DOM4j--write,dom4j

編輯:JAVA綜合教程

DOM4j--write,dom4j


import java.io.File;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

public class DOM4Write {
public static void main(String[] args) {
Document document = DocumentHelper.createDocument();
Element bookStore = document.addElement("bookstore");
Element book = bookStore.addElement("book");
book.addAttribute("id", "001");
Element name = book.addElement("name");
name.addText("明朝那些事");
Element price = book.addElement("price");
price.setText("100元");
Element type = book.addElement("type");

type.setText("歷史");
Element author = book.addElement("author");
author.setText("安格");
OutputFormat format = OutputFormat.createPrettyPrint();
try {
XMLWriter writer = new XMLWriter(new FileOutputStream(new File(
"books.xml")), format);
writer.write(document);
writer.close();
} catch (Exception e) {
e.printStackTrace();
      }
   }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved