JAVA_DOM4J Parsing xml

Source: Internet
Author: User

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

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.