xml的读取分析 key value

package com.basedata.xml;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.basedata.hibernate.BaseData;


public class XmlReadWrite {

 private static final String xmlPath = "/com/basedata/xml/common.xml";

 // 保存配置文件信息
 public static Hashtable enumHash = new Hashtable();

 public static void  readXML() throws IOException,
   DocumentException {
  URL url = XmlReadWrite.class.getResource(xmlPath);
  String rootUrl = url.getPath();
  rootUrl = java.net.URLDecoder.decode(rootUrl, "utf-8");
  File xmlFile = new File(rootUrl);
  FileInputStream fis = null;
  try {
   fis = new FileInputStream(xmlFile);
  } catch (FileNotFoundException e) {
   e.printStackTrace();
   System.err.println("File is not exsit!");
  }
  SAXReader saxReader = new SAXReader();
  Document doc = saxReader.read(fis);
  Element root = doc.getRootElement();
  for (Iterator iter = root.elementIterator(); iter.hasNext();) {
   Element child = (Element) iter.next();
   Attribute childAttr = child.attribute("name");
   String keyName = childAttr.getText();
   List lst = new ArrayList();
   for (Iterator iterCh = child.elementIterator(); iterCh.hasNext();) {
    Element element2 = (Element) iterCh.next();
    Attribute attr = element2.attribute("status");
    String sta = attr.getText();
    String staV = element2.getText();
    BaseData option = new BaseData();
     option.setType(sta);
     option.setTypeName(staV);
     lst.add(option);
   }
   enumHash.put(keyName, lst);
  }
 }
 /**
  * 读取基础数据集合
  * @param name
  * @return
  */
 public static List getEnum(String name) {
  List list = new ArrayList();
  try {
   if (enumHash.size() == 0) {
    readXML();
   }
  } catch (IOException e) {
   e.printStackTrace();
  } catch (DocumentException e) {
   e.printStackTrace();
  }
  list = (ArrayList) enumHash.get(name);
  return list;
 }

  /**
   * 根据枚举名称和key获得值
   *
   * @param nameSpace
   * @param status
   * @return
   */
  public static String getTextByStatus(String nameSpace, String status){
   if (enumHash.size() == 0) {
    getEnum(nameSpace);
   }
   String value = "";
   List list = (List) enumHash.get(nameSpace);
   for (Iterator iter = list.iterator(); iter.hasNext();) {
    BaseData e = (BaseData) iter.next();
    if (e.getType().equalsIgnoreCase(status)) {
     value = e.getTypeName();
     break;
    }
   }
   return value;
  }
}

 -------------------------xml--------------------------------------------------------

 

<?xml version="1.0" encoding="GB2312"?>
<enum>
 <enumitem name="baseType" desc="基础数据类型">
  <option status="1">婚姻状况</option>
  <option status="2">就业状况</option>
  <option status="3">家庭类别</option>
  <option status="4">房屋产权性质</option>
  <option status="5">房屋类型</option>
  <option status="6">与申请人关系</option>
  <option status="7">证件类型</option>
  <option status="8">性别</option>
  <option status="9">职工类型</option>
  <option status="10">单位类别</option>
  <option status="11">单位所属行业</option>
 </enumitem>
 <enumitem name="auditStatus" desc="审核状态">
  <option status="1">未提交</option>
  <option status="2">待审核</option>
  <option status="3">审核通过</option>
  <option status="4">未通过</option>
 </enumitem>
 <enumitem name="verifyType" desc="家庭核定类型">
  <option status="1">核定住房面积</option>
  <option status="2">核定收入</option>
 </enumitem>
 <enumitem name="flowAuditStatus" desc="审核状态">
  <option status="1">待审核</option>
  <option status="2">审核通过</option>
  <option status="3">未通过</option>
 </enumitem>
 
</enum>

你可能感兴趣的:(java,xml,Hibernate,.net)