日期:2014-05-16 浏览次数:20494 次
package xls.util; import java.io.ByteArrayInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.util.Iterator; import java.util.List; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.Node; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.SAXValidator; import org.dom4j.io.XMLWriter; import org.dom4j.util.XMLErrorHandler; import org.xml.sax.SAXException; import org.xml.sax.SAXNotRecognizedException; import org.xml.sax.SAXNotSupportedException; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.io.xml.DomDriver; /** * @author panming * @since 2008-5-6 */ public final class Dom4jUtil { /** * 取锟斤拷Root锟节碉拷 * @param document * @return */ public static Element getRootElement(Document document) { return document.getRootElement(); } /** * 锟斤拷锟斤拷锟街凤拷芨锟绞斤拷锟絰ml锟斤拷锟斤拷 * @param document * @return */ public String getStringDocument(Document document) { String xmlContent = null; if(document != null){ xmlContent = document.asXML(); } return xmlContent; } /** * 锟斤拷锟截节碉拷路锟斤拷 * @param rootNodeName(锟斤拷诘锟斤拷锟斤拷) * @param childNodeName(锟接节碉拷锟斤拷锟? * @return String(锟斤拷式锟斤拷锟矫碉拷xPath锟斤拷锟? */ public static String getNodePath(String rootNodeName, String childNodeName) { String result = null; if (rootNodeName == "" || rootNodeName == null || childNodeName == "" || childNodeName == null) { return result; } result = getNodePath(rootNodeName, childNodeName, null); return result; } /** * 锟斤拷锟截节碉拷路锟斤拷 * @param rootNodeName (锟斤拷诘锟斤拷锟斤拷) * @param childNodeName (锟接节碉拷锟斤拷锟? * @param childAttributeName (锟斤拷锟斤拷锟斤拷锟? * @return String (锟斤拷式锟斤拷锟矫碉拷xPath锟斤拷锟? */ public static String getNodePath(String rootNodeName, String childNodeName, String childAttributeName) { String result = null; if (rootNodeName == "" || rootNodeName == null || childNodeName == "" || childNodeName == null) { return result; } String dima = "/"; StringBuffer nodePath = new StringBuffer(); nodePath.append(rootNodeName); nodePath.append(dima).append(childNodeName); if (childAttributeName != null && childAttributeName != "") { nodePath.append(dima).append("@"); nodePath.append(childAttributeName); } result = nodePath.toString(); return result; } /** * 取锟斤拷Document锟斤拷指锟斤拷锟节碉拷锟铰碉拷锟斤拷锟叫节点集锟斤拷 * @param document (document锟斤拷锟斤拷) * @param nodeName (锟斤拷式锟斤拷锟矫碉拷xPath锟斤拷锟? "/books/book/@show" * @return List */ public static List<?> getListByDocument(Document document, String nodeName) { if (document == null || nodeName == null || nodeName == "") { return null; } List<?> list = null; list = document.selectNodes(nodeName); return list; } /** * 取锟矫节点集锟斤拷 * @param filePathAndName(xml锟侥硷拷) * @param nodePath(锟节碉拷xpath锟斤拷锟? * @return List(锟节碉拷锟叫憋拷) * @throws DocumentException */ public static List<?> getNodeList(String filePathAndName, String nodePath) throws DocumentException { List<?> list = null; if (filePathAndName == null || filePathAndName == "" || nodePath == null || nodePath == "") { return list; } list = getListByDocume