XML 存储文档

package com.kpsh.myself;

import java.io.File;
import java.io.FileWriter;

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

public class ParseXML {
 
 public static void main(String[] args) {
  createXMLFile();
 }

 public static void createXMLFile() {
  Document document = DocumentHelper.createDocument();
  Element booksElement = document.addElement("Student");

  Element bookElement = booksElement.addElement("book");
  bookElement.addAttribute("show", "Name");
  Element titleElement = bookElement.addElement("title");
  titleElement.setText("the new stduent");
  
  bookElement = booksElement.addElement("book");
  bookElement.addAttribute("show", "Num");
  titleElement = bookElement.addElement("title");
  titleElement.setText("his name");
  
  bookElement = booksElement.addElement("book");
  bookElement.addAttribute("show", "Address");
  titleElement = bookElement.addElement("title");
  titleElement.setText("上海");
  
  bookElement = booksElement.addElement("book");
  bookElement.addAttribute("show", "Tel");
  titleElement = bookElement.addElement("title");
  titleElement.setText("12345678");
  
  Element ownerElement = booksElement.addElement("owner");
  ownerElement.setText("qh");
  try {
   XMLWriter writer = new XMLWriter(new FileWriter(new File("e://output.xml")));
   writer.write(document);
   writer.close();
  } catch (Exception ex) {
   ex.printStackTrace();
  }
 }

 }

posted on 2014-04-29 18:10  我是齐欢  阅读(161)  评论(0编辑  收藏  举报