首先我给出一个xml嵌套文件:
很多时候获取到的的xml文件是混乱无序的,需要自己花时间去区分整理,下方是我区分好的
(这里有一个小工具推荐给大家:在线 XML 格式化 | 菜鸟工具 ())
-
<root>
-
<country>
-
<name>中国</name>
-
<province>
-
<name>广东省</name>
-
<city>
-
<name>广州市</name>
-
<district>
-
<name>天河区</name>
-
<population>1357242</population>
-
</district>
-
<district>
-
<name>越秀区</name>
-
<population>1008543</population>
-
</district>
-
</city>
-
-
<city>
-
<name>深圳市</name>
-
<district>
-
<name>福田区</name>
-
<population>1035837</population>
-
</district>
-
<district>
-
<name>南山区</name>
-
<population>1043563</population>
-
</district>
-
</city>
-
</province>
-
</country>
-
</root>
打开idea,创建一个springboot项目,在pom文件中导入依赖:
-
<dependency>
-
<groupId></groupId>
-
<artifactId>jaxb-api</artifactId>
-
</dependency>
-
<dependency>
-
<groupId></groupId>
-
<artifactId>jaxb-core</artifactId>
-
<version>2.3.0</version>
-
</dependency>
-
<dependency>
-
<groupId></groupId>
-
<artifactId>jaxb-impl</artifactId>
-
<version>2.3.0</version>
-
</dependency>
-
<dependency>
-
<groupId></groupId>
-
<artifactId>jaxb-runtime</artifactId>
-
<version>2.3.0</version>
-
</dependency>
-
<dependency>
-
<groupId></groupId>
-
<artifactId>activation</artifactId>
-
<version>1.1.1</version>
-
</dependency>
创建一个Java类,根据整理好的xml文件写第一层嵌套:
按照我提供的xml里面的内容,第一层是country,它下边的子元素分别为name和province
先了解以下重要注解:(按照我的格式写就好了,格式不用改)
@XmlRootElement:这是在JAXB中使用的Object必须有的注解,它定义了根元素
@XmlType:它将类映射到XML模式类型,我们可以用它来排列XML中的元素
@XmlTransient:这将确保Object属性不被写入XML中
@XmlAttribute: 这将创建Object属性作为一个属性
@XmlElement(name = “ABC”): 这将创建名称为 "ABC "的元素
@XmlElementWrapper: 注解表示生成一个包装器元素(一般用于集合元素)
-
import ;
-
import ;
-
import ;
-
import ;
-
-
import .annotation.*;
-
import ;
-
-
@XmlAccessorType()
-
@XmlRootElement(name = "country")//根元素名
-
@XmlType(propOrder = {"name","province"})//子元素的排序,多个用逗号隔开
-
@Data
-
@AllArgsConstructor
-
@NoArgsConstructor
-
@Builder
-
/**
-
* @Author:changsail
-
* @Data:2023/10/18
-
*/
-
public class country {
-
@XmlElement(name ="name")
-
private String name ;
-
@XmlElement(name ="province")
-
private province province ;
-
}
(由于这一层嵌套里面name没有下级嵌套,只有字符,所以用String修饰,而province有下级嵌套,所以用Province修饰)
注意:由于province下级有多个city并列,所以需要用到List列表,如下:
-
import ;
-
import ;
-
import ;
-
import ;
-
-
import .annotation.*;
-
import ;
-
-
@XmlAccessorType()
-
@XmlRootElement(name = "province")//根元素名
-
@XmlType(propOrder = {"name","city"})//子元素的排序,多个用逗号隔开
-
@Data
-
@AllArgsConstructor
-
@NoArgsConstructor
-
@Builder
-
/**
-
* @Author:changsail
-
* @Data:2023/10/18
-
*/
-
public class province {
-
-
@XmlElement(name ="name")
-
private String name;
-
@XmlElement(name ="city",type=)
-
private List<city> city;
-
}
(这里city用List<city>修饰保证所有city都可被转,不会发生遗漏)
最后我们封装一个工具类,用来实现xml到Java的转换:
网上有很多封装好的,找到直接cv就好了,也可以复制我的......
-
import ;
-
import ;
-
import ;
-
import ;
-
import ;
-
import ;
-
-
/**
-
* 转换Xml工具类
-
* @Author: changsail
-
* @Date: 2023/10/18
-
*/
-
public class ConvertXmlUtil {
-
-
-
/**
-
* 字符串xml转换成Java对象
-
* @param mapObject xml映射实体
-
* @param xmlStr xml字符串
-
* @return: java对象
-
*/
-
@SuppressWarnings("unchecked")
-
public static Object convertXmlStrToObject(Class mapObject, String xmlStr) {
-
-
Object javaObject = null;
-
-
try {
-
JAXBContext context = (mapObject);
-
Unmarshaller unmarshaller = ();
-
StringReader sr = new StringReader(xmlStr);
-
javaObject = (sr);
-
} catch (JAXBException e) {
-
();
-
}
-
-
return javaObject;
-
-
}
-
-
-
/**
-
* 将file类型的xml转换成Java对象
-
* @param mapObject xml映射实体
-
* @param xmlPath xml文件路径
-
* @return java对象
-
*/
-
@SuppressWarnings("unchecked")
-
public static Object convertXmlFileToObject(Class mapObject, String xmlPath) {
-
-
Object javaObject = null;
-
-
try {
-
JAXBContext context = (mapObject);
-
Unmarshaller unmarshaller = ();
-
FileReader fr = null;
-
try {
-
fr = new FileReader(xmlPath);
-
} catch (FileNotFoundException e) {
-
();
-
}
-
javaObject = (fr);
-
} catch (JAXBException e) {
-
();
-
}
-
-
return javaObject;
-
-
}
-
-
}
完成以上所有步骤,我们就可以创建Test测试类来调方法进行转换了!
-
import ;
-
-
import .*;
-
-
public class Test {
-
-
public static void main(String[] args) throws IOException {
-
-
String fileName = "C:\\Users\\Administrator\\Desktop\\示例"; //xml文件地址
-
String encoding = "utf-8"; //保证转换后的结果不会乱码
-
-
File file = new File(fileName);
-
Long fileLength = file.length();
-
byte[] fileContent = new byte[()];
-
FileInputStream in = null;
-
-
String newFile = null;
-
try {
-
in = new FileInputStream(file);
-
in.read(fileContent);
-
newFile = new String(fileContent, encoding);
-
} catch (IOException e) {
-
();
-
}
-
-
int begin = ("<country>");
-
-
int end = ("</country>");
-
-
String contentFile = (begin, end + "</country>".length());
-
-
-
contentFile =("[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]", "");
-
contentFile =("<!\\[CDATA\\[", "");
-
contentFile =("]]", "");
-
contentFile =(">>", "");
-
-
root= () (.class,contentFile);
-
// HIPMessageServerResult hipMessageServerResult= (HIPMessageServerResult) (contentFile,HIPMessageServerResult.class);
-
-
((root));
-
-
-
-
}
-
-
public static String xmlToJava(){
-
-
String fileName = "C:\\Users\\Administrator\\Desktop\\示例";
-
String encoding = "utf-8";
-
-
File file = new File(fileName);
-
Long fileLength = file.length();
-
byte[] fileContent = new byte[()];
-
FileInputStream in = null;
-
-
String newFile = null;
-
try {
-
in = new FileInputStream(file);
-
in.read(fileContent);
-
newFile = new String(fileContent, encoding);
-
} catch (IOException e) {
-
();
-
}
-
-
int begin = ("<country>");
-
-
int end = ("</country>");
-
-
String contentFile = (begin, end + "</country>".length());
-
-
-
contentFile =("[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]", "");
-
contentFile =("<!\\[CDATA\\[", "");
-
contentFile =("]]", "");
-
contentFile =(">>", "");
-
-
root= () (country.class,contentFile);
-
// HIPMessageServerResult hipMessageServerResult= (HIPMessageServerResult) (contentFile,HIPMessageServerResult.class);
-
-
return ((root));
-
}
-
}
运行Test,返回结果:
然后大功告成!