java查询WFS服务

在我们访问wfs服务时候,有时候会遇到前台访问时候的跨域问题。这里给出java访问的一个小例子。

  1 import java.io.BufferedReader;
  2 import java.io.IOException;
  3 import java.io.InputStream;
  4 import java.io.InputStreamReader;
  5 import java.io.UnsupportedEncodingException;
  6 import java.util.List;
  7 
  8 import org.apache.commons.httpclient.HttpClient;
  9 import org.apache.commons.httpclient.HttpException;
 10 import org.apache.commons.httpclient.methods.PostMethod;
 11 import org.apache.commons.httpclient.methods.StringRequestEntity;
 12 import org.apache.commons.httpclient.params.HttpMethodParams;
 13 import org.dom4j.Document;
 14 import org.dom4j.DocumentException;
 15 import org.dom4j.DocumentHelper;
 16 import org.dom4j.Element;
 17 import org.dom4j.QName;
 18 
 19 public class WFSUtil
 20 {
 21     
 22     /**
 23      * 得到过滤结果。名字默认是Poi
 24      * 
 25      * @param keyWords
 26      * @param wfsUrl
 27      * @return
 28      * @throws HttpException
 29      * @throws IOException
 30      */
 31     public String findPointsXmlByKeywords(String[] keyWords, String wfsUrl,
 32             String wfsTitle, String layerName) throws HttpException,
 33             IOException
 34     {
 35         if (keyWords.length==0)
 36         {
 37             return "<root/>";
 38         }
 39 
 40         if (wfsTitle == null)
 41         {
 42             wfsTitle = "ST";
 43         }
 44         if (layerName == null)
 45         {
 46             layerName = "POI";
 47         }
 48         StringBuffer buffer = new StringBuffer(
 49                 "<?xml version='1.0' encoding='UTF-8'?>"
 50                         + "<wfs:GetFeature maxFeatures='100' service='WFS' version='1.1.0' "
 51                         + "xmlns:wfs='http://www.opengis.net/wfs' "
 52                         + "xmlns:gml='http://www.opengis.net/gml' "
 53                         + "xmlns:ogc='http://www.opengis.net/ogc' "
 54                         + "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
 55                         + "xsi:schemaLocation='http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.1.0/wfs.xsd'>"
 56                         + "<wfs:Query typeName='" + layerName
 57                         + "' srsName='EPSG:4326'>"
 58                         + "<ogc:Filter xmlns:ogc='http://www.opengis.net/ogc'>");
 59 
 60         if (keyWords.length > 1)
 61         {
 62             buffer.append("<ogc:Or>");
 63             for (int i = 0; i < keyWords.length; i++)
 64             {
 65                 buffer.append(addPropertyIsLike(keyWords[i]));
 66             }
 67             buffer.append("</ogc:Or>");
 68         }
 69         else if (keyWords.length == 1)
 70         {
 71             buffer.append(addPropertyIsLike(keyWords[0]));
 72         }
 73         else
 74         {
 75             return null;
 76         }
 77 
 78         buffer.append("</ogc:Filter></wfs:Query></wfs:GetFeature>");
 79         PostMethod post = new PostMethod(wfsUrl);
 80         post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,
 81                 "utf-8");
 82         post.setRequestEntity(new StringRequestEntity(buffer.toString(),
 83                 "application/xml", "UTF-8"));
 84         HttpClient httpclient = new HttpClient();
 85 
 86         httpclient.executeMethod(post);
 87 
 88         InputStream xmlis = post.getResponseBodyAsStream();
 89         //确定读取编码为utf-8,不设置的话会用系统默认编码,例如在tomcat容器中,就可能乱码。
 90         BufferedReader br = new BufferedReader(new InputStreamReader(xmlis,"utf-8"));
 91 
 92         String tempbf;
 93         StringBuffer html = new StringBuffer(100);
 94         while ((tempbf = br.readLine()) != null)
 95         {
 96             html.append(tempbf);
 97         }
 98         // System.out.println(html.toString());
 99         return formatPoint(html.toString(),keyWords[0]);
100     }
101 
102     private String addPropertyIsLike(String word)
103     {
104         String result = "";
105         if (word != null && (!word.trim().isEmpty()))
106         {
107             result += "<ogc:PropertyIsLike wildCard='*' singleChar='.' escape='!'><ogc:PropertyName>POI_NAME</ogc:PropertyName><ogc:Literal>*"
108                     + word + "*</ogc:Literal></ogc:PropertyIsLike>";
109         }
110         return result;
111     }
112 
113     private String formatPoint(String xml,String keyWord)
114     {
115         try
116         {
117             Document doc = DocumentHelper.parseText(xml);
118             
119             Element root = doc.getRootElement();
120             
121             Element newRoot = DocumentHelper.createElement("root");
122             newRoot.addAttribute("keyword", keyWord);
123         
124             List<Element> list = root.elements("featureMember");
125             
126             for (Element e : list)
127             {
128                 Element point = newRoot.addElement("point");
129                 Element xs = e.element("Poi").element("X");
130                 Element ys = e.element("Poi").element("Y");
131                 if (xs != null&&ys!=null)
132                 {
133                     String text=xs.getText()+","+ys.getText();
134                     point.addElement("xys").setText(text);
135                     point.addElement("type").setText("point");
136                     //point.addElement("x").setText(xs.getText());
137                     //point.addElement("y").setText(ys.getText());
138                 }
139                 point.addElement("name").setText(
140                         e.element("Poi").element("POI_NAME").getText());
141                 point.addElement("address").setText(
142                         e.element("Poi").element("ADDRESS").getText());
143             }
144             return newRoot.asXML();
145             // throw new Exception("formatPoint is fail");
146         }
147         catch (DocumentException e)
148         {
149             // TODO Auto-generated catch block
150             System.out.println("格式化失败!");
151             e.printStackTrace();
152         }
153 
154         return null;
155     }
156 
157     
158 }

这里面用到了commons.HttpClientj-3.1.jar和dom4j这两个jar包,如果是不同版本的jar包,写法可能略有不同。

欢迎交流:http://www.cnblogs.com/shizhongtao/p/3468664.html

有时间再整理下……

 

posted @ 2013-12-11 09:36  bingyulei  阅读(1431)  评论(0编辑  收藏  举报