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 an absolute path to the XML file and file name: \ n");
Path = Breader.readline ();
Sbuilder.append ("Start output 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 ("No access to root node");
return;
}
//Get encoding format for XML documents
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 ();
}
/**
* Gets the string of values for the properties 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 = Element.attribute (i);
STR + + attribute.getname () + "=\" "+ attribute.getvalue () +" "";
}
return str;
}
/**
* Gets 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 this property or method in the dom4j, the Nodecount () method is used to determine the time and the 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 Documents
* @throws malformedurlexception
* @throws documentexception
*/
public static Document read (String file) throws Malformedurlexception,
documentexception {
Saxreader reader = new Saxreader ();
Document document = Reader.read (new file);
return document;
}
/**
* Get the root node of document documents
*
* @param document
* @return
*/
public static Element Getrootelement (document document) {
return Document.getrootelement ();
}
}