Java code simple manipulation of parsing XML files with dom4j

Source: Internet
Author: User

Date: 2016/02/17

Target: Write a capture XML file for the Telenor Alu Femto interface and parse the value from the locationname tag and update to the database.

Searching the web, there are four common ways to parse XML, including Dom,jaxd,dom4j,sax (Simple API for XML). I just had the dom4j jar package, and I chose dom4j to parse the XML.

The first half of the parsing test for XML was successful. After the connection to the database, the operation to update the data to the database is not tested. The corresponding XML file has a bit more lines, cannot be uploaded, and another XML document upload.

1  Packagecom.example.xml.dom4h;2 3 ImportJava.io.File;4 Importjava.sql.Connection;5 ImportJava.sql.DriverManager;6 Importjava.sql.Statement;7 ImportJava.util.Iterator;8 Importjava.util.List;9 Ten ImportJavax.xml.parsers.DocumentBuilder; One Importjavax.xml.parsers.DocumentBuilderFactory; A  - Importorg.dom4j.Document; - Importorg.dom4j.Element; the ImportOrg.dom4j.io.DOMReader; - ImportOrg.dom4j.io.SAXReader; -  - /** + * DOM4J Framework Learning: Reading and parsing XML - *  + *  A */ at  Public classDom4j_parse { -  Public Static voidMain (string[] args)throwsException { -Saxreader Saxreader =NewSaxreader (); -  -Document document = Saxreader.read (NewFile ("Snapshot-automatic-2016-02-09-15-53-00-1.xml")); -String bsrname = ""; inString Bsrid = ""; -String region = ""; toString City = ""; +String address = ""; -String classification = ""; the //Get root element *Element root =document.getrootelement (); $System.out.println ("Root:" +root.getname ());Panax Notoginseng  - //get all child elements theList<element> childlist =root.elements (); +SYSTEM.OUT.PRINTLN ("Total child count:" +childlist.size ()); A  the //get child elements of a specific name +Element Femtocluster = root.element ("Femtocluster"); -list<element> childList2 = femtocluster.elements ("Femto"); $  $System.out.println ("LocationName Child:" +childlist2.size ()); - //The locationname analysis of each femto -  for(inti=0; I < childlist2.size (); i++){ theElement femtoelement =Childlist2.get (i); -String femtoid= femtoelement.attributevalue ("id");WuyiElement attributeelement = femtoelement.element ("Attributes"); the  - //STEP1 the name and ID of the BSC is processed first Wu  -Element bsrnameelement = attributeelement.element ("Bsrname"); AboutElement bsridelement = attributeelement.element ("Bsrid"); $Bsrname =Bsrnameelement.gettexttrim (); -Bsrid =Bsridelement.gettexttrim (); -System.out.println ("Bstname:" +bsrnameelement.gettexttrim () + ", Bsrid:" +Bsridelement.gettexttrim ()); - //the treatment of Locatin by Step2 A //use underline to divide the value into three parts, the first part is region, the second part is address, the third part is city +Element locationelement = attributeelement.element ("LocationName"); theString allstring =Locationelement.gettexttrim (); - //If there is a value, install the above three-part parsing, if no value, give region,city empty.  $ if(!allstring.isempty () && allstring! =NULL&& allstring! = "" ){ the //when allstring has a value, thestring[] Allstringarray = Allstring.split ("_"); theRegion = Allstringarray[0]; theCity = allstringarray[2]; -Address = Allstringarray[1]; in if(City.isempty ()) { theClassification = "RR"; the}Else{ AboutClassification = "U_SB"; the } theSystem.out.println (' Region: ' + region + ', City: ' +city + ', Address: ' +address); the}Else{ + //allstring not worth the time -Region = ""; theCity = "";BayiAddress = ""; theClassification = "RR"; theSystem.out.println (' Region: ' + region + ', City: ' +city + ', Address: ' +address); -}//End If - //insert this data into the database the /* the * Update pm4h_db.mo_moentity t set (region,city,address,classification) = (region,city,address,classification)  the * WHERE T.moentityid = Bsrid the *  - * */ the}//End One element the //gets the first child element whose name is the specified name the //Element firstworldelement = root.element ("World");94 //    //output its properties the //System.out.println ("First World Attr:" the //+ firstworldelement.attribute (0). GetName () + "=" the //+ firstworldelement.attributevalue ("name"));98 // About //System.out.println ("Iterative output-----------------------"); - //    //Iterative Output101 //For (Iterator iter = Root.elementiterator (); Iter.hasnext ();) {102 //element e = (Element) iter.next ();103 //System.out.println (E.attributevalue ("name"));104 // the //    }106 107 //System.out.println ("with Domreader-----------------------");108 //documentbuilderfactory dbf = Documentbuilderfactory.newinstance ();109 //Documentbuilder db = Dbf.newdocumentbuilder (); the //    //Note To use the full class name111 //org.w3c.dom.Document Document2 = db.parse (New File ("Students.xml")); the //113 //Domreader domreader = new Domreader (); the // the //    //convert JAXP document to dom4j document the //Document document3 = Domreader.read (Document2);117 //118 //Element rootelement = Document3.getrootelement ();119 // - //System.out.println ("Root:" + rootelement.getname ());121 //connect to the database and Execute methods to update SQL122 123 124 } the 126  Public voidupdatefemtodata (String region1,string city1,string address1,string classification1,string bsrid1) {127 Try{ - //0 Spelling SQL statements129  the //Modify131String sql= "Update pm4h_db.mo_moentity t set t.region =" +Region1 the+ ", City =" +city1133+ ", classification=" +Classification1134+ ", address =" +Address1135+ "Where T.moentityid =" +Bsrid1136 ;137 //1. Load Driver138Class.forName ("Oracle.jdbc.driver.OracleDriver");139 //2 Establishing the connection $ //Modify IP and141Connection ct = drivermanager.getconnection ("jdbc:oracle:thin:@10.218.6.165:1521:mos5200", "Oracle", "Oracle");142Statement st =ct.createstatement ();143 intrs =st.executeupdate (SQL);144}Catch(Exception e) {145 e.printstacktrace ();146}finally{147 148 if(true){149 //st.close (); Max //ct.close ();151 } the}

Java code simple manipulation of parsing XML files with dom4j

Related Article

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.