Package FORRQ;
Import Java.io.File;
Import java.net.MalformedURLException;
Import Java.util.Iterator;
Import java.util.List;
Import Org.dom4j.Attribute;
Import org.dom4j.Document;
Import org.dom4j.DocumentException;
Import org.dom4j.Element;
Import Org.dom4j.io.SAXReader;
public class Main {
Public Document Read (String fileName) throws Exception {
Saxreader reader = new Saxreader ();
Document document = Reader.read (new File);
return document;
}
public static void Main (String args[]) throws Exception {
String str = "Rqxml/table.xml";
Main main = new main ();
Document doc = Main.read (str);
Element root = doc.getrootelement ();
list<?> tables = root.elements ("table");//depending on the root node, place the root node under
for (Object table:tables) {//
Element etable = ( Element) Table;
System.out.println (Etable.getname () + "-----");//Gets the data under the node.
list<?> fields = etable.elements ("field");//Get all the contents under the node, deposit in the container
list<?> attrs = Etable.attributes ();
for (Object attr:attrs) {
Attribute cattr = (Attribute) attr;
System.out.println (Cattr.getname () + ":" + Cattr.gettext ());//Gets the data under the node.
}
for (Object field:fields) {
Element Efield = (element) field;
list<?> fattrs = Efield.attributes ();
System.out.println (Efield.getname () + ": >>>>>");//Gets the data under the node.
for (Object attr:fattrs) {
Attribute cattr = (Attribute) attr;
System.out.println (Cattr.getname () + ":" + Cattr.gettext ());//Gets the data under the node.
}
}
}
}
}
JAVA_DOM4J parsing XML