Spring quartz定时结合Jsoup和Dom4j使用解析百度地图API

 

<?xml version="1.0" encoding="utf-8" ?>
<GeocoderSearchResponse>
	<status>0</status>
	<result>
		<location>
			<lat>39.983424071404</lat>
			<lng>116.322987</lng>
		</location>
		<formatted_address>北京市海淀区中关村大街27号1101-08室</formatted_address>
		<business>人民大学,中关村,苏州街</business>
		<addressComponent>
			<streetNumber></streetNumber>
			<street>中关村大街</street>
			<district>海淀区</district>
			<city>北京市</city>
			<province>北京市</province>
		</addressComponent>
		<cityCode>131</cityCode>
		<pois>
			<poi>
				<addr>中关村西区南侧(中关村科技园区内)</addr>
				<distance>0.050000</distance>
				<name>中关村大厦</name>
				<poiType>办公大厦,商务大厦</poiType>
				<tel>(010)82856666</tel>
				<zip>100000</zip>
				<point>
					<x>116.32298658484</x>
					<y>39.983423843929</y>
				</point>
			</poi>
			<poi>
				<addr>中关村大街27号</addr>
				<distance>0.050000</distance>
				<name>眉州东坡酒楼中关村店</name>
				<poiType>中餐馆,餐饮</poiType>
				<tel>(010)82856948</tel>
				<zip></zip>
				<point>
					<x>116.32298658484</x>
					<y>39.983423843929</y>
				</point>
			</poi>
			<poi>
				<addr>中关村大街27号</addr>
				<distance>0.050000</distance>
				<name>中国人民财产保险中关村营业部</name>
				<poiType>中国人民财产保险,保险公司,金融</poiType>
				<tel>(010)82856779</tel>
				<zip>100000</zip>
				<point>
					<x>116.32298658484</x>
					<y>39.983423843929</y>
				</point>
			</poi>
			<poi>
				<addr>北京市海淀区</addr>
				<distance>94.432081</distance>
				<name>光合作用书房</name>
				<poiType>图书音像,购物</poiType>
				<tel></tel>
				<zip></zip>
				<point>
					<x>116.32239334388</x>
					<y>39.983890240676</y>
				</point>
			</poi>
			<poi>
				<addr>中关村大街27号</addr>
				<distance>42.195731</distance>
				<name>建行中关村支行</name>
				<poiType>中国建设银行,银行,金融</poiType>
				<tel></tel>
				<zip>100000</zip>
				<point>
					<x>116.32292037972</x>
					<y>39.983711118168</y>
				</point>
			</poi>
			<poi>
				<addr>北京市海淀区</addr>
				<distance>62.342644</distance>
				<name>海淀医院-激光整形美容部</name>
				<poiType>美容美发,生活服务</poiType>
				<tel></tel>
				<zip></zip>
				<point>
					<x>116.32317954086</x>
					<y>39.98301950182</y>
				</point>
			</poi>
			<poi>
				<addr>中关村大街19号新中关购物中心1楼</addr>
				<distance>112.983688</distance>
				<name>星巴克新中关店</name>
				<poiType>星巴克,咖啡厅,休闲餐饮,餐饮</poiType>
				<tel>(010)82486056</tel>
				<zip></zip>
				<point>
					<x>116.32218215226</x>
					<y>39.983899777278</y>
				</point>
			</poi>
		</pois>
	</result>
</GeocoderSearchResponse>

 从xml可以分析成几个实体类

 

  

 

 

纯粹的解析

 

package com.app.duapp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.app.common.AppDbUtil;
import com.app.entity.duapp.Address;
import com.app.entity.duapp.Geocoder;
import com.app.entity.duapp.Location;
import com.app.entity.duapp.Point;
import com.app.entity.duapp.Pois;
/**
 * @author liangjilong
 */
@SuppressWarnings("all")
public class Deocoder {
	
	public static void main(String[] args)throws Exception {
		String url = "http://api.map.baidu.com/geocoder/v2/?ak=E4805d16520de693a3fe707cdc962045&callback=renderReverse&location=39.983424,116.322987&output=xml&pois=1";
		readXml(url);
	}
	/**
	 * 
	 * @param url xml地址
	 * @return
	 */
	public static void readXml(String url) {
		List<Geocoder> list=new ArrayList<Geocoder>();
		try {
			SAXReader reader = new SAXReader();
			Document document =reader.read(url);//如果是本地的xml就new File(filename);
			Element root = document.getRootElement();//获取根节点
			System.out.println((root.elementText("status")));//状态-->第一个大节点
		
			Iterator<Element> iter = root.elementIterator("result");//第二大节点
			for (Iterator iter1 =iter; iter1.hasNext();) {
				Element Node1=(Element)iter1.next();
				
				Element locationNode=Node1.element("location");//location节点
				System.out.println(locationNode.elementText("lat"));//纬度
				String lng=locationNode.elementText("lng");//经度
				String format_addr=Node1.elementText("formatted_address");
				String business=Node1.elementText("business");
				
				Address address=new Address();
				Element addressComponent=Node1.element("addressComponent");//addressComponent节点
				if(addressComponent.elementText("streetNumber")!=""&&addressComponent.elementText("streetNumber")!=null)
				{
					address.setStreetNumber(addressComponent.elementText("streetNumber"));
				}
				System.out.println(addressComponent.elementText("street"));
				System.out.println(addressComponent.elementText("district"));
				System.out.println(addressComponent.elementText("city"));
				System.out.println(addressComponent.elementText("province"));
				System.out.println(Node1.elementText("cityCode"));//cityCode节点

			    Element poisNode=Node1.element("pois");//pois节点
			    
			    for (Iterator iter2 = poisNode.elementIterator(); iter2.hasNext();) {
					Element poisNodes=(Element)iter2.next();
					System.out.println(poisNodes.elementText("addr"));
					System.out.println(poisNodes.elementText("distance"));
					System.out.println(poisNodes.elementText("name"));
					System.out.println(poisNodes.elementText("tel"));
					if(poisNodes.elementText("zip")!=""&&poisNodes.elementText("zip")!=null)
			        {
						System.out.println(poisNodes.elementText("zip"));
					}
					
					Element point=poisNodes.element("point");//point节点
					
					System.out.println(point.elementText("x"));
					System.out.println(point.elementText("y"));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	 
}

 

 Jsoup的解析xml 

 

package com.app.duapp;
import java.util.Iterator;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
/**
 * @author liangjilong
 */
@SuppressWarnings("all")
public class JsoupXml {
	
	public static void main(String[] args)throws Exception {
		String url = "http://api.map.baidu.com/geocoder/v2/?ak=E4805d16520de693a3fe707cdc962045&callback=renderReverse&location=39.983424,116.322987&output=xml&pois=1";
		//readXml(url);
		JsoupReadXml(url);
	}
	/**
	 * 
	 * @param url xml地址
	 * @return
	 */
	public static void readXml(String url) throws Exception{ 
		
		Document doc=Jsoup.connect(url).get();
		
		Elements status=doc.select("status");//status节点
		for(Element st:status){
			System.out.println(st.text());//获取值
		}
		Elements lats=doc.select("lat");//lat节点
		for(Element lat:lats){
			System.out.println(lat.text());
		}
		Elements lngs=doc.select("lng");//lng节点
		for(Element lng:lngs){
			System.out.println(lng.text());
		}
		Elements formatted_address=doc.select("formatted_address");//formatted_address节点
		for(Element a:formatted_address){
			System.out.println(a.text());
		}
		
		Elements businesss=doc.select("business");//business节点
		for(Element b:businesss){
			System.out.println(b.text());
		}
		Elements streetNumbers=doc.select("streetNumber");//streetNumber节点
		for(Element sn:streetNumbers){
			System.out.println(sn.text());
		}
		Elements streets=doc.select("street");//street节点
		for(Element st:streets){
			System.out.println(st.text());
		}
		Elements districts=doc.select("district");//district节点
		for(Element d:districts){
			System.out.println(d.text());
		}
		Elements citys=doc.select("city");//city节点
		for(Element c:citys){
			System.out.println(c.text());
		}
		Elements provinces=doc.select("province");//province节点
		for(Element p:provinces){
			System.out.println(p.text());
		}
		Elements cityCodes=doc.select("cityCode");//cityCode节点
		for(Element cc:cityCodes){
			System.out.println(cc.text());
		}
		Elements addrs=doc.select("addr");//addr节点
		for(Element add:addrs){
			System.out.println(add.text());
		}
		Elements distances=doc.select("distance");//distance节点
		for(Element d:distances){
			System.out.println(d.text());
		}
		Elements names=doc.select("name");//name节点
		for(Element n:names){
			System.out.println(n.text());
		}
		Elements poiTypes=doc.select("poiType");//poiType节点
		for(Element pt:poiTypes){
			System.out.println(pt.text());
		}
		Elements tels=doc.select("tel");//tel节点
		for(Element t:tels){
			System.out.println(t.text());
		}
		Elements zips=doc.select("zip");//zip节点
		for(Element z:zips){
			System.out.println(z.text());
		}
		Elements xs=doc.select("x");//zip节点
		for(Element x:xs){
			System.out.println(x.text());
		}
		Elements ys=doc.select("y");//zip节点
		for(Element y:ys){
			System.out.println(y.text());
		}
		
	}
	
	/**
	 * 用Jsoup去解析xml
	 * @param url
	 */
	private static void JsoupReadXml(String url) throws Exception{
		org.jsoup.nodes.Document doc=Jsoup.connect(url).get();
		if(doc!=null)
		{
			Elements pois=doc.select("poi");//获取到poi节点
			for(org.jsoup.nodes.Element poi:pois){
				
				String addr=poi.getElementsByTag("addr").text().trim();
				String distance=poi.getElementsByTag("distance").text().trim();
				String name=poi.getElementsByTag("name").text().trim();
				String poiType=poi.getElementsByTag("poiType").text().trim();
				String tel=poi.getElementsByTag("tel").text().trim();
				String zip=poi.getElementsByTag("zip").text().trim();
				String x=poi.getElementsByTag("x").text().trim();
				String y=poi.getElementsByTag("y").text().trim();
				
				Elements comps=doc.select("addressComponent");
				
				for (org.jsoup.nodes.Element comp:comps) {
					String streetNumber=comp.getElementsByTag("streetNumber").text().trim();
					String street=comp.getElementsByTag("street").text().trim();
					String district=comp.getElementsByTag("district").text().trim();
					String city=comp.getElementsByTag("city").text().trim();
					String province=comp.getElementsByTag("province").text().trim();
				}
				
				String	lat=doc.select("lat").text().trim();
				String	lng=doc.select("lng").text().trim();
				String	formatted_address=doc.select("formatted_address").text().trim();
				String	business=doc.select("business").text().trim();
			}
		}
	}
	 
}

 

 

 

几个实体

package com.app.entity.duapp;

import java.io.Serializable;

public class Address implements Serializable{
	private String streetNumber;
	private String street;
	private String district;
	private String city;
	private String province;
	private String cityCode;
	public String getCityCode() {
		return cityCode;
	}
	public void setCityCode(String cityCode) {
		this.cityCode = cityCode;
	}
	public String getStreetNumber() {
		return streetNumber;
	}
	public void setStreetNumber(String streetNumber) {
		this.streetNumber = streetNumber;
	}
	public String getStreet() {
		return street;
	}
	public void setStreet(String street) {
		this.street = street;
	}
	public String getDistrict() {
		return district;
	}
	public void setDistrict(String district) {
		this.district = district;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getProvince() {
		return province;
	}
	public void setProvince(String province) {
		this.province = province;
	}
	
}

封装型解析:

package com.app.duapp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.app.common.AppDbUtil;
import com.app.entity.duapp.Address;
import com.app.entity.duapp.Geocoder;
import com.app.entity.duapp.Location;
import com.app.entity.duapp.Point;
import com.app.entity.duapp.Pois;

@SuppressWarnings("all")
public class DeocoderDao {
	/**
	 * 
	 * @param url xml地址
	 * @return
	 */
	public  List<Geocoder> readXml(String url) {
		List<Geocoder> list=new ArrayList<Geocoder>();
		try {
			SAXReader reader = new SAXReader();
			Document document =reader.read(url);//如果是本地的xml就new File(filename);
			Element root = document.getRootElement();//获取根节点
			Geocoder g=new Geocoder();
			g.setStatus(root.elementText("status"));//状态-->第一个大节点
		
			Iterator<Element> iter = root.elementIterator("result");//第二大节点
			for (Iterator iter1 =iter; iter1.hasNext();) {
				Element Node1=(Element)iter1.next();
				
				Location location=new Location();
				List<Location> listLocations=new ArrayList<Location>();
				
				Element locationNode=Node1.element("location");//location节点
				String lat=locationNode.elementText("lat");//纬度
				String lng=locationNode.elementText("lng");//经度
				String format_addr=Node1.elementText("formatted_address");
				String business=Node1.elementText("business");
				location.setLat(lat);
				location.setLng(lng);
				location.setBusiness(business);
				location.setFormattedAddress(format_addr);
				listLocations.add(location);//把location对象丢进集合里面
				g.setLocations(listLocations);//把集合丢进集合里面
				
				
				List<Address> listAddress=new ArrayList<Address>();
				Address address=new Address();
				Element addressComponent=Node1.element("addressComponent");//addressComponent节点
				if(addressComponent.elementText("streetNumber")!=""&&addressComponent.elementText("streetNumber")!=null)
				{
					address.setStreetNumber(addressComponent.elementText("streetNumber"));
				}
				address.setStreet(addressComponent.elementText("street"));
				address.setDistrict(addressComponent.elementText("district"));
				address.setCity(addressComponent.elementText("city"));
				address.setProvince(addressComponent.elementText("province"));
			    address.setCityCode(Node1.elementText("cityCode"));//cityCode节点
			    listAddress.add(address);//把address对象丢进集合里面

			    g.setAddress(listAddress);//把集合丢进集合里面
			    
			    Element poisNode=Node1.element("pois");//pois节点
			    
			    for (Iterator iter2 = poisNode.elementIterator(); iter2.hasNext();) {
			    	Pois pois=new Pois();
			    	List<Pois> listPois=new ArrayList<Pois>();
					Element poisNodes=(Element)iter2.next();
					
					pois.setAddr(poisNodes.elementText("addr"));
					pois.setDistance(poisNodes.elementText("distance"));
					pois.setName(poisNodes.elementText("name"));
					pois.setTel(poisNodes.elementText("tel"));
					if(poisNodes.elementText("zip")!=""&&poisNodes.elementText("zip")!=null)
			        {
						pois.setZip(poisNodes.elementText("zip"));
					}
					
					Element point=poisNodes.element("point");//point节点
					
					Point p=new Point();
					List<Point> listPoints=new ArrayList<Point>();
					p.setX(point.elementText("x"));
					p.setY(point.elementText("y"));
					listPoints.add(p);//point
					pois.setPoints(listPoints);
					listPois.add(pois);//把对象pois丢到集合
					g.setPois(listPois);//把集合丢到集合
				}
				list.add(g);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	/**
	 * 保存
	 */
	public static int batchsSaveData(List<Geocoder> list) {
		int index=0;
		String sqls[]=new String[list.size()];//多条SQL
		for (int i = 0; i < list.size(); i++) {
			StringBuffer buffer=new StringBuffer();
			Geocoder g=(Geocoder) list.get(i);
			String statue=g.getStatus();
			List<Location> listLocations=g.getLocations();//Locations
			List<Address> listAddress=g.getAddress();//Address
			List<Pois> listPois=g.getPois();//Pois
			
			for (Iterator iterator1 = listLocations.iterator(); iterator1.hasNext();) {
				Location location = (Location) iterator1.next();
				String lat=location.getLat();
				String lng=location.getLng();
			    String business=location.getBusiness();
			    String formattedAddress=location.getFormattedAddress();
			    
				for (Iterator iterator2 = listAddress.iterator(); iterator2.hasNext();) {
					Address addr = (Address) iterator2.next();
					
					String city=addr.getCity();
					String cityCode=addr.getCityCode();
					String district=addr.getDistrict();
					String province=addr.getProvince();
					String street=addr.getStreet();
					String StreetNumber=addr.getStreetNumber();
					
					for (Iterator iterator3 = listPois.iterator(); iterator3.hasNext();) {
						Pois p = (Pois) iterator3.next();
						String add=p.getAddr();
						String distance=p.getDistance();
						String name=p.getName();
						String poiType=p.getPoiType();
						String tel=p.getTel();
						String zip=p.getZip();
						
						List<Point> listPoints=p.getPoints();
						
						for (Iterator iterator4 = listPoints.iterator(); iterator4.hasNext();) {
							Point point = (Point) iterator4.next();
							String x=point.getX();
							String y=point.getY();
							
							buffer.append("Insert into GeocoderSearch(status,lat,lng,formatted_address,business,streetNumber,street,district,city,province,cityCode,addr,distance,name,poiType,tel,zip,x,y) values(");
						    buffer.append("'"+statue+"',").append("'"+lat+"',");
						    buffer.append("'"+lng+"',").append("'"+formattedAddress+"',");
						    buffer.append("'"+business+"',").append("'"+StreetNumber+"',");
						    buffer.append("'"+street+"',").append("'"+district+"',");
						    buffer.append("'"+city+"',").append("'"+province+"',");
						    buffer.append("'"+cityCode+"',").append("'"+add+"',");
						    buffer.append("'"+distance+"',").append("'"+name+"',");
						    buffer.append("'"+poiType+"',").append("'"+tel+"',");
						    buffer.append("'"+zip+"',").append("'"+x+"',").append("'"+y+"'");
						    buffer.append(")");
						    String sql=buffer.toString();
						    sqls[index]=sql;	
						}
					}
				}
			}
			index++;
		}
		int rows=AppDbUtil.getInstance().supportsBatch(sqls);
		return rows;
	}
}

 

package com.app.entity.duapp;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class Pois implements Serializable{
	private String addr;
	private String distance;
	private String name;
	private String poiType;
	private String tel;
	private String zip;
	private List<Point> points=new ArrayList<Point>();
	
	public String getAddr() {
		return addr;
	}
	public void setAddr(String addr) {
		this.addr = addr;
	}
	public String getDistance() {
		return distance;
	}
	public void setDistance(String distance) {
		this.distance = distance;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPoiType() {
		return poiType;
	}
	public void setPoiType(String poiType) {
		this.poiType = poiType;
	}
	public String getTel() {
		return tel;
	}
	public void setTel(String tel) {
		this.tel = tel;
	}
	public String getZip() {
		return zip;
	}
	public void setZip(String zip) {
		this.zip = zip;
	}
	public List<Point> getPoints() {
		return points;
	}
	public void setPoints(List<Point> points) {
		this.points = points;
	}
	
}

 

package com.app.entity.duapp;

import java.io.Serializable;

public class Point implements Serializable{
	private String x;
	private String y;
	public String getX() {
		return x;
	}
	public void setX(String x) {
		this.x = x;
	}
	public String getY() {
		return y;
	}
	public void setY(String y) {
		this.y = y;
	}
	
}

 

package com.app.entity.duapp;

import java.io.Serializable;

public class Location implements Serializable{
	private  String lat;//纬度
	private  String lng;//经度
	private  String formattedAddress;//经度
	private  String business;//经度
	public String getLat() {
		return lat;
	}
	public void setLat(String lat) {
		this.lat = lat;
	}
	public String getLng() {
		return lng;
	}
	public void setLng(String lng) {
		this.lng = lng;
	}
	public String getFormattedAddress() {
		return formattedAddress;
	}
	public void setFormattedAddress(String formattedAddress) {
		this.formattedAddress = formattedAddress;
	}
	public String getBusiness() {
		return business;
	}
	public void setBusiness(String business) {
		this.business = business;
	}
}

 

package com.app.entity.duapp;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class Geocoder  implements Serializable{
	private String status;
	private List<Location> locations=new ArrayList<Location>();
	private List<Address> address=new ArrayList<Address>();
	private List<Pois> pois=new ArrayList<Pois>();
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	public List<Location> getLocations() {
		return locations;
	}
	public void setLocations(List<Location> locations) {
		this.locations = locations;
	}
	public List<Address> getAddress() {
		return address;
	}
	public void setAddress(List<Address> address) {
		this.address = address;
	}
	public List<Pois> getPois() {
		return pois;
	}
	public void setPois(List<Pois> pois) {
		this.pois = pois;
	}
}

 

 

连接数据库:

package com.app.common;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.hibernate.SQLQuery;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
@SuppressWarnings("all")
public class AppDbUtil  extends HibernateDaoSupport{
	private static String Driver=PropertyUtil.getProperty("jdbc.driver");
	private static String url=PropertyUtil.getProperty("jdbc.url");
	private static String url1=PropertyUtil.getProperty("jdbc.url");
	private static String username=PropertyUtil.getProperty("jdbc.username");
	private static String password=PropertyUtil.getProperty("jdbc.password");
	
	private static AppDbUtil instance=null;
	private static Connection con = null;
	private static Statement stmt = null;
	private static PreparedStatement  pst = null;
	private static ResultSet rs = null;
	/**
	 * Single
	 * @return
	 */
	public static AppDbUtil getInstance(){
		if (instance == null) {
			synchronized (AppDbUtil.class) {
				instance = new AppDbUtil();
			}
		}
		return instance;
	}
	
	/**
	 * 通过过文件处理流加载配置,读key获取value值
	 * @return
	 */
	public static Connection getConnection() {
		Connection conn=null;
		try {
			Class.forName(Driver);
			conn=DriverManager.getConnection(url, username, password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	/**
	 * @return
	 */
	public static Connection getConn() {
		Connection conn=null;
		try {
			Class.forName(Driver);
			conn=DriverManager.getConnection(url1, username, password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	/**
	 * 调用log4通过key值去获取value值
	 * @return
	 */
	public static Connection getConfigConnection() {
		try {
			Class.forName(Driver);
			con = DriverManager.getConnection(url1, username, password);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return con;
	}
	/**
	 * 支持批量处理delete update insert
	 * @param sqls
	 * @return
	 */
	public  int supportsBatch(Object [] sqls){
		
		try {
			con=AppDbUtil.getInstance().getConnection();
			con.setAutoCommit(false);
			DatabaseMetaData dma=con.getMetaData();
			
			if(dma.supportsBatchUpdates()){
				stmt=con.createStatement();
				for (int i = 0; i < sqls.length; i++) {
					stmt.addBatch(sqls[i]+"");
				}
				int [] rows=stmt.executeBatch();
				con.commit();
				return rows.length;
			}else{
				//HQL或SQL
				for (int i = 0; i < sqls.length; i++) {
					SQLQuery query=this.getSession().createSQLQuery(sqls[i]+"");
					query.list();
				}
				return sqls.length;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			Relesae(null, stmt, con);
		}
		return 0;
	}
	
	/***
	 * 释放资源...
	 * @param rs
	 * @param st
	 * @param conn
	 */
	public static void Relesae(ResultSet rs, Statement st, Connection conn) {
		try {
			if (rs != null)
				rs.close();
			if (st != null)
				st.close();
			if (conn != null)
				conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	 
}

 

 

 

 

 

package com.app.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

import org.apache.log4j.PropertyConfigurator;

public class PropertyUtil {
	
	protected static Properties pro = null;
	protected static InputStream is = null;
	private static File file = null;
	private static long fileLastModified = 0L;

	public static synchronized Properties getInstance() {
		if(pro==null){
			  pro=new Properties();
		}
		return pro;
	}
	/**
	 * 初始化
	 */
	private static void init() {
		URL url = PropertyUtil.class.getClassLoader().getResource(
				"/jdbc.properties");
		file = new File(url.getFile());
		fileLastModified = file.lastModified();
		pro = new Properties();
		load();
	}
	/**
	 * 加载
	 */
	private static void load() {
		try {
			pro.load(new FileInputStream(file));
			fileLastModified = file.lastModified();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 获取key
	 * @param key
	 * @return
	 */
	public static String getProperty(String key) {
		if ((file == null) || (pro == null)) {
			init();
		}
		if (file.lastModified() > fileLastModified) {
			load();
		}
		return pro.getProperty(key);
	}

	/**
	 * @param key
	 * @return
	 */
	public static String getLog4jConfigKey(String key) {
		String strVal = "";
		String path="/jdbc.properties";
		try {
			// is=PropertyUtil.class.getClass().getResourceAsStream(path);
			is = new FileInputStream(new File(path));
			pro = new Properties();
			pro.load(is);

			PropertyConfigurator.configure(pro);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		strVal = (String) pro.getProperty(key);
		return strVal;
	}
	
}

 数据库配置:jdbc.properties

#################################MYSQL#############################################
jdbc.url=jdbc:mysql://localhost:3306/moa?characterEncoding=GBK
jdbc.username=root
jdbc.password=root
jdbc.driver=com.mysql.jdbc.Driver

 

 

spring定时器配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

        <!-- 要调用的工作类 -->
        <bean id="duApp" class="com.app.timer.DuApp"/>
        
         <bean id="taskDuApp" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            <property name="targetObject" ref="duApp"/>
            <property name="targetMethod" value="doDeocoder"/>
        </bean>
        
        
        <bean id="TriggerDuApp" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail">
			<ref bean="taskDuApp"/>
		</property>
              <property name="cronExpression">
				<!--<value>0 0 12 ? * *</value> -->
				<value>0 33 20 * * ?</value>
            </property>
        </bean>
        
        <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序  -->
        <bean autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <property name="triggers">
                <list>
			<!--   <ref bean="TriggerQuart"/>
		 	  <ref bean="TriggerSpider"/> -->
		 	  <ref bean="TriggerDuApp"/>
                </list>
            </property>
        </bean>
    
</beans>

 数据库脚步

-- ----------------------------
-- Table structure for `geocodersearch`
-- ----------------------------
DROP TABLE IF EXISTS `geocodersearch`;
CREATE TABLE `geocodersearch` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `lat` varchar(25) CHARACTER SET gbk DEFAULT '',
  `lng` varchar(25) CHARACTER SET gbk DEFAULT NULL,
  `formatted_address` varchar(150) CHARACTER SET gbk DEFAULT '',
  `business` varchar(150) DEFAULT NULL,
  `streetNumber` varchar(25) CHARACTER SET gbk DEFAULT '',
  `street` varchar(35) CHARACTER SET gbk DEFAULT '',
  `district` varchar(25) CHARACTER SET gbk DEFAULT '',
  `city` varchar(25) CHARACTER SET gbk DEFAULT '',
  `province` varchar(25) CHARACTER SET gbk DEFAULT '',
  `cityCode` varchar(25) CHARACTER SET gbk DEFAULT '',
  `addr` varchar(150) CHARACTER SET gbk DEFAULT '',
  `distance` varchar(150) CHARACTER SET gbk DEFAULT '',
  `name` varchar(150) CHARACTER SET gbk DEFAULT '',
  `poiType` varchar(100) CHARACTER SET gbk DEFAULT '',
  `tel` varchar(15) CHARACTER SET gbk DEFAULT '',
  `zip` varchar(15) CHARACTER SET gbk DEFAULT '',
  `x` varchar(25) CHARACTER SET gbk DEFAULT '',
  `y` varchar(25) CHARACTER SET gbk DEFAULT '',
  `status` varchar(5) CHARACTER SET gbk DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

 web.xml配置

 

<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>/WEB-INF/spring/applicationContextion.xml</param-value>
</context-param>

<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

 定时器测试类

 

package com.app.timer;

import java.util.List;

import com.app.duapp.DeocoderDao;
import com.app.entity.duapp.Geocoder;

/**
 *@DEMO:napp
 *@Author:jilongliang
 *@Date:2013-7-15
 */
public class DuApp {
	public void doDeocoder() {
		DeocoderDao dao=new DeocoderDao();
		String url = "http://api.map.baidu.com/geocoder/v2/?ak=E4805d16520de693a3fe707cdc962045&callback=renderReverse&location=39.983424,116.322987&output=xml&pois=1";
		List<Geocoder> list=dao.readXml(url);
		int result=dao.batchsSaveData(list);
		if(result>0){
			System.out.println("成功保存"+result+"条数据");
		}else{
			System.out.println("保存失败..");
		}
	}
}

 源代码:https://github.com/jilongliang/JL_NAPP.git

你可能感兴趣的:(xml,JSoup,quarts,supportsBatch)