Below is an XML reading test I wrote myself
Copy the code code as follows:
import java.util.Iterator;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import org.dom4j.*;
import org.dom4j.io.SAXReader;
public class XmlRead {
static StringBuilder sBuilder = new StringBuilder();
public static void main(String[] args) throws IOException {
BufferedReader bReader = new BufferedReader(new InputStreamReader(
System.in));
String path = null;
System.out.println("Please enter the absolute path and file name of the XML file:/n");
path = bReader.readLine();
sBuilder.append("Start outputting XML file content/n");
Document document = null;
try {
document = read(path);
sBuilder.append(path + "/n");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
Element root = getRootElement(document);
if (root == null) {
System.out.print("The root node was not obtained");
return;
}
// Get the encoding format of the XML document
String encString = document.getXMLEncoding();
sBuilder.append("<?xml version=/"1.0/" encoding=/"" + encString
+ "/">/n");
sBuilder.append(elementText(root, attText(root), 0));
System.out.println(getIterator(root, 0) + "</" + root.getName() + ">");
}
/**
* Recursive node
*
* @description
* @param element
* @param lvl
* Level
* @return
*/
private static String getIterator(Element element, int lvl) {
lvl += 1;
for (Iterator i = element.elementIterator(); i.hasNext();) {
Element e = (Element) i.next();
sBuilder.append(elementText(e, attText(e), lvl));
getIterator(e, lvl);
int count = e.nodeCount();
if (count > 0) {
for (int j = 0; j < lvl; j++) {
sBuilder.append(" ");
}
}
sBuilder.append("</" + e.getName() + ">/n");
}
return sBuilder.toString();
}
/**
* Get the string value of the attribute of the current node
*
* @description
* @param element
* Current node
* @return
*/
private static String attText(Element element) {
String str = " ";
for (int i = 0; i < element.attributeCount(); i++) {
Attribute attribute = element.attribute(i);
str += attribute.getName() + "=/"" + attribute.getValue() + "/" ";
}
return str;
}
/**
* Get the text value of the current Element
*
* @description
* @param element
* Current Element node
* @param text
* attribute value
* @param lvl
* Level
* @return
*/
private static String elementText(Element element, String text, int lvl) {
String str = "";
for (int i = 0; i < lvl; i++) {
str += " ";
}
str += "<" + element.getName();
if (text != null && text != "") {
str += text;
}
//Since there is no hasChild attribute or method in dom4j, the nodeCount() method must be used to determine when there are child nodes.
int count = element.nodeCount();
if (count == 0) {
return str += ">";
}
return str += ">/n";
}
/**
*
* @description Read XML file
* @param file
* XML file path, including file name
* @return Document document
* @throws MalformedURLException
* @throws DocumentException
*/
public static Document read(String file) throws MalformedURLException,
DocumentException {
SAXReader reader = new SAXReader();
Document document = reader.read(new File(file));
return document;
}
/**
* Get the root node of the Document document
*
* @param document
* @return
*/
public static Element getRootElement(Document document) {
return document.getRootElement();
}
}