Java dom4j reads XML instance code _java

Source: Internet
Author: User

Here's an XML read Test I wrote myself

Copy 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 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 &lt; lvl; i++) {


STR + + "";


}


STR + + "&lt;" + Element.getname ();


if (text!= null &amp;&amp; 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 + + "&gt;";


}


Return str + + "&gt;\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 ();
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.