xml生成java bean类
xsd生成java bean
使用jdk内置 xjc.exe 程序生成
两种方式:
- 采用命令行模式
详细资料查看w3c
D:\ProgrammingSoftware\JDK\install\JDK1.8\bin\xjc.exe -p com.example.demo.panxg.jaxb.up.CAS003001xsd F:\baocai\bwmc\source\testv\demo\target\classes\SCHEMAS\up\CAS.003.001.xsd -d F:\baocai\bwmc\source\testv\demo\src\main\java
- 采用java程序生成 java自带的执行shell脚本功能
package com.example.demo.panxg.jaxb;
import org.apache.tomcat.jni.FileInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;
import javax.annotation.Resource;
import javax.xml.bind.JAXBContext;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
@Service
public class XsdConvertJavaBean {
public static void main(String[] args) throws FileNotFoundException {
System.out.println(System.getProperty("user.dir") + "\\demo\\src\\main\\java\\com\\example\\demo\\panxg\\jaxb\\down");
createXsdBeasn();
}
public static void createXsdBeasn() throws FileNotFoundException {
//获取jdk路径
String xjcExe = System.getenv("JAVA_HOME") + "\\bin\\xjc.exe";
//获取xsd文件/down
File downFiles = ResourceUtils.getFile("classpath:SCHEMAS/down");
//获取xsd文件/up
File upFiles = ResourceUtils.getFile("classpath:SCHEMAS/up");
File[] arrayDownFile = downFiles.listFiles();
for (File file : arrayDownFile) {
//开始生成java类
List<String> command2 = new ArrayList<>();
//xjc.exe
command2.add(xjcExe);
command2.add("-p");
//包名
command2.add("com.example.demo.panxg.jaxb.down." + file.getName().replaceAll("\\.", ""));
//xsd文件名
command2.add(file.getPath());
command2.add("-d");
//java文件存放目录
command2.add(System.getProperty("user.dir") + "\\demo\\src\\main\\java");
String join = join(command2, " ");
System.err.println(join);
execute(new ProcessBuilder(command2));
break;
}
File[] arrayUpFile = upFiles.listFiles();
for (File file : arrayUpFile) {
//开始生成java类
List<String> command2 = new ArrayList<>();
//xjc.exe
command2.add(xjcExe);
command2.add("-p");
//包名
command2.add("com.example.demo.panxg.jaxb.up." + file.getName().replaceAll("\\.", ""));
//xsd文件
command2.add(file.getPath());
command2.add("-d");
//java文件存放目录
command2.add(System.getProperty("user.dir") + "\\demo\\src\\main\\java");
String join = join(command2, " ");
System.err.println(join);
execute(new ProcessBuilder(command2));
break;
}
}
/**
* 将xml文件生成成java实体类
*
* @param trangLibPath 存放trang的路径
* @param xmlFileFolderPath 存放xml文件的文件夹
* @param packageName 生成的java实体类的包名
* @param javaFileFolderpath 生成后的java实体类的文件夹
* @throws Exception
*/
public static void createXmlBean(String trangLibPath, String xmlFileFolderPath, String packageName, String javaFileFolderpath) throws Exception {
// createXmlBean("C:\\Users\\lrxsz\\Desktop\\demo\\xml\\trang.jar", "C:\\Users\\lrxsz\\Desktop\\demo\\xml", "com.test.structs", "C:\\Users\\lrxsz\\Desktop\\demo\\xml\\created");
String javaHome = System.getenv("JAVA_HOME"); //获取jdk路径
if (!new File(javaHome).exists()) {
throw new Exception("没有配置jdk环境变量");
}
String java = javaHome + "\\bin\\java.exe";
String xjc = javaHome + "\\bin\\xjc.exe";
File file = new File(xmlFileFolderPath);
if (!file.exists() || !file.isDirectory()) {
throw new Exception("xml文件夹不存在,或此路径不是文件夹!");
}
//由于xjc无法指定不存在的目录,所以在这里判断一下,如果没有就创建一下新的目录
File path = new File(javaFileFolderpath);
if (!path.exists()) {
path.mkdirs();
}
File[] listFiles = file.listFiles();
//遍历文件夹下面的xml
for (File xmlFile : listFiles) {
if (xmlFile.getName().endsWith(".xml")) {
//开始生成xsd
String xmlFileName = xmlFile.getAbsolutePath();
String xsdFileName = xmlFileName.replace(".xml", ".xsd");
List<String> command = new ArrayList<String>();
command.add(java); //java.exe
command.add("-jar");
command.add(trangLibPath); //trang.jar
command.add(xmlFileName); //xml文件名
command.add(xsdFileName); //xsd文件名
System.out.println(join(command, " "));
execute(new ProcessBuilder(command));
//开始生成java类
List<String> command2 = new ArrayList<String>();
command2.add(xjc);//xjc.exe
command2.add("-p");
command2.add(packageName);//包名
command2.add(xsdFileName);//xsd文件名
command2.add("-d");
command2.add(javaFileFolderpath); //java文件存放目录
System.out.println(join(command2, " "));
execute(new ProcessBuilder(command2));
File xsdFile = new File(xsdFileName);
xsdFile.delete();//将xsd文件删除
}
}
}
private static void execute(ProcessBuilder pb) {
try {
Map<String, String> env = pb.environment();
env.clear();
env.put("LANG", "en_US.UTF-8"); //must set LANG to support chinese
pb.redirectErrorStream(true);
Process p = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream(), "GBK"));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
p.waitFor();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static String join(List<String> strs, String separator) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < strs.size(); i++) {
if (i > 0) {
sb.append(toString(separator));
}
sb.append(toString(strs.get(i)));
}
return sb.toString();
}
public static String toString(Object value) {
if (value == null) {
return "";
}
return value.toString();
}
}
package com.baosight.bsfc4.common.utils.message.utils;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;
import java.io.*;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.*;
public class XsdConvertJavaBean {
public static void main(String[] args) throws IOException {
String propertyPath = System.getProperty("user.dir");
String projectPath = propertyPath + "/ndco_branches/ndco/nd-message/src/main/java";
String downPath = propertyPath + "/ndco_branches/ndco/nd-message/src/main/java/com/baosight/bsfc4/common/utils/message/bean/down";
String upPath = propertyPath + "/ndco_branches/ndco/nd-message/src/main/java/com/baosight/bsfc4/common/utils/message/bean/up";
System.out.println(downPath);
System.out.println(upPath);
createXsdBeasn(projectPath,downPath, upPath);
}
public static void createXsdBeasn(String projectPath,String downPath, String upPath) throws IOException {
//获取jdk路径
String xjcExe = System.getenv("JAVA_HOME") + "\\bin\\xjc.exe";
//获取xsd文件/down
File downFiles = new File(downPath + "/xsd");
File[] arrayDownFile = downFiles.listFiles();
for (File file : arrayDownFile) {
//开始生成java类
List<String> command = new ArrayList<>();
//xjc.exe
command.add(xjcExe);
command.add("-p");
//包路径
String javaPackageName = file.getName().substring(0,file.getName().length()-4).replaceAll("\\.","").toUpperCase();
command.add("com.baosight.bsfc4.common.utils.message.bean.down." + javaPackageName);
File javaPackage = new File(downPath + "/" + javaPackageName);
if (!javaPackage.exists()) {
javaPackage.mkdir();
}
//xsd文件路径
command.add(file.getPath());
command.add("-d");
//java文件存放目录
command.add(projectPath);
String join = join(command, " ");
System.err.println(join);
execute(new ProcessBuilder(command));
// break;
}
//获取xsd文件/up
File upFiles = new File(upPath+"/xsd");
File[] arrayUpFile = upFiles.listFiles();
for (File file : arrayUpFile) {
//开始生成java类
List<String> command = new ArrayList<>();
//xjc.exe
command.add(xjcExe);
command.add("-p");
//包名
String javaPackageName = file.getName().substring(0,file.getName().length()-4).replaceAll("\\.","").toUpperCase();
command.add("com.baosight.bsfc4.common.utils.message.bean.up." + javaPackageName);
File javaPackage = new File(upPath + "/" + javaPackageName);
if (!javaPackage.exists()) {
javaPackage.mkdir();
}
//xsd文件
command.add(file.getPath());
command.add("-d");
//java文件存放目录
command.add(projectPath);
String join = join(command, " ");
System.err.println(join);
execute(new ProcessBuilder(command));
// break;
}
}
private static void execute(ProcessBuilder pb) {
try {
Map<String, String> env = pb.environment();
env.clear();
//must set LANG to support chinese
env.put("LANG", "en_US.UTF-8");//这个没用
env.put("JAVA_TOOL_OPTIONS", "-Dfile.encoding=UTF-8");//这个有用
pb.redirectErrorStream(true);
Process p = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream(), "GBK"));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
p.waitFor();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static String join(List<String> strs, String separator) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < strs.size(); i++) {
if (i > 0) {
sb.append(toString(separator));
}
sb.append(toString(strs.get(i)));
}
return sb.toString();
}
public static String toString(Object value) {
if (value == null) {
return "";
}
return value.toString();
}
}