Java对象转化成xml文件:
需要导入(JDK内置):import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
转化代码:以下四个功能函数自己根据需要选择public class XMLUtil {
/**
* 将对象直接转换成String类型的 XML输出
*
* @param obj
* @return
*/
public static String convertToXml(Object obj) {
// 创建输出流
StringWriter sw = new StringWriter();
try {
// 利用jdk中自带的转换类实现
JAXBContext context = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = context.createMarshaller();
// 格式化xml输出的格式
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
Boolean.TRUE);
// 将对象转换成输出流形式的xml
marshaller.marshal(obj, sw);
} catch (JAXBException e) {
e.printStackTrace();
}
return sw.toString();
}
/**
* 将对象根据路径转换成xml文件
*
* @param obj
* @param path
* @return
*/
public static void convertToXml(Object obj, String path) {
try {
// 利用jdk中自带的转换类实现
JAXBContext context = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = context.createMarshaller();
// 格式化xml输出的格式
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
Boolean.TRUE);
// 将对象转换成输出流形式的xml
// 创建输出流
FileWriter fw = null;
try {
fw = new FileWriter(path);
} catch (IOException e) {
e.printStackTrace();
}
marshaller.marshal(obj, fw);
} catch (JAXBException e) {
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
/**
* 将String类型的xml转换成对象
*/
public static Object convertXmlStrToObject(Class clazz, String xmlStr) {
Object xmlObject = null;
try {
JAXBContext context = JAXBContext.newInstance(clazz);
// 进行将Xml转成对象的核心接口
Unmarshaller unmarshaller = context.createUnmarshaller();
StringReader sr = new StringReader(xmlStr);
xmlObject = unmarshaller.unmarshal(sr);
} catch (JAXBException e) {
e.printStackTrace();
}
return xmlObject;
}
@SuppressWarnings("unchecked")
/**
* 将file类型的xml转换成对象
*/
public static Object convertXmlFileToObject(Class clazz, String xmlPath) {
Object xmlObject = null;
try {
JAXBContext context = JAXBContext.newInstance(clazz);
Unmarshaller unmarshaller = context.createUnmarshaller();
FileReader fr = null;
try {
fr = new FileReader(xmlPath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
xmlObject = unmarshaller.unmarshal(fr);
} catch (JAXBException e) {
e.printStackTrace();
}
return xmlObject;
}
}
需要序列化的对象(一定需要无参构造函数及绑定xml标签与类字段):@XmlAccessorType(XmlAccessType.FIELD)
// XML文件中的根标识
@XmlRootElement(name = "Project")
// 控制JAXB 绑定类中属性和字段的排序
@XmlType(propOrder = {
"projectName",
"projectPath",
"projectNote",
"APIGraphPath",
"classFileList"
})
public class Project implements Serializable {
private static final long serialVersionUID = 1L;
// 项目名称
private String projectName;
// 项目地址
private String projectPath;
// 项目注释
private String projectNote;
// API使用图地址
private String APIGraphPath;
// 有效类文件
private List classFileList = new ArrayList<>();
public Project() {
super();
}
public Project(String projectName, String projectPath, String projectNote, String aPIGraphPath,
List classFileList) {
super();
this.projectName = projectName;
this.projectPath = projectPath;
this.projectNote = projectNote;
APIGraphPath = aPIGraphPath;
this.classFileList = classFileList;
}
public String getProjectName() {
return projectName;
}
public void setProjectName(String projectName) {
this.projectName = projectName;
}
public String getProjectPath() {
return projectPath;
}
public void setProjectPath(String projectPath) {
this.projectPath = projectPath;
}
public String getProjectNote() {
return projectNote;
}
public void setProjectNote(String projectNote) {
this.projectNote = projectNote;
}
public String getAPIGraphPath() {
return APIGraphPath;
}
public void setAPIGraphPath(String aPIGraphPath) {
APIGraphPath = aPIGraphPath;
}
public List getClassFileList() {
return classFileList;
}
public void setClassFileList(List classFileList) {
this.classFileList = classFileList;
}
@Override
public String toString() {
return "Project [projectName=" + projectName + ", projectPath=" + projectPath
+ "]";
}
}
Python工具:pip install untangleimport untangle
# 将文件解析成对象
obj = untangle.parse('xml2obj.xml')
# 获取
标签内容obj.root.title.__dict__['cdata']
# 获取第一个section标签
obj.root.body.section[0]
# 获取第二个section标签的id
obj.root.body.section[1]['id']