使用 JAXB 将内嵌的JAVA对象转换为 xml文件
- 1. 需求
- 2. 实现
- (1)FileDesc类
- (2)MetaFileXml类
- (3)生成对应的xml文件
1. 需求
获取一个目录下所有文件的元数据信息(文件名、大小、后缀等),并生成对应的xml文件;
思路:
创建一个 FileDesc
类用来存储文件的元数据信息;
创建一个 MetaFileXml
类,将 List<FileDesc> metaFile
的List作为属性,用来存储多个文件的元数据信息;
2. 实现
(1)FileDesc类
用来保存文件的元数据信息;
注意:类上面要加 @XmlRootElement
注解!
import javax.xml.bind.annotation.XmlRootElement;@XmlRootElement
public class FileDesc {private String file;private String parentPath;private String suffix;private String size;public FileDesc() {}public FileDesc(String file, String size) {this.file = file;this.size = size;}public FileDesc(String file, String parentPath, String suffix, String size) {this.file = file;this.parentPath = parentPath;this.suffix = suffix;this.size = size;}public String getFile() {return file;}public void setFile(String file) {this.file = file;}public String getParentPath() {return parentPath;}public void setParentPath(String parentPath) {this.parentPath = parentPath;}public String getSize() {return size;}public void setSize(String size) {this.size = size;}public String getSuffix() {return suffix;}public void setSuffix(String suffix) {this.suffix = suffix;}}
(2)MetaFileXml类
用来保存多个文件的元数据信息;
注意:类上面要加 @XmlRootElement
注解!
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;@XmlRootElement
public class MetaFileXml {private List<FileDesc> metaFile;public List<FileDesc> getMetaFile() {return metaFile;}public void setMetaFile(List<FileDesc> metaFile) {this.metaFile = metaFile;}
}
(3)生成对应的xml文件
注意:建立 JAXBContext 时是以 MetaFileXml
类来创建;
@Testpublic void testXml() throws Exception {// 1. 准备文件目录String filePath = "C:\\Users\\liziq\\Desktop\\zipTest02";File sourceFile = new File(filePath);// 2. 获取目录下所有文件List<File> files = (List<File>) FileUtils.listFiles(sourceFile, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);List<FileDesc> descList = new ArrayList<>();// 3. 将所有文件的元数据放入 List中for (File file : files) {long size = Files.size(Paths.get(file.getAbsolutePath()));String sizeString = String.valueOf(size);String suffix = FilenameUtils.getExtension(file.getName()).toLowerCase();descList.add(new FileDesc(file.getName(), file.getParent(), suffix, sizeString));}// 4. 将List<FileDesc> 添加到 fileDescXmlMetaFileXml fileDescXml = new MetaFileXml();fileDescXml.setMetaFile(descList);// 5. 生成xml格式信息try {// 创建JAXBContext对象JAXBContext jaxbContext = JAXBContext.newInstance(MetaFileXml.class);// 创建Marshaller对象Marshaller marshaller = jaxbContext.createMarshaller();// 设置Marshaller的属性,格式化输出marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);StringWriter writer = new StringWriter();writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");marshaller.marshal(fileDescXml, writer);// 6. 写入xml文件File fileDesc = new File("C:\\Users\\liziq\\Desktop\\meta.xml");FileWriter fw = new FileWriter(fileDesc);BufferedWriter bw = new BufferedWriter(fw);bw.write(writer.toString());bw.flush();bw.close();} catch (Exception e) {e.printStackTrace();}}
成功生成xml文件: