class="java" name="code">package com.cn.xml; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import org.w3c.dom.Document; public class XMLParseUtil { private static final Document document=bulidDocument(); //获取document解析对象 private static final Transformer transformer=buildTransformer(); //获取transformer转换对象 private static Document bulidDocument(){ try{ DocumentBuilderFactory dFactory=DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder=dFactory.newDocumentBuilder(); return dBuilder.newDocument(); }catch(Throwable ex){ throw new ExceptionInInitializerError(ex); } } public static Document getDocument(){ return document; } private static Transformer buildTransformer(){ try{ TransformerFactory tFactory=TransformerFactory.newInstance(); return tFactory.newTransformer(); }catch(Throwable ex){ throw new ExceptionInInitializerError(ex); } } public static Transformer getTransformer(){ return transformer; } }
?