Java通过JAXB转换xml文件以及实体类
程序员文章站
2022-05-24 18:45:47
Java通过JAXB以xml的形式保存实体类数据到本地首先导入包 javax.xml.bind jaxb-api 2.3.1 XML文件的结构
Java通过JAXB转换xml文件以及实体类
首先导入包
<dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.1</version> </dependency>
XML文件的结构
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <configuration> <tblName>X_FRM_DT_HOE533_TBL</tblName> <pageRow>11</pageRow> <column> <tag>TGA00020</tag> <row>3</row> <col>15</col> <eTaxMax>1</eTaxMax> <cTaxMax>1</cTaxMax> <rowStep>0</rowStep> <colStep>0</colStep> <chld/> <prop/> <dataType>yymmdd</dataType> <fldLength1/> <fldLength2/> <maxData/> <minData/> <updateType/> <inOutFlag>O</inOutFlag> <itemName>TGA00020名</itemName> </column> <column> <tag>TGA00030</tag> <row>4</row> <col>15</col> <eTaxMax>1</eTaxMax> <cTaxMax>1</cTaxMax> <rowStep>0</rowStep> <colStep>0</colStep> <chld/> <prop/> <dataType>yymmdd</dataType> <fldLength1/> <fldLength2/> <maxData/> <minData/> <updateType/> <inOutFlag>O</inOutFlag> <itemName>TGA00030名</itemName> </column> </configuration>
实体类
package pojo; import java.util.List; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; // XmlRootElement:用于标注*类或枚举,会被保留到运行时。表示xml的根元素,有name和namespace两个方法,可以自定义xml的根元素名称和命名空间,name默认会使用类名,namespace默认为空。 @XmlRootElement(name="configuration") public class IOXmlFileDto { private String tblName; private String pageRow; private List<IOColumnDto> columns; public IOXmlFileDto() { super(); } public String getTblName() { return tblName; } public void setTblName(String tblName) { this.tblName = tblName; } public String getPageRow() { return pageRow; } public void setPageRow(String pageRow) { this.pageRow = pageRow; } // 用于标注属性、方法或者参数,会被保留到运行时。用于将java属性映射为xml节点,可以通过name来修改节点的名称 @XmlElement(name="column") public List<IOColumnDto> getColumns() { return columns; } public void setColumns(List<IOColumnDto> columns) { this.columns = columns; } @Override public String toString() { return "IOXmlFileDto [tblName=" + tblName + ", pageRow=" + pageRow + ", columns=" + columns + "]"; } }
package pojo; public class IOColumnDto { private String tag; private String row; private String col; private String eTaxMax; private String cTaxMax; private String rowStep; private String colStep; private String chld; private String prop; private String dataType; private String fldLength1; private String fldLength2; private String maxData; private String minData; private String updateType; private String inOutFlag; private String itemName; public IOColumnDto() { super(); } public String getTag() { return tag; } public void setTag(String tag) { this.tag = tag; } public String getRow() { return row; } public void setRow(String row) { this.row = row; } public String getCol() { return col; } public void setCol(String col) { this.col = col; } public String geteTaxMax() { return eTaxMax; } public void seteTaxMax(String eTaxMax) { this.eTaxMax = eTaxMax; } public String getcTaxMax() { return cTaxMax; } public void setcTaxMax(String cTaxMax) { this.cTaxMax = cTaxMax; } public String getRowStep() { return rowStep; } public void setRowStep(String rowStep) { this.rowStep = rowStep; } public String getColStep() { return colStep; } public void setColStep(String colStep) { this.colStep = colStep; } public String getChld() { return chld; } public void setChld(String chld) { this.chld = chld; } public String getProp() { return prop; } public void setProp(String prop) { this.prop = prop; } public String getDataType() { return dataType; } public void setDataType(String dataType) { this.dataType = dataType; } public String getFldLength1() { return fldLength1; } public void setFldLength1(String fldLength1) { this.fldLength1 = fldLength1; } public String getFldLength2() { return fldLength2; } public void setFldLength2(String fldLength2) { this.fldLength2 = fldLength2; } public String getMaxData() { return maxData; } public void setMaxData(String maxData) { this.maxData = maxData; } public String getMinData() { return minData; } public void setMinData(String minData) { this.minData = minData; } public String getUpdateType() { return updateType; } public void setUpdateType(String updateType) { this.updateType = updateType; } public String getInOutFlag() { return inOutFlag; } public void setInOutFlag(String inOutFlag) { this.inOutFlag = inOutFlag; } public String getItemName() { return itemName; } public void setItemName(String itemName) { this.itemName = itemName; } }
实现工具类
package pojo; import java.io.File; import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; public class XmlUtilDto<T> { private String xmlName; private String xmlPath; public void writeXml(T data, Class<T> cls) { try { File file = new File(xmlPath); if (!file.exists()) { file.mkdir(); } file = new File(xmlPath + File.separator + xmlName); file.createNewFile(); JAXBContext context = JAXBContext.newInstance(cls); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); m.marshal(data, file); } catch (Exception e) { e.printStackTrace(); } } @SuppressWarnings("unchecked") public T readXml(Class<T> cls) { try { File file = new File(xmlPath + File.separator + xmlName); if (!file.exists()) { return null; } JAXBContext context = JAXBContext.newInstance(cls); Unmarshaller um = context.createUnmarshaller(); return (T) um.unmarshal(file); } catch (Exception e) { e.printStackTrace(); return null; } } public void setXmlName(String xmlName) { this.xmlName = xmlName; } public void setXmlPath(String xmlPath) { this.xmlPath = xmlPath; } }
测试
package test; import pojo.IOXmlFileDto; import pojo.XmlUtilDto; public class test002 { public static void main(String[] args) { XmlUtilDto<IOXmlFileDto> xmlDto = new XmlUtilDto<IOXmlFileDto>(); xmlDto.setXmlName("b039172.xml"); xmlDto.setXmlPath("E:"); IOXmlFileDto xmlList = (IOXmlFileDto) xmlDto.readXml(IOXmlFileDto.class); System.out.println(xmlList); } }
本文地址:https://blog.csdn.net/OudoWt/article/details/108035457
下一篇: 皋兰县