开发工具:
文件大小: 2mb
下载次数: 0
上传时间: 2015-01-06
详细说明:
dom4j,jsoup,jdom,w3cdom,xstream使用代码工程 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.dom 4j.io.SAXReader; import org.jsoup.Jsoup; import org.jsoup.select.Elements; /** *@Date:2015-1-6 *@Author:liangjilong *@Email:jilongliang@sina.com *@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; } } ...展开收缩
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.