XMLTool.java
Upload User: gtz2001
Upload Date: 2016-12-29
Package Size: 2489k
Code Size: 8k
Category:

WEB Mail

Development Platform:

Java

  1. package net.meybo.util;
  2. import java.io.*;
  3. import java.util.*;
  4. import org.dom4j.*;
  5. import org.dom4j.io.SAXReader;
  6. /***************************************
  7.  * <p>Title:处理 XML 文件程序</p>
  8.  * <p>Description:
  9.  * 对Dom4J进行简单的简化及封装,负责XML
  10.  * </p>
  11.  * <p>Copyright: Copyright (c) 2005</p>
  12.  * <p>Company: 脉博软件</p>
  13.  * @author 阚吉彬
  14.  * @version 1.0
  15.  ***************************************/
  16. public final class XMLTool{
  17.     static String encoding = "GBK";
  18. //    public static void main(String args[]) {
  19. //        try {
  20. //
  21. //        } catch(Exception ex) {
  22. //            ex.printStackTrace();
  23. //        }
  24. //    }
  25.     private XMLTool() {
  26.     }
  27.     /**
  28.      * 设置 XML 的编码 encding
  29.      * @param encding:UTF-8/GBK/GB2312
  30.      */
  31.     public static void setEncoding(String encding) {
  32.         encoding = encding;
  33.     }
  34.     /**
  35.      * 将一个 XML 字符串,转换成一个 Element
  36.      * @param xmlStr:一个正确的,良好的 XML 字符串
  37.      * @return
  38.      * @throws java.lang.Exception
  39.      */
  40.     public static Element loadXML(String xmlStr) {
  41.         Element el = null;
  42.         try {
  43.          el=DocumentHelper.createElement(xmlStr);            
  44.         } catch(Exception e) {
  45.           e.printStackTrace();
  46. //          throw new Exception("Load xml string error!", e);
  47.         }
  48.         return el;
  49.     }
  50.     /**
  51.      * 从一个磁盘路径导入 XML
  52.      * @param fileName
  53.      * @return
  54.      * @throws java.lang.Exception
  55.      */
  56.     public static synchronized Element load(String fileName) throws Exception {
  57.         Element el = null;
  58.         try {
  59.           SAXReader reader = new SAXReader();     
  60.           File file=new File(fileName);
  61.           Document document = file.exists()?reader.read(fileName):null;
  62.           el=document.getRootElement();            
  63.         } catch(Exception e) {
  64.           throw new Exception("Load xml string error!", e);
  65.         }
  66.         return el;
  67.     }
  68.    
  69.     /**
  70.      * 保存 XML 到磁盘上
  71.      * @param el
  72.      * @param fileName
  73.      * @throws java.lang.Exception
  74.      */
  75.     public static void save(Element el, String fileName) throws Exception {
  76.         save(el, fileName, encoding);
  77.     }
  78.     /**
  79.      * 保存 XML 到磁盘上,要传送 XML 编码 encoding
  80.      * @param el:Element
  81.      * @param fileName:带路径的文件名
  82.      * @param encoding;GBK/UTF-8/GB2312
  83.      * @throws java.lang.Exception
  84.      */
  85.     public static synchronized void save(Element el, String fileName, String encoding) throws Exception {
  86.         try {
  87.             XMLUtil.toXmlFile(XMLUtil.buildDocument(el), encoding, fileName);
  88.         } catch(Exception e) {
  89.           throw new Exception("Save xml string error!", e);
  90.         }
  91.     }
  92.     /**
  93.      * 将 Element 转换成 XML 字符串
  94.      * @param el
  95.      * @return
  96.      * @throws java.lang.Exception
  97.      */
  98.     public static String toXMLString(Element el) throws Exception {
  99.       try{
  100.         if (el == null)
  101.           throw new NullPointerException();
  102.         return el.asXML();
  103.       } catch(Exception e) {
  104.         throw new Exception("to xml string error!");
  105.       }
  106.     }
  107.     public static synchronized String getNodeValue(String fileName,String node)  {
  108.      String tmpstr="";
  109.      try{
  110.      Element config=XMLTool.load(fileName);    
  111. if(config!=null)
  112. tmpstr=config.selectSingleNode(node).getText();
  113.      }
  114. catch(Exception e)
  115. {
  116. }
  117. return tmpstr;
  118.     }
  119.     /**
  120.      * 将 Element 转换成 XML 字符串,要传送 XML 编码 encoding
  121.      * @param el
  122.      * @param encoding:GBK/UTF-8/GB2312
  123.      * @return
  124.      * @throws java.lang.Exception
  125.      */
  126.     public static String toXMLString(Element el, String encoding) throws Exception {
  127.       try{
  128.         if(el == null)
  129.             throw new NullPointerException();
  130.         return el.asXML();
  131.       } catch(Exception e) {
  132.         throw new Exception("to xml string error!");
  133.       }
  134.     }
  135.     /**
  136.      * 设置节点的一个属性
  137.      * @param element
  138.      * @param name
  139.      * @param value
  140.      */
  141.     public static void setAttribute(Element element, String name, String value) {
  142.         if(value != null && element.attribute(name)!=null)
  143.         {
  144.          element.attribute(name).setText(value);
  145.         }
  146.             
  147.     }
  148.     /**
  149.      * 设置节点的多个属性
  150.      * @param element
  151.      * @param attrList
  152.      */
  153.     public static void setAttributes(Element element, List attrList) {
  154.         int len = attrList.size();
  155.         for(int i = 0; i < len; i++) {
  156.             Attribute attr = (Attribute)attrList.get(i);
  157.             setAttribute(element, attr.getName(), attr.getValue());
  158.         }
  159.     }
  160.     /**
  161.      * 设置节点的一个属性
  162.      * @param element
  163.      * @param name
  164.      * @param value
  165.      */
  166.     public static void addAttribute(Element element, String name, String value) {
  167.         if(value != null)
  168.             if(element.attribute(name) == null)
  169.                 element.add(element.addAttribute(name,value));
  170.             else
  171.                 throw new RuntimeException("Attribute already exist! attribute name is " + name);
  172.     }
  173.     /**
  174.      * 获得节点属性值
  175.      * @param element
  176.      * @param name
  177.      * @return
  178.      */
  179.     public static String getAttributeValue(Element element, String name) {
  180.         String attributeValue = element.attributeValue(name);
  181.         if(attributeValue == null)
  182.             attributeValue = "";
  183.         return attributeValue;
  184.     }
  185.     /**
  186.      * 获得节点属性名
  187.      * @param element
  188.      * @param name
  189.      * @return
  190.      */
  191.     public static String getAttributeString(Element element, String name) {
  192.         String attributeValue =element.attributeValue(name);
  193.         if(attributeValue == null)
  194.             attributeValue = "";
  195.         return attributeValue;
  196.     }
  197.     public static int getIndex(List elementLT, String name, String value)
  198.     {
  199.         int index = -1;
  200.         if(value != null)
  201.         {
  202.             int listLength = elementLT.size();
  203.             for(int i = 0; i < listLength; i++)
  204.             {
  205.                 String attrValue = ((Element)elementLT.get(i)).attributeValue(name);
  206.                 if(attrValue == null || !attrValue.equals(value))
  207.                     continue;
  208.                 index = i;
  209.                 break;
  210.             }
  211.         }
  212.         return index;
  213.     }
  214.     public static List getAllChildren(Element theEL)
  215.     {
  216.         List childrens = new ArrayList(100);
  217.         List lt = theEL.elements();
  218.         childrens.addAll(lt);
  219.         for(int i = 0; i < lt.size(); i++)
  220.             childrens.addAll(getAllChildren((Element)lt.get(i)));
  221.         return childrens;
  222.     }
  223.     public static int getDeepness(Element el)
  224.     {
  225.         int deepness[] = {
  226.             0, 0
  227.         };
  228.         getDeepness(el, deepness);
  229.         return deepness[1];
  230.     }
  231.     private static void getDeepness(Element el, int deepness[])
  232.     {
  233.         deepness[0]++;
  234.         if(deepness[0] > deepness[1])
  235.             deepness[1] = deepness[0];
  236.         List elList = el.elements();
  237.         int ellength = elList.size();
  238.         for(int i = 0; i < ellength; i++)
  239.         {
  240.             Element subEl = (Element)elList.get(i);
  241.             getDeepness(subEl, deepness);
  242.             deepness[0]--;
  243.         }
  244.     }
  245.     public static Element copyAttributesToElement(Element srcEL, Element tgtEL, int copyType)
  246.     {
  247.         List attrs = srcEL.attributes();
  248.         int len = attrs.size();
  249.         for(int i = 0; i < len; i++)
  250.         {
  251.             Attribute attr = (Attribute)attrs.get(i);
  252.             Attribute attr2 = tgtEL.attribute(attr.getName());
  253.             if(attr2 == null)
  254.                 tgtEL.add((Attribute)attr.clone());
  255.             else
  256.             if(copyType == 0)
  257.                 attr2.setValue(attr.getValue());
  258.         }
  259.         return tgtEL;
  260.     }
  261.     public static Element copyAttributesToElement(List srcAttrs, Element tgtEL, int copyType)
  262.     {
  263.         int len = srcAttrs.size();
  264.         for(int i = 0; i < len; i++)
  265.         {
  266.             Attribute attr = (Attribute)srcAttrs.get(i);
  267.             Attribute attr2 = tgtEL.attribute(attr.getName());
  268.             if(attr2 == null)
  269.                 tgtEL.add((Attribute)attr.clone());
  270.             else
  271.             if(copyType == 0)
  272.                 attr2.setValue(attr.getValue());
  273.         }
  274.         return tgtEL;
  275.     }
  276. }