dom4j和jsoup解析百度地图xml获取地方信息

阅读更多
package ivyy.taobao.com.dom4j;
import ivyy.taobao.com.entity.Address;
import ivyy.taobao.com.entity.Location;
import ivyy.taobao.com.entity.Point;
import ivyy.taobao.com.entity.Pois;
import ivyy.taobao.com.utils.IoUtils;
import ivyy.taobao.com.utils.UrlUtils;
import ivyy.taobao.com.utils.Dom4jUtils;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.jsoup.Jsoup;
import org.jsoup.select.Elements;
/**
 *@Date:2015-1-6
 *@Author:liangjilong
 *@Email:[email protected]
 *@Version:1.0
 *@Description:
 */
@SuppressWarnings("all")
public class Dom4jTest2 {
	
	public static void main(String[] args)throws Exception {
		//String filepath="D:/"+System.currentTimeMillis()+".xml";
		String filepath="D:/test/map1.xml";
		File f=new File(filepath);
		if(!f.exists()){
			f.createNewFile();
		}
		//List list=getReaderXml("URL");
		List list=getReaderXml("FILE");
		org.dom4j.Document doc=createAsXML(list);
		IoUtils.write(doc.asXML(),filepath);
		//格式化 
		Dom4jUtils.formatAsXml(doc);
	}
	
	 
	/****
	 * 组装成一个xml
	 * @param list
	 * @return
	 * @throws Exception
	 */
	private static org.dom4j.Document createAsXML(List list) throws Exception{
		org.dom4j.Document doc=DocumentHelper.createDocument();
		Element  root=doc.addElement("GeocoderSearchResponse");//根
		root.addElement("status").setText("0");//status
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			Pois pois = (Pois) iterator.next();

			Element result=root.addElement("result");//result
			List  listLoc=pois.getLocations();
			
			Element location=result.addElement("location");//location
			for (Iterator iterator2 = listLoc.iterator(); iterator2.hasNext();) {
				Location locObj = (Location) iterator2.next();
				location.addElement("lat").setText(locObj.getLat()+"");//lat
				location.addElement("lng").setText(locObj.getLng()+"");//lat
				result.addElement("formatted_address").setText(locObj.getFormattedAddress()+"");//formatted_address
				result.addElement("business").setText(locObj.getBusiness()+"");//business
			}
			
			List
listAdd=pois.getAddress(); Element comp=result.addElement("addressComponent");//addressComponent for (Iterator iterator3 = listAdd.iterator(); iterator3.hasNext();) { Address address = (Address) iterator3.next(); comp.addElement("streetNumber").setText(address.getStreetNumber()+"");//streetNumber comp.addElement("street").setText(address.getStreet()+"");//street comp.addElement("district").setText(address.getDistrict()+"");//district comp.addElement("city").setText(address.getCity()+"");//city comp.addElement("province").setText(address.getProvince()+"");//province comp.addElement("cityCode").setText(address.getCityCode()+"");//cityCode } Element poi=result.addElement("pois").addElement("poi"); poi.addElement("addr").setText(pois.getAddr());//addr poi.addElement("distance").setText(pois.getDistance());//distance poi.addElement("name").setText(pois.getName());//name poi.addElement("poiType").setText(pois.getPoiType());//poiType poi.addElement("tel").setText(pois.getTel());//tel poi.addElement("zip").setText(pois.getZip());//zip List listPoint=pois.getPoints(); Element point=poi.addElement("point"); for (Iterator iterator4 = listPoint.iterator(); iterator4.hasNext();) { Point p = (Point) iterator4.next(); point.addElement("x").setText(p.getX()+""); point.addElement("y").setText(p.getY()+""); } } return doc; } /** * Dom4j(SAX)读取xml数据(解析) * @param params * @throws Exception */ private static List getReaderXml(String flg) throws Exception{ String fromRead=Dom4jTest2.class.getClassLoader().getResource("xml/map1.xml").getPath(); List list=new ArrayList(); SAXReader saxReader = new SAXReader(); org.dom4j.Document document=null; //从api上面解析 if(flg.equals("URL")){ String url = UrlUtils.getBaiduMapUrl("你的key", "39.983424,116.322987", "xml"); document = saxReader.read(url); //从文件上面的xml解析 }else if(flg.equals("FILE")){ document = saxReader.read(new File(fromRead)); } Element resultEl = (Element)document.getRootElement().element("result"); Element poisEl=resultEl.element("pois");//pois节点 Element locationEl=resultEl.element("location");//location节点 Element addressEl=resultEl.element("addressComponent");//addressComponent节点 /*******从pois节点下面遍历多个poi节点*******/ for (Iterator poIter = poisEl.elementIterator("poi"); poIter.hasNext();) { Element element = (Element) poIter.next(); String addr = element.elementText("addr"); String distance = element.elementText("distance"); String name = element.elementText("name"); String poiType = element.elementText("poiType"); String tel =(element.elementText("tel")==""?"":element.elementText("tel")); String zip =(element.elementText("zip")==""?"":element.elementText("zip")); Pois poi=new Pois(); poi.setAddr(addr); poi.setDistance(distance); poi.setName(name); poi.setPoiType(poiType); poi.setTel(tel); poi.setZip(zip); List listLoc=new ArrayList(); /************Location***************************/ String business=resultEl.elementText("business"); String formatted_address=resultEl.elementText("formatted_address"); String lat = locationEl.elementText("lat"); String lng = locationEl.elementText("lng"); Location location=new Location(); location.setBusiness(business); location.setFormattedAddress(formatted_address); location.setLat(lat); location.setLng(lng); listLoc.add(location); poi.setLocations(listLoc); List
listAddr=new ArrayList
(); /************Address***************************/ Address address=new Address(); String streetNumber=(addressEl.elementText("streetNumber")==""?"":addressEl.elementText("streetNumber")); String street=(addressEl.elementText("street")==""?"":addressEl.elementText("street")); String district=(addressEl.elementText("district")==""?"":addressEl.elementText("district")); String city=(addressEl.elementText("city")==""?"":addressEl.elementText("city")); String province=(addressEl.elementText("province")==""?"":addressEl.elementText("province")); String direction=(addressEl.elementText("direction")==""?"":addressEl.elementText("direction")); String distancez=(addressEl.elementText("distance")==""?"":addressEl.elementText("distance")); address.setStreetNumber(streetNumber); address.setStreet(street); address.setCity(city); address.setDistrict(district); address.setDirection(direction); address.setDistance(distancez); address.setProvince(province); listAddr.add(address); poi.setAddress(listAddr); list.add(poi); } return list; } }

 

package ivyy.taobao.com.jsoup;
import ivyy.taobao.com.dom4j.Dom4jTest1;
import ivyy.taobao.com.entity.Address;
import ivyy.taobao.com.entity.Location;
import ivyy.taobao.com.entity.Point;
import ivyy.taobao.com.entity.Pois;
import ivyy.taobao.com.utils.IoUtils;
import ivyy.taobao.com.utils.UrlUtils;
import ivyy.taobao.com.utils.Dom4jUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.jsoup.Jsoup;
import org.jsoup.select.Elements;
/**
 *@Date:2015-1-6
 *@Author:liangjilong
 *@Email:[email protected]
 *@Version:1.0
 *@Description:
 */
@SuppressWarnings("all")
public class JsoupMap {
	
	public static void main(String[] args)throws Exception {
		String Encodeing="GBK";
		//List list=JsoupReadXml("URL",Encodeing);//从api读取

		//------------------------------可以將這些數據保存到數據庫-------------------
		List list=JsoupReadXml("FILE",Encodeing);//从本地的文件读取
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			Pois pois = (Pois) iterator.next();
			
			//System.out.println(pois.getAddr());
			
			//------------------------------Address-----------------
			
			List
listAdds=pois.getAddress(); for (Iterator iterator2 = listAdds.iterator(); iterator2.hasNext();) { Address address = (Address) iterator2.next(); System.out.println(address.getCity()); } //------------------------------Location----------------- List locations = pois.getLocations(); for (Iterator iterator2 = locations.iterator(); iterator2.hasNext();) { Location location = (Location) iterator2.next(); System.out.println(location.getFormattedAddress()); } } } /** * 用Jsoup去解析xml * @param params */ private static List JsoupReadXml(String flg,String Encodeing) throws Exception{ List list=new ArrayList(); String url = UrlUtils.getBaiduMapUrl("你的key", "39.983424,116.322987", "xml"); org.jsoup.nodes.Document doc=null; if(flg.equals("URL")){ doc=Jsoup.connect(url).get();//网络连接 }else if(flg.equals("FILE")){ String fromRead=Dom4jTest1.class.getClassLoader().getResource("xml/map1.xml").getPath(); String html=IoUtils.reader(fromRead); doc=Jsoup.parse(html,Encodeing); } //org.jsoup.nodes.Document doc=Jsoup.parse(params);//本地数据,连接关闭就可以调用 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 tel=poi.getElementsByTag("tel").text().trim(); String zip=poi.getElementsByTag("zip").text().trim(); String poiType=poi.getElementsByTag("poiType").text().trim(); Pois p=new Pois(); p.setAddr(addr); p.setDistance(distance); p.setName(name); p.setPoiType(poiType); p.setTel(tel); p.setZip(zip); List listPoint=new ArrayList(); Point point=new Point(); String x=poi.getElementsByTag("x").text().trim(); point.setX(x); String y=poi.getElementsByTag("y").text().trim(); point.setY(y); listPoint.add(point); p.setPoints(listPoint); List
listAdd=new ArrayList
(); Elements comps=doc.select("addressComponent"); for (org.jsoup.nodes.Element comp:comps) { String streetNumber=comp.getElementsByTag("streetNumber").text().trim(); String district=comp.getElementsByTag("district").text().trim(); String street=comp.getElementsByTag("street").text().trim(); String city=comp.getElementsByTag("city").text().trim(); String province=comp.getElementsByTag("province").text().trim(); Address add=new Address(); add.setStreetNumber(streetNumber); add.setStreet(street); add.setDistrict(district); add.setCity(city); add.setProvince(province); listAdd.add(add); } p.setAddress(listAdd); List listLoc=new ArrayList(); Elements location=doc.select("location"); for (org.jsoup.nodes.Element obj:location) { String lat=obj.getElementsByTag("lat").text().trim(); String formatted_address=obj.getElementsByTag("formatted_address").text().trim(); String lng=obj.getElementsByTag("lng").text().trim(); String business=doc.select("business").text().trim(); Location locat=new Location(); locat.setLng(lng); locat.setLat(lat); locat.setFormattedAddress(formatted_address); locat.setBusiness(business); listLoc.add(locat); } String status=doc.select("status").text().trim(); p.setStatus(status); p.setLocations(listLoc); list.add(p); } } return list; } }

 

package ivyy.taobao.com.entity;

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 ivyy.taobao.com.entity;

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 ivyy.taobao.com.entity;

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 String status;
	private String url;
	private String mapUrl;
	private List points=new ArrayList();
	private List locations=new ArrayList();
	private List
address=new ArrayList
(); private List pois=new ArrayList(); public String getStatus() { return status; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public void setStatus(String status) { this.status = status; } public List getLocations() { return locations; } public void setLocations(List locations) { this.locations = locations; } public List
getAddress() { return address; } public void setAddress(List
address) { this.address = address; } public List getPois() { return pois; } public void setPois(List pois) { this.pois = pois; } 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 getPoints() { return points; } public void setPoints(List points) { this.points = points; } public String getMapUrl() { return mapUrl; } public void setMapUrl(String mapUrl) { this.mapUrl = mapUrl; } }

 
dom4j和jsoup解析百度地图xml获取地方信息_第1张图片
 
dom4j和jsoup解析百度地图xml获取地方信息_第2张图片


 

源代码  http://download.csdn.net/detail/jilongliang/8333361

 

  • dom4j和jsoup解析百度地图xml获取地方信息_第3张图片
  • 大小: 20.3 KB
  • dom4j和jsoup解析百度地图xml获取地方信息_第4张图片
  • 大小: 161.5 KB
  • 查看图片附件

你可能感兴趣的:(dom4j,xml,jsoup)