网站品牌词如何优化,企业数字化平台,遵义网站开发公司,潍坊seo按天收费说明#xff1a;当我们在工作中需要将数据转为XML文件、或者读取解析XML文件时#xff0c;使用Hutool工具包中的XMLUtil相关方法是最容易上手的方法#xff0c;本文介绍如何使用Hutool工具包来解析、生成XML文件。
开始之前#xff0c;需要导入Hutool工具包的依赖
de…说明当我们在工作中需要将数据转为XML文件、或者读取解析XML文件时使用Hutool工具包中的XMLUtil相关方法是最容易上手的方法本文介绍如何使用Hutool工具包来解析、生成XML文件。
开始之前需要导入Hutool工具包的依赖
dependencygroupIdcn.hutool/groupIdartifactIdhutool-all/artifactIdversion5.8.6/version
/dependencyXML 转 JavaBean
如下面这个XML文件
?xml version1.0 encodingutf-8?
databooksbooktitle《许三观卖血记》/titleauthor余华/authorpublish作家出版社/publish/bookbooktitle《罪与罚》/titleauthor陀思妥耶夫斯基/authorpublish浙江文艺出版社/publish/bookbooktitle《白夜》/titleauthor陀思妥耶夫斯基/authorpublish上海译文出版社/publish/book/books
/data经分析从外至里我们需要创建以下对象
data标签对象里面包含了books标签对象
import cn.hutool.core.annotation.Alias;
import lombok.Data;import java.io.Serializable;Data
public class DataBean implements Serializable {/*** 数据包*/Alias(books)private BooksBean books;
}books标签对象里面有book对象并且是一个集合
import cn.hutool.core.annotation.Alias;
import lombok.Data;import java.io.Serializable;
import java.util.List;Data
public class BooksBean implements Serializable {/*** 书籍列表*/Alias(book)private ListBookBean books;
}book对象里面是多个属性
import cn.hutool.core.annotation.Alias;
import lombok.Data;import java.io.Serializable;Data
public class BookBean implements Serializable {/*** 书名*/Alias(title)private String title;/*** 作者*/Alias(author)private String author;/*** 出版社*/Alias(publish)private String publish;
}需要注意对应属性上面的注解以及注解里面的名称需要与xml中的标签名对应
解析XML代码 // 读取xml文件String xml FileUtil.readString(new File(data.xml), UTF-8);// 将xml转换为java对象Element rootElement XmlUtil.getRootElement(XmlUtil.parseXml(xml));// 使用根节点下的直接 转成对象DataBean dataBean XmlUtil.xmlToBean(rootElement, DataBean.class);// 输出对象数据System.out.println(dataBean.getBooks().getBooks());解析完成 JavaBean 转 XML
对象转XML是相反的过程先组装数据成一个对象。然后依次创建节点标签将数据放入到标签中。 // 制造数据BookBean bookBean new BookBean();bookBean.setTitle(《活着》);bookBean.setAuthor(余华);bookBean.setPublish(作家出版社);// 创建Books对象ListBookBean beans new ArrayList();beans.add(bookBean);// 创建xml对象Document document XmlUtil.createXml();// 创建data节点Element data document.createElement(data);document.appendChild(data);// 创建books节点Element books document.createElement(books);data.appendChild(books);// 创建book节点Element book document.createElement(book);books.appendChild(book);// 填充数据for (BookBean bean : beans) {Element title document.createElement(title);title.setTextContent(bean.getTitle());book.appendChild(title);Element author document.createElement(author);author.setTextContent(bean.getAuthor());book.appendChild(author);Element publish document.createElement(publish);publish.setTextContent(bean.getPublish());book.appendChild(publish);}// 将document对象转换为xml字符串String xmlStr XmlUtil.toStr(document);// 输出System.out.println(xmlStr);转换成功 另外这里的xml是未格式化的可以通过下面这个方法输出格式化后的xml private String formatXml(Document document) {try {TransformerFactory tf TransformerFactory.newInstance();Transformer transformer tf.newTransformer();transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, no);transformer.setOutputProperty(OutputKeys.INDENT, yes);transformer.setOutputProperty({http://xml.apache.org/xslt}indent-amount, 4);// 将 Document 转换为 SourceDOMSource source new DOMSource(document);StreamResult result new StreamResult(new StringWriter());// 使用 Transformer 对象进行格式化transformer.transform(source, result);return result.getWriter().toString();} catch (Exception e) {e.printStackTrace();return null;}}将上面的代码换成如下 // 将document对象转换为xml字符串并格式化String xmlStr formatXml(document);运行看效果 总结
使用Hutool工具包解析、生成XML文件使用的是XmlUtil中的XmlUtil.xmlToBean()、XmlUtil.toStr()等方法另外还有其他方法可供使用可自行研究。