手头正好需要一个xml转bean的工具和xml解析工具,网上实现很多,自己造一次轮子,一整套流程直接复制可用,一分钟实现转换加解析(xml转换使用idea实现,eclipse同样有工具,一搜一大把这里就不赘述了)。本文可转载,标注来源即可。
1.xml转xsd
1.1样例XML(persons.xml)
<?xml version="1.0" encoding="UTF-8" ?>
<Persons>
<person id="1" races="yellow">
<name type=""></name>
<age></age>
<sex></sex>
<height></height>
<location>
<home>china</home>
<company></company>
</location>
</person>
<person id="2" races="red">
<name type=""></name>
<age></age>
<sex></sex>
<height></height>
<location>
<home>earth</home>
<company></company>
</location>
</person>
<feature total="">
<action id=""></action>
<source type="org">investigation</source>
</feature>
</Persons>
1.2 利用idea将persons.xml生成persons.xsd
1.2.1首先打开persons.xml文件,右击点击Generate XSD Schema from XML File…
1.2.2 生成persons.xsd设置
需要注意Design type选项要选择local elements/types,确保在xsd转换bean时保证属性一致。
1.2.3生成persons.xsd文件
生成的persons.xsd文件预览
2.xsd转JavaBean
2.1 选中person.xsd idea导航栏 [TOOL]>[JAXB]>Generate Java Code From XML Schema Using JAXB…
2.2 person.xsd转bean设置
属性Package Prefix设置你需要的bean对象类的存放包路径,其他属性不需要调整。
2.3 生成bean
设置完成后会生成如下结构的java类,此时persons.xml转换bean已经完成
2.4 备注
如过生成后发现有参数需要修改,那么我们只需修改persons.xml文件,将过程重新走一遍,相关的persons.xsd以及生成的bean类会自动覆盖更新。
3.xml解析实现代码
直接贴代码,直接复制可用。
3.1 XmlParser工具类实现
package util.xml;
import util.bean.Person;
import util.bean.Persons;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import java.io.*;
import java.util.List;
/** * xml工具类 * * @author sssuperMario * @date 2020/08/28 */
public class XmlParser {
public static void main(String[] args) {
Persons persons = xmlToBean(new File("/IdeaProjects/Persons.xml"), Persons.class);
System.out.println("person feature[source]:" + persons.getFeature().getSource().getValue());
List<Person> personList = persons.getPerson();
for (Person person : personList) {
System.out.println("person id:" + person.getId() + "\trace:"
+ person.getRaces() + "\thome:"
+ person.getLocation().getHome());
}
}
/** * xml to bean * @param xmlFile * @param beanClass * @param <T> * @return */
public static <T> T xmlToBean(File xmlFile, Class<T> beanClass) {
if (xmlFile.length() == 0) {
throw new RuntimeException("xml file content length is blank.");
}
BufferedReader bufferedReader = null;
FileReader fileReader = null;
StringBuffer content = new StringBuffer();
try {
fileReader = new FileReader(xmlFile);
bufferedReader = new BufferedReader(fileReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
content.append(line);
}
} catch (Exception e) {
throw new RuntimeException("xml file read error.", e);
} finally {
try {
fileReader.close();
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return xmlToBean(content.toString(), beanClass);
}
/** * xml to bean * @param xmlContent * @param beanClass * @param <T> * @return */
public static <T> T xmlToBean(String xmlContent, Class<?> beanClass) {
Reader reader = null;
try {
JAXBContext context = JAXBContext.newInstance(beanClass);
Unmarshaller unmarshaller = context.createUnmarshaller();
reader = new StringReader(xmlContent);
return (T) unmarshaller.unmarshal(reader);
} catch (Exception e) {
throw new RuntimeException("xml parse error.", e);
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}