JOX解析xml文件

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;
}
}

通过JOX来实现从JavaBeans到XML文件的相互转换。 为了灵活的满足Web应用和Web services需求的变化,Java和XML的轻便性和可扩展性使它们成为解决这一问题的理想选择。SAX (Simple API for XML), DOM (document.nbspObject Model), XSL (Extensible Stylesheet Language), XSLT (XSL Transformations), SOAP (Simple Object Access Protocol), and BML (Bean Markup Language)是XML领域内的相关技术。本文集合了Java和XML轻便和可扩展的优点,但又不需要开发人员了解上述的相关技术。 在Java分布式应用中使用远程方法调用(RMI),而不是直接用底层的socket或其它网络链接代码。EJB技术也使开发人员从transaction,recovery, activation等底层机制中解放出来。同样,使用本文的JavaBean-XML映射组件,开发人员也不用直接处理与XML有关的APIs。 1.先定义javaBean package com.wutka.jox.test; import com.wutka.jox. * ; import java.util. * ; public class TestBean implements java.io.Serializable { protected int foo; protected String bar; protected java.util.Date baz; protected Vector thingies; protected TestSubbean subbean; public TestBean() { bar = "" ; baz = new Date(); thingies = new Vector(); } public int getFoo() { return foo; } public void setFoo( int aFoo) { foo = aFoo; } public String getBar() { return bar; } public void setBar(String aBar) { bar = aBar; } public java.util.Date getBaz() { return baz; } public void setBaz(java.util.Date aBaz) { baz = aBaz; } public TestSubbean getSub() { return subbean; } public void setSub(TestSubbean aSub) { subbean = aSub; } public String[] getThingies() { String[] retThingies = new String[thingies.size()]; if (thingies.size() > 0 ) thingies.copyInto(retThingies); return retThingies; } public void setThingies(String[] newThingies) { thingies = new Vector(newThingies.length); for ( int i = 0 ; i < newThingies.length; i ++ ) { thingies.addElement(newThingies[i]); } } public String getThingies( int i) { return (String) thingies.elementAt(i); } public void setThingies( int i, String thingy) { thingies.setElementAt(thingy, i); } public String toString() { StringBuffer ret = new StringBuffer( " foo= " + foo + " ;bar= " + bar + " ;baz= " + baz.toString() + " ;thingies= " ); for ( int i = 0 ; i < thingies.size(); i ++ ) { if (i > 0 ) ret.append( " , " ); ret.append((String) thingies.elementAt(i)); } ret.append( " ;sub= " ); ret.append(subbean.toString()); return ret.toString(); } } 2.xml文件 <? xml version="1.0" ?> < MarkTest > < thingies >网 </ thingies > < thingies > 载 </ thingies > < thingies > 下 </ thingies > < thingies > 托 </ thingies > < thingies > 灵 </ thingies > < foo > 5 </ foo > < baz > 6/25/00 12:46 AM </ baz > < bar > This is the website value </ bar > < sub > < age > 1 </ age > < name > wangdei </ name > </ sub > </ MarkTest > 下面的程序是把xml转为成javaBean package com.wutka.jox.test; import com.wutka.jox. * ; import java.io. * ; public class TestDeser { public static void main(String[] args) { try { FileInputStream in = new FileInputStream( " bean.xml " ); JOXBeanInputStream joxIn = new JOXBeanInputStream(in); TestBean testBean = (TestBean) joxIn.readObject( TestBean. class ); System.out.println(testBean); } catch (Exception exc) { exc.printStackTrace(); } } } 下面的程序是把javaBean转为成xml package com.wutka.jox.test; import com.wutka.jox. * ; import java.io. * ; public class TestSer { public static void main(String[] args) { try { TestBean b = new TestBean(); b.setFoo( 5 ); b.setBar( " This is the bar value " ); b.setThingies( new String[] { " 网 " , " 载 " , " 下 " , " 托 " , " 灵 " } ); TestSubbean sub = new TestSubbean(); sub.setName( " Mark " ); sub.setAge( 35 ); b.setSub(sub); FileOutputStream fileOut = new FileOutputStream( " bean.xml " ); JOXBeanOutputStream joxOut = new JOXBeanOutputStream(fileOut); joxOut.writeObject( " MarkTest " , b); joxOut.close(); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值