关键词不能为空

当前您在: 主页 > 英语 >

JAVA生成XML文件

作者:高考题库网
来源:https://www.bjmy2z.cn/gaokao
2021-02-28 05:22
tags:

-

2021年2月28日发(作者:lone)


package




import



import


tFoundException;


import


tputStream;


import


ption;


import


Stream;



import


nt;


import


t;


import



import


putter;




public



class


FileDemo {




/**





*



@param



args





*/




public



static



void


main(String[] args) {




//


TODO


Auto-generated method stub





String filename=



;




File file=


new


File(filename);




/**






*



判断文件是否存在


.


如果文件不存在则生成一个文件






*



则生成一个文件;反之则先删除原有文件,生成一个新的文件






*/








if


(()){





try


{






();





}


catch


(Exception e) {






//


TODO


Auto-generated catch block







tackTrace();





}




}





try


{






NewFile();





}


catch


(IOException e) {






//


TODO


Auto- generated catch block







tackTrace();





}









//


创建根节点






Element root=


new


Element(



);





//


根节点添加到文档中






Document document=


new


Document(root);





//


添加工单基本信息






//


创建工单基本信息的节点






Element baseinfo=


new


Element(



);





//


创建工单编号节点






Element formOid=


new


Element(



);





//


添加工单编号节点内容






t(< /p>



工单编号



);





//< /p>


将工单编号节点添加到基本信息节点下


































































































































































tent(formOid);


//


创建任务名称节点



Element taskName=


new


Element(



);


//


添加任务名称内容



t(



任务名称



);


//


将任务名称添加到基本信息中



tent(taskName);


//


创建工单类型



Element leafletType=


new


Element(



);


t(



工单类型



);


tent(leafletType);


//


涉及网元



Element metadataName=


new


Element(



);


t(



涉及网元



);


tent(metadataName);


//


申请人



Element applicant=


new


Element(



);


t(



申请人



);


tent(applicant);


//


申请时间



Element applyTime=


new


Element(



);


t(



申请时间



);


tent(applyTime);


//


问题描述



Element problemDesc=


new


Element(



);


t(



申请时间



);


tent(problemDesc);


//


附件



Element accessory=


new


Element(



);


t(



附件



);


tent(accessory);


//


调整原因



Element AdjustReason=


new


Element(



);


//


创建调整原因的根节点



Element items=


new


Element(



);


//


创建一个原因信息



Element item=


new


Element(



);


//


小区名



Element villagename=


new


Element(



);


t(



小区名



);


tent(villagename);


//LAC



Element lac=


new


Element(



);


t(



);


tent(lac);


//CI



Element ci=


new


Element(



);


t(



);


tent(ci);


//bsc



Element bsc=


new


Element(



);


t(



);


tent(bsc);




















































}


















































}


















































//msc



Element msc=


new


Element(



);


t(



);


tent(msc);


//


行政区域



Element canton=


new


Element(



);


t(



);


tent(canton);


//


设备类型



Element devicetype=


new


Element(



);


t(



设备类型



);


tent(devicetype);


//


问题时间



Element problemtime=


new


Element(



);


t(



问题时间



);


tent(problemtime);


//


指标或参数名



Element parametername=


new


Element(



);


t(



指标或参数名



);


tent(parametername);


//


指标或参数值



Element parametervalues=


new


Element(



);


t(



指标或参数值



);


tent(parametervalues);



tent(item);


tent(items);


tent(AdjustReason);



//


将工单编号节点添加到根节点下



tent(baseinfo);


//


设置


xml


文件格式


---


选用这用格式可以是生成的


xml


文件自动换行 同时缩进



Format format=Format.


getPrettyFormat


();



XMLOutputter xmlout=


new


XMLOutputter(format);




OutputStream out;


try


{



out =


new


FileOutputStream(file,


true


);



//


将内容写到文件中




(document, out);


}


catch


(Exception e) {



//


TODO


Auto-generated catch block




tackTrace();


}










推荐群组


: JSF



更多相关推荐



最近的项目中用到了< /p>


XML


,所以就写了个对象转换为


XML


文件的通用类。



Java


代码





package







import




import tionTargetException;




import




import DateFormat;




import p;




import







/**




*




* Description:


反射机制工具类,根据传入的


Obj






*















为生成


xml


文件提取所有属性和属性对应的值












*




*





*





* @author Administrator




* @since



2008-12-1




* @version




*/




public class ReflectionUtil {











//


所有的系统类型









private static String[] allSysDefinedType = new String[] {






















































































private static String dateFormatString =











//< /p>


使用不可以是属性名前缀的


*


作为标识是 否是用户自定义的类型









private static String userDefiendTypePrefix =











/**








*


根据传入的对象将对象的属性和该属性对应的值放入


map








* @param



obj








*












待映射属性和属性值的对象









* @param



unReflectProp








*












不需要映射属性和属性值的所有属性;









*












eg. CbsTargetTime


对象中有


DATE_FORMAT


不需要映射则在该参数中添 加









*

















TE_FORMAT








*










* @return Map








*












key = property name value = property value;








*












(


如果属性为一个用户自定义类型的对象那么









*












key = '*' + 'user defined type';








*












value = map;map



key


是属性,

value


是值,依次循环)









*/








public Map transformObjData2Map(Object obj,
















String[] unReflectProp) {












Map rootMap = new HashMap();












Map dataMap = new HashMap();












(userDefiendTypePrefix + ss().getSimpleName(),




















dataMap); //


使用不可以是属性名前缀的


*


作为标识是否是用户自定义的类型













Pro(obj, unReflectProp, dataMap, ss().getName());












return rootMap;








}











/**








*


得到传入对象的每个属性域,









*


根据每个属性域得到属性域对应的值









*









* @param obj









*










待映射得到属性和值的对象









*

















* @param



unReflectProp








*










不需要映射属性和属性值的所有属性;









*

















* @param



className









*










对象的类型名,如果传入对象为空时,用其新建一个对象









* @param dataMap








*










key=


属性,


value=


属性的值









*/











@SuppressWarnings(








private void getObjPro(Object obj, String[] unReflectProp, Map dataMap,
















String className) {












try {
















Class clas;
















if (obj == null) {




















clas = e(className);




















obj = tance();
















} else {




















clas = e(ss().getName());
















}



















//< /p>


得到


obj


类的所有属性

















Field[] fileds = laredFields();
















for (Field field : fileds) {




















String fieldName = e();




















String fieldType = e().getName();




















//< /p>


如果该属性是不需要映射的属性则跳出循环





















if (!reflectPropOrNot(ss().getSimpleName() +




























+ fieldName, unReflectProp)) {
























continue;




















}




















//< /p>


属性名的第一个字母大写,与


get


或者


is


组成方法名





















String firstCharUpper = ing(0, 1).toUpperCase()




























+ ing(1, ());




















Method method;




















//


如果 是


boolean


型则方法名为


is*


;反之为


get*




















if (isBooleanType(fieldType)) {
























method = hod(




















} else {
























method = hod(




















}























if (isSysDefinedType(fieldType)) {
























//


如果是系统类型则添加进入


map
























String formatDateStr = isTimeType(fieldType, method, obj);
























if (formatDateStr != null) {




























(fieldName, formatDateStr);




























continue;
























}
























(fieldName,
































(obj, null) == null ?








































.invoke(obj, null));























} else {
























//


如果不是系统类型对象则


key = * +


属性名





value = map


;循环迭代

























Map dateMap2 = new HashMap();
























Pro((obj, null), unReflectProp,
































dateMap2, fieldType);
























(userDefiendTypePrefix
































+ e().getSimpleName(), dateMap2);




















}



















}















} catch (Exception e) {
















// TODO Auto-generated catch block
















n(


映射 属性和属性所对应值出现异常!


















tackTrace();















}











}











/**








*


验证属性是否是要通过反射机制得到名称和值









* @param fieldName








*















待验证的属性









* @return








*









true



要利用反射机制的到属性名和值









*









false


不需要利用反射机制的到属性名和值









*/








private boolean reflectPropOrNot(String fieldName, String[] unReflectProp) {












if (unReflectProp == null) {
















return true;












}












for (String propName : unReflectProp) {
















if ((fieldName)) {




















return false;
















}












}












return true;








}











/**








*


验证属性的类型是否是系统定义的类型









* @param fieldType








*














待验证类型的名称









* @return








*









true



是系统定义的对象









*









false


用户定义的对象









*/








private boolean isSysDefinedType(String fieldType) {












for (String type : allSysDefinedType) {
















if ((type)) {




















return true;
















}












}












return false;








}











/**








*


如果该属性是日期属性则根据要 输出的类型返回字符串,反之返回


null








* @param fileType








* @param method








* @param obj








* @return








* @throws IllegalAccessException








* @throws InvocationTargetException








*/








private String isTimeType(String fileType, Method method, Object obj)
















throws IllegalAccessException, InvocationTargetException {












if ((




















|| (
















if ((obj, null) == null) {




















return null;
















}
















SimpleDateFormat sdf = new SimpleDateFormat(dateFormatString);
















return ((obj, null));












} else {
















return null;












}








}











public boolean isBooleanType(String fieldType) {












if ((




















|| (



-


-


-


-


-


-


-


-



本文更新与2021-02-28 05:22,由作者提供,不代表本网站立场,转载请注明出处:https://www.bjmy2z.cn/gaokao/678863.html

JAVA生成XML文件的相关文章

  • 余华爱情经典语录,余华爱情句子

    余华的经典语录——余华《第七天》40、我不怕死,一点都不怕,只怕再也不能看见你——余华《第七天》4可是我再也没遇到一个像福贵这样令我难忘的人了,对自己的经历如此清楚,

    语文
  • 心情低落的图片压抑,心情低落的图片发朋友圈

    心情压抑的图片(心太累没人理解的说说带图片)1、有时候很想找个人倾诉一下,却又不知从何说起,最终是什么也不说,只想快点睡过去,告诉自己,明天就好了。有时候,突然会觉得

    语文
  • 经典古训100句图片大全,古训名言警句

    古代经典励志名言100句译:好的药物味苦但对治病有利;忠言劝诫的话听起来不顺耳却对人的行为有利。3良言一句三冬暖,恶语伤人六月寒。喷泉的高度不会超过它的源头;一个人的事

    语文
  • 关于青春奋斗的名人名言鲁迅,关于青年奋斗的名言鲁迅

    鲁迅名言名句大全励志1、世上本没有路,走的人多了自然便成了路。下面是我整理的鲁迅先生的名言名句大全,希望对你有所帮助!当生存时,还是将遭践踏,将遭删刈,直至于死亡而

    语文
  • 三国群英单机版手游礼包码,三国群英手机单机版攻略

    三国群英传7五神兽洞有什么用那是多一个武将技能。青龙飞升召唤出东方的守护兽,神兽之一的青龙。玄武怒流召唤出北方的守护兽,神兽之一的玄武。白虎傲啸召唤出西方的守护兽,

    语文
  • 不收费的情感挽回专家电话,情感挽回免费咨询

    免费的情感挽回机构(揭秘情感挽回机构骗局)1、牛牛(化名)向上海市公安局金山分局报案,称自己为了挽回与女友的感情,被一家名为“实花教育咨询”的情感咨询机构诈骗4万余元。

    语文