java代码用dom4j解析xml文件的简单操作

时间: 2016/02/17

目标:为telenor的ALU Femto接口写一个采集xml文件并解析出locationName标签里的值,然后更新到数据库中。

从网上搜了下,有四种常用的解析xml的方式,包括DOM,JAXD,Dom4J,SAX(simple api for xml).我正好有Dom4j的jar包,就选择了Dom4j来解析xml。

前半截对xml的解析测试成功了。后面的数据库的连接,把数据更新到数据库的操作没有测试。对应的xml文件的行数有点多,没法上传,另写一个xml的文档上传。

  1 package com.example.xml.dom4h;
  2 
  3 import java.io.File;
  4 import java.sql.Connection;
  5 import java.sql.DriverManager;
  6 import java.sql.Statement;
  7 import java.util.Iterator;
  8 import java.util.List;
  9 
 10 import javax.xml.parsers.DocumentBuilder;
 11 import javax.xml.parsers.DocumentBuilderFactory;
 12 
 13 import org.dom4j.Document;
 14 import org.dom4j.Element;
 15 import org.dom4j.io.DOMReader;
 16 import org.dom4j.io.SAXReader;
 17 
 18 /**
 19 * dom4j框架学习: 读取并解析xml
 20 * 
 21 * 
 22 */
 23 public class Dom4j_parse {
 24 public static void main(String[] args) throws Exception {
 25 SAXReader saxReader = new SAXReader();
 26 
 27 Document document = saxReader.read(new File("snapshot-Automatic-2016-02-09-15-53-00-1.xml"));
 28 String bsrName = "";
 29 String bsrId = "";
 30 String region = "";
 31 String city = "";
 32 String address ="";
 33 String classification = "";
 34 // 获取根元素
 35 Element root = document.getRootElement();
 36 System.out.println("Root: " + root.getName());
 37 
 38 // 获取所有子元素
 39 List<Element> childList = root.elements();
 40 System.out.println("total child count: " + childList.size());
 41 
 42 // 获取特定名称的子元素
 43 Element femtoCluster = root.element("FemtoCluster");
 44 List<Element> childList2 = femtoCluster.elements("Femto");
 45 
 46 System.out.println("locationname child: " + childList2.size());
 47 //对每一个femto的locationName解析
 48 for(int i=0; i < childList2.size(); i++){
 49 Element femtoElement = childList2.get(i);
 50 String femtoId= femtoElement.attributeValue("id");
 51 Element attributeElement = femtoElement.element("attributes");
 52 
 53 //step1 对bsc的名称和id先处理
 54 
 55 Element bsrNameElement = attributeElement.element("bSRName");
 56 Element bsridElement = attributeElement.element("bsrId");
 57 bsrName = bsrNameElement.getTextTrim();
 58 bsrId = bsridElement.getTextTrim();
 59 System.out.println( " bstname :"+bsrNameElement.getTextTrim()+ " ,bsrid :"+bsridElement.getTextTrim());
 60 //step2 对locatin的处理
 61 //用下划线把值分割成三部分,第一部分是region,第二部分是address,第三部分是city
 62 Element locationElement =    attributeElement.element("locationName");
 63 String allString = locationElement.getTextTrim();
 64 //如果有值,安装上面的三部分解析,如果没值,给region,city为空。
 65 if(!allString.isEmpty() && allString !=null && allString !="" ){
 66 //allString有值的时候
 67 String[] allStringArray = allString.split("_");
 68 region = allStringArray[0];
 69 city = allStringArray[2];
 70 address = allStringArray[1];
 71 if(city.isEmpty() ){
 72 classification = "RR";
 73 }else{
 74 classification = "U_SB";
 75 }
 76 System.out.println( " region :"+ region + " , city :"+city +" , address:"+ address);
 77 }else{
 78 //allString没有值得时候
 79 region = "";
 80 city = "";
 81 address = "";
 82 classification = "RR";
 83 System.out.println( " region :"+ region + " , city :"+city +" , address:"+ address);
 84 }//end if
 85 //对这些数据插入到数据库中
 86 /*
 87 * update pm4h_db.mo_moentity t set (region,city,address,classification) = (region,city,address,classification)
 88 * where t.moentityid = bsrId
 89 * 
 90 * */
 91 }// end one element
 92 // 获取名字为指定名称的第一个子元素
 93 //    Element firstWorldElement = root.element("world");
 94 //    // 输出其属性
 95 //    System.out.println("first World Attr: "
 96 //    + firstWorldElement.attribute(0).getName() + "="
 97 //    + firstWorldElement.attributeValue("name"));
 98 //
 99 //    System.out.println("迭代输出-----------------------");
100 //    // 迭代输出
101 //    for (Iterator iter = root.elementIterator(); iter.hasNext();) {
102 //    Element e = (Element) iter.next();
103 //    System.out.println(e.attributeValue("name"));
104 //
105 //    }
106 
107 // System.out.println("用DOMReader-----------------------");
108 //    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
109 //    DocumentBuilder db = dbf.newDocumentBuilder();
110 //    // 注意要用完整类名
111 //    org.w3c.dom.Document document2 = db.parse(new File("students.xml "));
112 //
113 //    DOMReader domReader = new DOMReader();
114 //
115 //    // 将JAXP的Document转换为dom4j的Document
116 //    Document document3 = domReader.read(document2);
117 //
118 //    Element rootElement = document3.getRootElement();
119 //
120 //    System.out.println("Root: " + rootElement.getName());
121 //连接数据库,并执行更新sql的方法
122 
123 
124 }
125 
126 public void updateFemtoData(String region1,String city1,String address1,String classification1,String bsrid1) {
127 try{
128 //0 拼sql语句
129 
130 //modify
131 String sql= " update pm4h_db.mo_moentity t set t.region =" + region1 
132 +" ,city = "+ city1
133 +" , classification= "+ classification1
134 +" ,address = "+ address1
135 + " where t.moentityid = " +bsrid1
136 ;
137 //1.加载驱动
138 Class.forName("oracle.jdbc.driver.OracleDriver");
139 //2 建立连接
140 //modify ip and 
141 Connection ct = DriverManager.getConnection("jdbc:oracle:thin:@10.218.6.165:1521:mos5200","oracle","oracle");
142 Statement st = ct.createStatement() ;
143 int rs = st.executeUpdate(sql);
144 }catch(Exception e){
145 e.printStackTrace();
146 }finally{
147 
148 if(true){
149 //    st.close();
150 //    ct.close();
151 }
152 }
原文地址:https://www.cnblogs.com/gaochsh/p/5197006.html