xml格式转换为Bean
Posted 情似雨餘黏地絮
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xml格式转换为Bean相关的知识,希望对你有一定的参考价值。
/** * */ package com.hdrs.ris.law2.large.xml.utils; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.util.ArrayList; import java.util.List; /** * 该程序可以从xml中提出生成对应的beans,方便jaxb使用 * * 解析说明: * 1、如果使用"/>"做为结束,元素的开始和结束必须在一行. * 2、如果使用</元素>结束,元素的开始和结束标签不能在一行。 * 3、如果某个元素为list,这元素中添加属性list(类型)用以标记,如<users list(user)> * * @author liuwenbin */ public class CreateBeansFromXml { private static List<String> createFileNames=new ArrayList<String>(); private static List<String> error=new ArrayList<String>(); private static String listNameString=""; /** * 创建java beans的主方法 * @param xmlFile xml文件的路径和文件名 * @param outBeansPath 生成beans存放的路径 */ public static void createBeans(String xmlFile,String outBeansPath) throws Exception { File file=new File(xmlFile); BufferedReader bufferedReader=new BufferedReader(new FileReader(file)); String line=bufferedReader.readLine(); String makeFile=""; String parent=""; List<String> parents=new ArrayList<String>(); boolean isHead=true; List<String> listElement=new ArrayList<String>(); while(null!=line){ if(line.contains("<?xml")){ line=bufferedReader.readLine(); continue; } makeFile=""; line=line.trim(); if(line.startsWith("<")){ if(line.contains("/>")){ if(line.contains(" ")){ makeFile=line.substring(line.indexOf("<")+1,line.indexOf(" ")); parent=parents.get(parents.size()-1); fileWrite(outBeansPath+firstToUp(parent), writeElement(makeFile)); //生成java文件 if(createFileNames.contains(outBeansPath+firstToUp(makeFile))){ error.add(makeFile); } createFileNames.add(outBeansPath+firstToUp(makeFile)); fileWrite(outBeansPath+firstToUp(makeFile), makeJavaHead(false,isHead,makeFile)); String[] contents=line.substring(line.indexOf(" ")+1,line.indexOf("/>")).split(" "); for (String string : contents) { if(string.contains("=")){ fileWrite(outBeansPath+firstToUp(makeFile), writeAttribute(string.split("=")[0])); } } }else{ makeFile=line.substring(line.indexOf("<")+1,line.indexOf("/>")); } }else if(line.contains("</")){ makeFile=line.substring(line.indexOf("</")+2,line.lastIndexOf(">")); if(line.contains("<"+makeFile+">")){ parent=parents.get(parents.size()-1); if(!listNameString.contains("list("+makeFile)){ fileWrite(outBeansPath+firstToUp(parent), writeElementString(makeFile)); } }else{ parents.remove(makeFile); } }else{ line=line.substring(line.indexOf("<")+1,line.indexOf(">")); if(line.contains(" ")){ makeFile=line.substring(0,line.indexOf(" ")); }else{ makeFile=line; } if(!isHead){ parent=parents.get(parents.size()-1); if(!listElement.contains(makeFile)){ fileWrite(outBeansPath+firstToUp(parent), writeElement(makeFile)); } } parents.add(makeFile); if(createFileNames.contains(outBeansPath+firstToUp(makeFile))){ error.add(makeFile); } createFileNames.add(outBeansPath+firstToUp(makeFile)); if(line.contains("list(")){ fileWrite(outBeansPath+firstToUp(makeFile), makeJavaHead(true,isHead,makeFile)); }else{ fileWrite(outBeansPath+firstToUp(makeFile), makeJavaHead(false,isHead,makeFile)); } isHead=false; if(line.contains(" ")){ String attrs[] =line.substring(line.indexOf(" ")+1).split(" "); for (String attr : attrs) { if(attr.contains("=")){ attr=attr.split("=")[0]; if(attr.startsWith("list(")){ listNameString=attr+"||"; attr=attr.substring(attr.indexOf("list(")+5,attr.indexOf(")")); listElement.add(attr); fileWrite(outBeansPath+firstToUp(makeFile), writeElementList(attr)); }else{ fileWrite(outBeansPath+firstToUp(makeFile), writeAttribute(attr)); } }else if (attr.startsWith("list(")) { listNameString=attr+"||"; attr=attr.substring(attr.indexOf("list(")+5,attr.indexOf(")")); listElement.add(attr); fileWrite(outBeansPath+firstToUp(makeFile), writeElementList(attr)); } } } } } line=bufferedReader.readLine(); } bufferedReader.close(); //为生成的文件添加getter和setter方法 File fileDir=new File(outBeansPath); File[] files=fileDir.listFiles(); BufferedReader bufferedReader2=null; String[] contSplit=null; for (File file2 : files) { if(file2.getName().contains(".svn")){ continue; } bufferedReader2=new BufferedReader(new FileReader(file2)); line=bufferedReader2.readLine(); while(null!=line){ if(line.contains("private")){ contSplit=line.split(" "); writeGetterAndSetterToFile(file2.getAbsolutePath(), writeGetterAndSetter(contSplit[2].substring(0,contSplit[2].length()-1), contSplit[1])); } line=bufferedReader2.readLine(); } writeGetterAndSetterToFile(file2.getAbsolutePath(), "}"); } bufferedReader2.close(); System.out.println("======================全部结束====================="); System.out.println("一共创建"+createFileNames.size()+"个文件:"); for (String string : createFileNames) { System.out.println(string+".java"); } System.out.println("---------------------------------------------------"); if(error.size()!=0){ System.out.println("产生错误:"); for (String string : error) { System.out.println(string+"重复,可能为list,其父节点应该加上\"list("+string+")\"标识,并删除重复段"); } } } /** * 写文件 * @param fileName 文件名 * @param content 内容 * @throws Exception */ private static void fileWrite(String fileName,String content) throws Exception{ File file=new File(fileName+".java"); if(!file.exists()){ file.createNewFile(); } BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter(file,true)); bufferedWriter.write(content); bufferedWriter.flush(); bufferedWriter.close(); } /** * 将get和set方法写到文件中 * @param fileName * @param content * @throws Exception */ private static void writeGetterAndSetterToFile(String fileName,String content) throws Exception{ File file=new File(fileName); if(!file.exists()){ file.createNewFile(); } BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter(file,true)); bufferedWriter.write(content); bufferedWriter.flush(); bufferedWriter.close(); } /** * 生成java beans文件头 * @param importList 是否是list * @param isHead 是否是头部 * @param Element 元素名称 * @return */ private static String makeJavaHead(boolean importList,boolean isHead,String Element){ StringBuffer buffer=new StringBuffer(); buffer.append("package com.hdrs.ris.law2.large.xml.add;\r\n\r\n"); buffer.append("import com.thoughtworks.xstream.annotations.XStreamAlias;\r\n"); if(importList){ buffer.append("import java.util.List;\r\n"); } if(isHead){ buffer.append("@XStreamAlias(\""); buffer.append(Element); buffer.append("\")\r\n"); } buffer.append("public class "); buffer.append(firstToUp(Element)); buffer.append(" {\r\n"); return buffer.toString(); } /** * 写元素(字符串) * @param Element 元素 * @return */ private static String writeElementString(String Element){ StringBuffer buffer=new StringBuffer(); buffer.append("\[email protected](\""); buffer.append(Element); buffer.append("\")\r\n"); buffer.append("\tprivate "); buffer.append("String"); buffer.append(" "); buffer.append(Element); buffer.append(";\r\n\r\n"); return buffer.toString(); } /** * 写元素(对象) * @param Element 元素 * @return */ private static String writeElement(String Element){ StringBuffer buffer=new StringBuffer(); buffer.append("\[email protected](\""); buffer.append(Element); buffer.append("\")\r\n"); buffer.append("\tprivate "); buffer.append(firstToUp(Element)); buffer.append(" "); buffer.append(Element); buffer.append(";\r\n\r\n"); return buffer.toString(); } /** * 写元素(list) * @param Element 元素 * @return */ private static String writeElementList(String Element){ StringBuffer buffer=new StringBuffer(); buffer.append("\[email protected](\""); buffer.append(Element); buffer.append("\")\r\n"); buffer.append("\tprivate List<"); buffer.append(firstToUp(Element)); buffer.append("> "); buffer.append(Element); buffer.append("s;\r\n\r\n");//如果是list加”S“ return buffer.toString(); } /** * 写属性 * @param attr 属性名称 * @return */ private static String writeAttribute(String attr){ StringBuffer buffer=new StringBuffer(); buffer.append("\[email protected]"); buffer.append("\r\n"); buffer.append("\tprivate "); buffer.append("String"); buffer.append(" "); buffer.append(attr); buffer.append(";\r\n\r\n"); return buffer.toString(); } public static String writeGetterAndSetter(String param,String type){ //Getter StringBuffer buffer=new StringBuffer(); buffer.append("\r\n\tpublic "); buffer.append(type); buffer.append(" get"); buffer.append(firstToUp(param)); buffer.append("() {\r\n"); buffer.append("\t\treturn "); buffer.append(param); buffer.append(";\r\n"); buffer.append("\t}\r\n"); //Setter buffer.append("\tpublic void "); buffer.append("set"); buffer.append(firstToUp(param)); buffer.append("("); buffer.append(type); buffer.append(" "); buffer.append(param); buffer.append(") {\r\n"); buffer.append("\t\tthis."); buffer.append(param); buffer.append(" = "); buffer.append(param); buffer.append(";\r\n"); buffer.append("\t}\r\n"); return buffer.toString(); } /** * 首字母大写 * @param word 单词 * @return */ private static String firstToUp(String word){ return word.replaceFirst(word.substring(0, 1), word.substring(0, 1).toUpperCase()); } }
以上是关于xml格式转换为Bean的主要内容,如果未能解决你的问题,请参考以下文章