package com.app.duapp;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
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;
import com.app.common.FileUtil;
import com.app.entity.duapp.Address;
import com.app.entity.duapp.Location;
import com.app.entity.duapp.Point;
import com.app.entity.duapp.Pois;
/**
* @author liangjilong
*/
@SuppressWarnings("all")
public class JsoupDom {
//private static String url = "http://api.map.baidu.com/geocoder/v2/?ak=E4805d16520de693a3fe707cdc962045&callback=renderReverse&location=39.983424,116.322987&output=xml&pois=1";
private static String url = "http://liangjilong.duapp.com/it/xml.xml";
public static void main(String[] args)throws Exception {
String filepath="D:/map.xml";
List<Pois> list=JsoupReadXml(url,null);
String xml=createAsXml(list);
write(xml,filepath);
}
/**
* Dom4j组装xml数据
* @param html
* @return
*/
private static String createAsXml(List<Pois> 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<Location> 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<Address> 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<Point> 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.asXML();
}
/**
* 一对多的解析http://jilongliang.iteye.com/blog/1906728
*
* 用Jsoup去解析xml
* @param params
*/
private static List<Pois> JsoupReadXml(String params,String flag) throws Exception{
List<Pois> list=new ArrayList<Pois>();
org.jsoup.nodes.Document doc=Jsoup.connect(params).get();//网络连接
//org.jsoup.nodes.Document doc=Jsoup.parse(params);//本地数据,连接关闭就可以调用
if(doc!=null)
{
Elements pois=doc.select("poi");//获取到poi节点
for(org.jsoup.nodes.Element poi:pois){
Pois p=new Pois();
String addr=poi.getElementsByTag("addr").text().trim();
p.setAddr(addr);
String distance=poi.getElementsByTag("distance").text().trim();
p.setDistance(distance);
String name=poi.getElementsByTag("name").text().trim();
p.setName(name);
String poiType=poi.getElementsByTag("poiType").text().trim();
p.setPoiType(poiType);
String tel=poi.getElementsByTag("tel").text().trim();
p.setTel(tel);
String zip=poi.getElementsByTag("zip").text().trim();
p.setZip(zip);
List<Point> listPoint=new ArrayList<Point>();
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<Address> listAdd=new ArrayList<Address>();
Elements comps=doc.select("addressComponent");
for (org.jsoup.nodes.Element comp:comps) {
Address add=new Address();
String streetNumber=comp.getElementsByTag("streetNumber").text().trim();
add.setStreetNumber(streetNumber);
String street=comp.getElementsByTag("street").text().trim();
add.setStreet(street);
String district=comp.getElementsByTag("district").text().trim();
add.setDistrict(district);
String city=comp.getElementsByTag("city").text().trim();
add.setCity(city);
String province=comp.getElementsByTag("province").text().trim();
add.setProvince(province);
listAdd.add(add);
}
p.setAddress(listAdd);
List<Location> listLoc=new ArrayList<Location>();
Elements location=doc.select("location");
for (org.jsoup.nodes.Element obj:location) {
Location locat=new Location();
String lat=obj.getElementsByTag("lat").text().trim();
locat.setLat(lat);
String lng=obj.getElementsByTag("lng").text().trim();
locat.setLng(lng);
String formatted_address=obj.getElementsByTag("formatted_address").text().trim();
locat.setFormattedAddress(formatted_address);
String business=doc.select("business").text().trim();
locat.setBusiness(business);
listLoc.add(locat);
}
String status=doc.select("status").text().trim();
p.setStatus(status);
p.setLocations(listLoc);
list.add(p);
}
}
return list;
}
public static boolean write(String content, String htmlPath) {
boolean flag = true;
try {
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlPath), "UTF-8"));
out.write("\n" + content);
out.close();
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
return flag;
}
}