jar包版本:jox-1.16.jar
xml数据流读取:
import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.text.DecimalFormat; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * Date: 2016-12-15 下午09:07:40 * * Desc: XML配置文件解析类 * * @author zhaoning */ public class ViewsConfigXmlParse { private static final ViewsConfigXmlParse instance = new ViewsConfigXmlParse(); // 线程池 private ExecutorService threadPool = Executors.newFixedThreadPool(1); //配置文件路径 private String viewConfigFilePath = "resource/configure.xml"; private ViewsConfigXmlParse(){ } public static ViewsConfigXmlParse getInstance(){ return instance; } public void initialize(){ threadPool.submit(new Runnable() { public void run() { try { initViewsConfig(); }catch (Exception e) { e.printStackTrace(); System.out.println("initialize ViewsCache failed."); } } }); } /** * 初始化视图缓存 * * @throws Exception */ private void initViewsConfig() throws Exception { initViewsConfigCache(viewConfigFilePath); // 设置缓存初始化成功 ViewsCache.getInstance().setInitialized(); } public void initViewsConfigCache(String filePath)throws Exception { File viewConfigFile = new File(filePath); String fileName = viewConfigFile.getName(); if (fileName.contains("configure") && fileName.endsWith("xml")) { loadViewConfig(viewConfigFile); } } private void loadViewConfig(File configFile) throws IOException { System.out.println("fileName:"+configFile.getName()); InputStream inputStream = null; try { inputStream = new FileInputStream(configFile); XCollectionManager metaDataManager = XCollectionManager .getInstance(); XCollection moView = metaDataManager.initialize(inputStream); } catch (Exception e) { e.printStackTrace(); System.out.println("fileName:"+configFile.getName()); } finally { if (inputStream != null) { inputStream.close(); } } System.out.println("load view config file finish. fileName=" + configFile.getName()); } public static void main(String[] args){ } }
xml文件解析映射类:
import java.io.IOException; import java.io.InputStream; import java.net.URL; import com.wutka.jox.JOXBeanInputStream; /** * Date: 2016年12月27日 上午9:34:14 * * Desc: 类简要描述 * * @author zhaoning */ public class XCollectionManager { private static final XCollectionManager instance = new XCollectionManager(); private XCollectionManager() { } public static XCollectionManager getInstance() { return instance; } public XCollection initialize(URL url) { try { return this.initialize(url.openStream()); } catch (IOException e) { return null; } } public XCollection initialize(InputStream inputStream) { JOXBeanInputStream joxIn = null; try { joxIn = new JOXBeanInputStream(inputStream); XCollection collection = (XCollection) joxIn .readObject(XCollection.class); return collection; } catch (Exception e) { e.printStackTrace(); } finally { if (joxIn != null) { try { joxIn.close(); } catch (IOException e) { } } } return null; } }
xml映射成java类模型: import java.io.Serializable; /** * Date: 2016年12月27日 上午9:51:55 * * Desc: 类简要描述 * * @author zhaoning */ public class XCollection implements Serializable { private String id; private String zhname; private String father; private String secu; private String actions; private XView[] view = new XView[0]; /** * id. * * @return the id */ public String getId() { return id; } /** * id. * * @param id the id to set */ public void setId(String id) { this.id = id; } /** * zhname. * * @return the zhname */ public String getZhname() { return zhname; } /** * zhname. * * @param zhname the zhname to set */ public void setZhname(String zhname) { this.zhname = zhname; } /** * father. * * @return the father */ public String getFather() { return father; } /** * father. * * @param father the father to set */ public void setFather(String father) { this.father = father; } /** * secu. * * @return the secu */ public String getSecu() { return secu; } /** * secu. * * @param secu the secu to set */ public void setSecu(String secu) { this.secu = secu; } /** * actions. * * @return the actions */ public String getActions() { return actions; } /** * actions. * * @param actions the actions to set */ public void setActions(String actions) { this.actions = actions; } /** * viewList. * * @return the viewList */ public XView[] getView() { return view; } /** * viewList. * * @param viewList the viewList to set */ public void setView(XView[] view) { this.view = view; } }