Article directory
The current versions are as follows:
This article uses version 1.6.1.
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
Of course, there are also direct object-to-XML dependencies, such as JAXB. The author here gives a Zhihu link to refer to the Java JAXB tutorial
Create an XML
Here we directly output a simple XML.
There are various attributes, which correspond to various elements inside XML. It is easier to use.
If you don't know some settings, you can refer to the XML document XML Tutorial | Novice Tutorial
@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();
}
The array is simply adding an identical node:
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();
}
Of course, element.add(Element t); can also be mounted like this
Among them, both Document and Element inherit from Branch: the relationship is as shown in the figure below,
mainly based on the types defined in 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;
In addition, the internal elements are stored in the form of list.
Because XM constraints usually have the following two types,
- XML DTD
- XML Schema
Limiters are stored in Dom4j, and variables such as EntityResolver and QName are related to it.
Dom4j also provides format adjustments, as follows for format beautification during output
// 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);
read an XML
There are usually two ways to read XML:
- DOM: load all XML into memory and build a tree before processing
- SAX:S quickly scans a large XML document, stops as soon as it finds query criteria, and then processes
Here we use XML according to the official sample
We simply pass in the XML generated above for simple traversal, and actually use breadth-first/depth-first traversal.
@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, and elements methods can pass in the name of the element to get the corresponding element
XPath navigation
XPath expressions can be evaluated on anything in the tree (such as , or ). For example greater than, less than, addition, subtraction, multiplication and division etc.
Reference - Zvon Tutorial
<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");
}
If you are looking for all hypertext links in an XHTML document, doing the following code does the trick.
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();
}
}