目前版本如下:
<dependency>
<groupId>dom4jgroupId>
<artifactId>dom4jartifactId>
<version>1.6.1version>
dependency>
当然也有直接对象转XML的依赖,比如 JAXB 。笔者这里给出一个知乎链接可以参考Java JAXB 教程
我们这里就直接输出一个简单的XML。
其中有各类属性,对应了XML内部各类元素。使用起来比较容易。
如果不了解一些设置,可以查阅XML的文档 XML 教程|菜鸟教程
@RequestMapping
public void test(HttpServletResponse httpServletResponse) throws IOException {
httpServletResponse.setStatus(HttpServletResponse.SC_OK);
httpServletResponse.setHeader("Content-Disposition", "attachment;filename=" + LocalDate.now() + ".xml");
httpServletResponse.setCharacterEncoding("UTF-8");
httpServletResponse.setContentType("text/html;charset=UTF-8");
Writer writer = httpServletResponse.getWriter();
Document document = DocumentHelper.createDocument();
Element element = document.addElement("节点1");
element.addAttribute("id", "node-1");
element.addText("这里是节点1的内容");
element.addElement("节点1的子节点").addAttribute("id", "node-2").addText("节点1的子节点的内容");
document.write(writer);
writer.flush();
writer.close();
}
public void test(HttpServletResponse httpServletResponse) throws IOException {
httpServletResponse.setStatus(HttpServletResponse.SC_OK);
httpServletResponse.setHeader("Content-Disposition", "attachment;filename=" + LocalDate.now() + ".xml");
httpServletResponse.setCharacterEncoding("UTF-8");
httpServletResponse.setContentType("text/html;charset=UTF-8");
Writer writer = httpServletResponse.getWriter();
Document document = DocumentHelper.createDocument();
Element element = document.addElement("节点1","0");
element.addText("这里是节点1的内容");
element.addElement("节点1的子节点").addText("节点1的子节点的内容");
element.addElement("节点1的子节点").addText("节点1的子节点的内容");
document.write(writer);
writer.flush();
writer.close();
}
当然也可以element.add(Element t);这样挂载
其中Document与Element均继承Branch:关系如下图
主要依据NODE中定义的类型分辨
short ANY_NODE = 0;
short ELEMENT_NODE = 1;
short ATTRIBUTE_NODE = 2;
short TEXT_NODE = 3;
short CDATA_SECTION_NODE = 4;
short ENTITY_REFERENCE_NODE = 5;
short PROCESSING_INSTRUCTION_NODE = 7;
short COMMENT_NODE = 8;
short DOCUMENT_NODE = 9;
short DOCUMENT_TYPE_NODE = 10;
short NAMESPACE_NODE = 13;
short UNKNOWN_NODE = 14;
short MAX_NODE_TYPE = 14;
另外内部elements以list的方式存储。
因为XM了约束通常有如下两种,
Dom4j中就存储着限制器,EntityResolver,QName等变量就与之相关。
Dom4j也提供了格式调整,如下为输出时的格式美化
// Pretty print the document to System.out
OutputFormat format = OutputFormat.createPrettyPrint();
writer = new XMLWriter(System.out, format);
// Compact format to System.out
format = OutputFormat.createCompactFormat();
writer = new XMLWriter(System.out, format);
读取XML方式通常有两种:
我们这里根据官方样例,使用XML
我们就传入上面生成的XML进行简单的遍历,实际使用可以广度优先/深度优先的方式进行遍历。
@RequestMapping("/import")
public void importXML(MultipartFile multipartFile) throws IOException, DocumentException {
SAXReader reader = new SAXReader();
Document document = reader.read(multipartFile.getInputStream());
Iterator<Element> iterator = document.getRootElement().elementIterator();
while (iterator.hasNext()) {
Element element = (Element) iterator.next();
Iterator<Element> iterator2 = element.elements().iterator();
log.info("{}",element.getName());
log.info("{}", element.getData());
while (iterator2.hasNext()){
Element element2 = (Element) iterator2.next();
log.info("{}",element2.getData());
}
}
}
getRootElement,elementIterator,elements方法中都可以传入element的name来获取对应的元素
可以在树中的任何(如 、 或 )上计算 XPath 表达式。例如大于,小于,加减乘除等。
参考-Zvon 教程
<foo>
<bar>
<author>author>
bar>
foo>
public void bar(Document document) {
List<Node> list = document.selectNodes("//foo/bar");
Node node = document.selectSingleNode("//foo/bar/author");
String name = node.valueOf("@name");
}
如果在XHTML文档中查找所有超文本链接,请执行以下操作 代码可以解决问题。
public void findLinks(Document document) throws DocumentException {
List<Node> list = document.selectNodes("//a/@href");
for (Iterator<Node> iter = list.iterator(); iter.hasNext();) {
Attribute attribute = (Attribute) iter.next();
String url = attribute.getValue();
}
}
dom4j官网