java Document文档处理工具类--写入转换(XML与File转Document)

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;

import org.dom4j.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class DocumentUtil {
   public static Document createDocument()  {
	   try {
			// 初始化xml解析工厂
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			
			// 创建DocumentBuilder
			DocumentBuilder builder = factory.newDocumentBuilder();
			
			// 创建Document
			Document doc = (Document) builder.newDocument();

			doc.setXMLEncoding("UTF-8");
            return doc;
		} catch (Exception e) {
			e.printStackTrace();
		   throw new RuntimeException(e);
		}

   }
   public static Document parseXml(String in) {
       try {
           DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
           DocumentBuilder db = dbf.newDocumentBuilder();
           InputSource is = new InputSource(new StringReader(in));
           return (Document) db.parse(is);
       } catch (ParserConfigurationException e) {
           throw new RuntimeException(e);
       } catch (SAXException e) {
           throw new RuntimeException(e);
       } catch (IOException e) {
           throw new RuntimeException(e);
       }
   }
   public static Document parseFile(File file){
	   try{
		   // 初始化xml解析工厂
		     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	
		   // 创建DocumentBuilder
		   DocumentBuilder builder = factory.newDocumentBuilder();
	
		   // 创建解析xml的Document
		   Document doc = (Document) builder.parse(file);
		   // 根节点名称
//		   String rootName = doc.getDocumentElement().getTagName();
//		   System.out.println("根节点: " + rootName);
//	
//		   System.out.println("递归解析--------------begin------------------");
//		   // 递归解析Element
//		   Element element = doc.getDocumentElement();
//		   parseElement(element);
//		   System.out.println("递归解析--------------end------------------");
		   return doc;

	   } catch (ParserConfigurationException e) {
	         e.printStackTrace();
	         throw new RuntimeException(e);
	   } catch (SAXException e) {
	          e.printStackTrace();
	          throw new RuntimeException(e);
	   } catch (IOException e) {
	        e.printStackTrace();
	        throw new RuntimeException(e);
	    }
	   }
   /**
    * 传入到哪个文件路径 传入生成的document
    * @param fileUrl 需要生成文件的路径
    * @param doc 生成的文档
    * @throws IOException
    */
   public static void write(String fileUrl, Document doc) throws IOException {
       File file = new File(fileUrl);
       write(file, "UTF-8", doc);
   }

   /**
    * 传入到哪个文件路径 传入生成的document
    * @param fileUrl 文件路径
    * @param charsetName 编码
    * @param doc 文档
    * @throws IOException
    */
   public static void write(String fileUrl,String charsetName, Document doc) throws IOException {
       File file = new File(fileUrl);
       write(file, charsetName, doc);
   }

   /**
    * 传入到哪个文件 传入生成的document
    * @param file
    * @param doc
    * @throws IOException
    */
   public static void write(File file, Document doc) throws IOException {
       write(file, "UTF-8", doc);
   }

   /**
    * 将document内容写入文件中
    * @param file
    * @param charsetName
    * @param doc
    * @throws IOException
    */
   public static void write(File file, String charsetName, Document doc) throws IOException {
	   try {
		   if (!file.exists()) {
	           file.createNewFile();
	       }
	       FileOutputStream fos = new FileOutputStream(file, false);
	       // 设置输出流
	       OutputStreamWriter osw = new OutputStreamWriter(fos, charsetName);
	       // 讲doc写入文件中
	       osw.write(doc.asXML());
	       osw.close();
		} catch (Exception e) {
			// TODO: handle exception
			throw new RuntimeException(e);
		}
      
   }

}
发布了14 篇原创文章 · 获赞 15 · 访问量 1637

猜你喜欢

转载自blog.csdn.net/u011323200/article/details/103711170