java xml dom4j 创建 修改 解析

来源:互联网 发布:js正则表达式数字范围 编辑:程序博客网 时间:2024/05/20 21:22


注:需要dom4j的jar包


package dom.parse;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

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


public class Dom4jTest {

 public static void main(String[] args) throws IOException, DocumentException {
    String fileName ="src/dom/parse/dom4j.xml";
    //createXml(fileName);
    //modifyXml(fileName);
    parseXml(fileName);
 
 }

 private static void parseXml(String fileName) throws DocumentException {
       SAXReader reader = new SAXReader();
       Document document = reader.read(fileName);
      
       List<Book> bookList = new ArrayList<Book>();
    
      
       Book book = new Book();
       Element root = document.getRootElement();
  for (Iterator books = root.elementIterator(); books.hasNext();) {
   
   Element bookElement = (Element)  books.next();
  
   String bookId=bookElement.attributeValue("bookId");
   Element shumingE=bookElement.element("shuming");
   String shuming=shumingE.getText();
  
   String shumingId=null;
   if(shumingE.attributes() != null)
   {
     shumingId=shumingE.attributeValue("shumingId");
     book.setShumingId(shumingId);
   }
  
  
   String zuozhe=bookElement.element("zuozhe").getText();
   String shoujia=bookElement.element("shoujia").getText();
  
   book.setBookId(bookId);
   book.setShuming(shuming);
   book.setZuozhe(zuozhe);
   book.setShoujia(shoujia);
  
   bookList.add(book);
 }
 
  System.out.println(bookList); 
 }

 private static void modifyXml(String fileName) throws DocumentException, IOException {

  SAXReader reader =new SAXReader();
  Document document = reader.read(fileName);
  
  Element root = document.getRootElement();
  
  Element book =(Element) root.elements("shu").get(1);
  String bookValue =book.attributeValue("bookId");
  System.out.println(bookValue);
  
  
  String value =  book.element("shuming").getText();
  System.out.println(value);
  
  String attrValue =book.element("shuming").attribute("shumingId").getValue();
  System.out.println(attrValue);
  
  
  Element book1 =(Element) root.elements("shu").get(0);
  book1.addElement("shoujia").setText("duiwaimai209");
  
  OutputFormat format = OutputFormat.createPrettyPrint();
  XMLWriter writer = new XMLWriter(new FileWriter(fileName),format);
  writer.write(document);
  writer.close();
  
  
  
 }

 public static void createXml(String fileName) throws IOException  {
  Document document = DocumentHelper.createDocument();
  Element root = document.addElement("shujia");
  
  Element book1=root.addElement("shu");
  book1.addElement("shuming").setText("zhangxiaoxiangjava");
  book1.addElement("zuozhe").setText("zhangxiaoxiang");
  book1.addElement("shoujia").setText("109");
  book1.addAttribute("bookId","1").addAttribute("baozhuang","jingzhuang");
  
  Element book2=root.addElement("shu");
  book2.addElement("shuming").setText("weigejava");
  book2.addElement("zuozhe").setText("weige");
  book2.addElement("shoujia").setText("79");
  book2.addAttribute("bookId","2");
  
  OutputFormat format = OutputFormat.createPrettyPrint();
  XMLWriter writer = new XMLWriter(new FileWriter(fileName),format);
  writer.write(document);
  writer.close();

 }
}

0 0