XML file containing DB connection information
1 <!--example three-level display -2 <db-connections>3 <Connection>4 <name>DBTest</name>5 <Jndi></Jndi>6 <URL>7 <! [Cdata[Jdbc:mysql://localhost:3306/db_test?useunicode=true&characterencoding=utf8]]>8 </URL>9 <Driver>Org.gjt.mm.mysql.Driver</Driver>Ten <User>Test</User> One <Password>test2012</Password> A <max-active>10</max-active> - <Max-idle>10</Max-idle> - <Min-idle>2</Min-idle> the <max-wait>10</max-wait> - <Validation-query>SELECT</Validation-query> - </Connection> - </db-connections>
Db-connections.xml
Node Properties XML file
1 <!--example 2--node Properties -2 <Bookstore>3 < Bookcategory= "Cooking">4 <titleLang= "en">Everyday Italian</title>5 <author>Giada De Laurentiis</author>6 < Year>2005</ Year>7 < Price>30.00</ Price>8 </ Book>9 < Bookcategory= "Children"title= "Harry Potter"author= "J K. Rowling" Year= "2005" Price= "$29.9"/>Ten </Bookstore>
Bookstore.xml
Parsing code:
1 PackageXML;2 3 ImportJava.io.File;4 ImportJava.io.FileInputStream;5 Importjava.util.ArrayList;6 ImportJava.util.HashMap;7 ImportJava.util.Iterator;8 Importjava.util.List;9 ImportJava.util.Map;Ten One ImportOrg.dom4j.Attribute; A Importorg.dom4j.Document; - Importorg.dom4j.Element; - ImportOrg.dom4j.io.SAXReader; the - Public classTestdom4j2 { - - /** + * Parse XML file containing DB connection information - * format must conform to the following specifications: + * 1. Up to three levels, the node name of each level is customized; A * 2. Level two node supports node attributes, and attributes are considered as child nodes; at * 3. CDATA must be included in the node and cannot appear separately. - */ - Public StaticList<map<string, string>> parsedbxml (String configfile)throwsException { -list<map<string, string>> dbconnections =NewArraylist<map<string, string>>(); - //from the Java Project path to query the file, the file must be in the SRC source package path to use this method - //InputStream is = TestDom4j2.class.getResourceAsStream (configfile); in - //get the contents of an XML file from a drive letter toFileInputStream is =NewFileInputStream (NewFile (configfile)); +Saxreader Saxreader =NewSaxreader (); -Document document =Saxreader.read (IS); the //get root node db-connections or bookstore *Element connections =document.getrootelement (); $ Panax NotoginsengIterator<element> Rootiter =connections.elementiterator (); - while(Rootiter.hasnext ()) { theElement connection =Rootiter.next (); +Iterator<element> Childiter =connection.elementiterator (); A themap<string, string> ConnectionInfo =NewHashmap<string, string>(); + //get all the properties of a connection or book node -list<attribute> attributes =connection.attributes (); $ //Traverse Node Properties $ for(inti = 0; I < attributes.size (); ++i) { - //To Add a node property value - Connectioninfo.put (Attributes.get (i). GetName (), Attributes.get (i). GetValue ()); the } - //traversing a child node of a connection or bookWuyi while(Childiter.hasnext ()) { the //Add child nodes -Element attr =Childiter.next (); Wu Connectioninfo.put (Attr.getname (). Trim (), Attr.gettext (). Trim ()); - } About Dbconnections.add (connectioninfo); $ } - returndbconnections; - } - A Public Static voidMain (string[] args) { + Try { theList<map<string, string>> dblist= parsedbxml ("E://xml//db-connections.xml"); -System.out.println ("url=" + dblist.get (0). Get ("URL")); $ the System.out.println (); the theList<map<string, string>> list= parsedbxml ("E://xml//bookstore.xml"); the for(intI=0;i<list.size (); i++){ -System.out.println ("title=" + list.get (i). Get ("title")); inSystem.out.println ("author=" + list.get (i). Get ("Author")); theSystem.out.println ("year=" + list.get (i). Get ("Year")); theSystem.out.println ("price=" + list.get (i). Get ("price"))); About } the}Catch(Exception e) { the e.printstacktrace (); the } + } -}
Operation Result:
Url=jdbc:mysql://localhost:3306/db_test?useunicode=true&characterencoding=UTF8title=Everyday Italianauthor=giada De laurentiisyear=2005price=30.00title=harry potterauthor=j K. rowlingyear=2005price=$29.9
Use DOM4J to parse an XML file containing DB connection information and to obtain a node attribute