package betwixt;
import java.beans.IntrospectionException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.betwixt.io.BeanReader;
import org.apache.commons.betwixt.io.BeanWriter;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class TestXML {
public static void main(String args[]) throws Exception {
Child children = new Child();
Map<String,String> scoreMap = new HashMap<String,String>();
scoreMap.put("English", "90");
scoreMap.put("Math", "88");
children.setId(1);
children.setChildrenName("zhaolixing");
children.setScoreMap(scoreMap);
Child children2 = new Child();
Map<String,String> scoreMap2 = new HashMap<String,String>();
scoreMap2.put("English", "100");
scoreMap2.put("Math", "80");
children2.setId(1);
children2.setChildrenName("zhaoliyan");
children2.setScoreMap(scoreMap2);
Father father = new Father();
father.setFathername("zbj");
ArrayList<Child> childrens = new ArrayList<Child>();
childrens.add(children);
childrens.add(children2);
father.setChildren(childrens);
Child child1 = new Child();
System.out.println(ConvertBean2StrFromConfig(children, "child",
"/betwixt/children.betwixt"));
child1 = (Child) getObjFromXml(ConvertBean2StrFromConfig(children,
"child", "/betwixt/children.betwixt"), child1,
"/betwixt/children.betwixt");
// child1= (Child)
// getObjFromXml(ConvertBean2StrFromConfig(children,"child"),child1);
System.out.println(child1.getChildrenName());
Father fa2 = new Father();
//System.out.println(ConvertBean2Str(father, "father"));
System.out.println(ConvertBean2StrFromConfig(father,"father","/betwixt/Father1.betwixt"));
fa2 = (Father)getObjFromXml(ConvertBean2StrFromConfig(father,"father","/betwixt/Father1.betwixt"),fa2,"/betwixt/Father1.betwixt");
System.out.println(fa2.getFathername());
}
/**
* @author zbj
* desc:此方法是将一个JavaBean转成xml格式字符串,
* 有两种途径:一是使用默的方法,一种是根据自己定义的格式去生成xml.下面分别用这两种方法转换Child和
* Father这两个JavaBean
* @throws IntrospectionException
* @throws SAXException
* @throws IOException
*/
public static String ConvertBean2Str(Object obj,String element) throws IOException, SAXException, IntrospectionException{
StringWriter outputWriter = new StringWriter();
outputWriter.write("<?xml version='1.0' encoding=\"UTF-8\"?>\n");
BeanWriter beanWriter = new BeanWriter(outputWriter);
beanWriter.getBindingConfiguration().setMapIDs(false);
beanWriter.enablePrettyPrint();
// beanWriter.write(element, obj);
beanWriter.write(obj);
//System.out.println(outputWriter.toString());
String str = outputWriter.toString();
beanWriter.close();
return str;
}
public static String ConvertBean2StrFromConfig(Object obj,String element,String path) throws IOException, SAXException, IntrospectionException{
StringWriter outputWriter = new StringWriter();
outputWriter.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
BeanWriter beanWriter = new BeanWriter(outputWriter);
beanWriter.getBindingConfiguration().setMapIDs(false);
beanWriter.enablePrettyPrint();
InputStream s = obj.getClass().getResourceAsStream(path);
InputSource config = new InputSource(s);
beanWriter.getXMLIntrospector().register(config);
beanWriter.write(obj);
String strTemp = outputWriter.toString();
outputWriter.close();
//strTemp = replaceStr(strTemp);
return strTemp;
}
public static Object getObjFromXml(String strXml,Object obj,String path) throws Exception{
if(strXml == null || strXml.compareTo("") == 0){
return null;
}
BeanReader beanReader = new BeanReader();
beanReader.getXMLIntrospector().getConfiguration()
.setAttributesForPrimitives(false);
beanReader.getBindingConfiguration().setMapIDs(false);
InputStream s = obj.getClass().getResourceAsStream(path);
InputSource config = new InputSource(s);
beanReader.getXMLIntrospector().register(config);
beanReader.registerBeanClass(obj.getClass());
InputStream xml = new ByteArrayInputStream(strXml.getBytes("UTF-8"));
obj = (Object) beanReader.parse(xml);
return obj;
}
}