url链接DOM4J分析

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CreateXmlHostRequest {
     final static Logger log = LoggerFactory.getLogger(CreateXmlHostRequest.class);
    //查询酒店列表
    public static String createSearchHotelItemXml(String userName,String passWord,String serchType,String serchCityValue)
    {
            StringBuffer xmlStream =new StringBuffer("");
            xmlStream.append("<?xml version=/"1.0/" encoding=/"GB2312/"?>");
            xmlStream.append("<Request>");
            xmlStream.append("<Source>");
            xmlStream.append("<RequestorID OfficeID=/"").append(userName).append("/" ").append("Passport=/"").append(passWord).append("/"/>");
            xmlStream.append("<RequestorPreferences Language=/"zh/">");
            xmlStream.append("<RequestMode>SYNCHRONOUS</RequestMode>");
            xmlStream.append("</RequestorPreferences></Source><RequestDetails>");
            xmlStream.append("<SearchItemRequest ItemType=/"hotel/">");
            xmlStream.append("<ItemDestination DestinationType=/"").append(serchType).append("/" ").append("DestinationCode=/"").append(serchCityValue).append("/"/>");
            xmlStream.append("</SearchItemRequest></RequestDetails></Request>");
            return xmlStream.toString();
    }
  
    //解析酒店查询列表返回的XML
     public static List<EtsHotelInfo> analysisHotelItem(String content)
     {

        StringReader sr = new StringReader(content);
        List<EtsHotelInfo> result = new ArrayList<EtsHotelInfo>();

        try {
                SAXReader reader = new SAXReader();
             Document doc = reader.read(sr);
             List<Node> list = doc.selectNodes("//ItemDetail");
            
              
                for (Node node : list) 
                    { 
                        String city = node.valueOf("City"); 
                        String hotel = node.valueOf("Item");
                        String cityCode=((Element) node).element("City").attribute("Code").getText();
                        String hotelCode=((Element) node).element("Item").attribute("Code").getText();
                       
                        log.debug(city+" "+cityCode+"~~"+hotel+" "+hotelCode); 
                        EtsHotelInfo temp= new  EtsHotelInfo();
                        temp.setCityCode(cityCode);
                        temp.setCityName(city);
                        temp.setHotelCode(hotelCode);
                        temp.setHotelName(hotel);
                        result.add(temp);
                      } 
               
              
           }catch (Exception e)
           {
               e.printStackTrace();
               log.debug(e.toString());
           }
              return result;
}

   
   
   
   
   
    //查询酒店详细信息
        public static String createSearchHotelDetailXml(String userName,String passWord,String serchType,String serchCityValue,String hotelCode)
        {
                StringBuffer xmlStream =new StringBuffer("");
                xmlStream.append("<?xml version=/"1.0/" encoding=/"GB2312/"?>");
                xmlStream.append("<Request>");
                xmlStream.append("<Source>");
                xmlStream.append("<RequestorID OfficeID=/"").append(userName).append("/" ").append("Passport=/"").append(passWord).append("/"/>");
                xmlStream.append("<RequestorPreferences Language=/"zh/">");
                xmlStream.append("<RequestMode>SYNCHRONOUS</RequestMode>");
                xmlStream.append("</RequestorPreferences></Source><RequestDetails>");
                xmlStream.append("<SearchItemRequest ItemType=/"hotel/">");
                xmlStream.append("<ItemDestination DestinationType=/"").append(serchType).append("/" ").append("DestinationCode=/"").append(serchCityValue).append("/"/>");
                xmlStream.append("<ItemCode>").append(hotelCode).append("</ItemCode>");
                xmlStream.append("</SearchItemRequest></RequestDetails></Request>");
                return xmlStream.toString();
        }
   
   
         public static Long avoidNull(String arg)
         {
             if(arg==null||"".equals(arg))
                  return new Long(0);
             return new Long(arg);
         }
   
        //解析酒店查询返回详细的酒店信息
         public static EtsHotelInfo analysisHotelDetail(String content)
         {

            StringReader sr = new StringReader(content);
            EtsHotelInfo result = new EtsHotelInfo();
            StringBuffer roomFacility =new StringBuffer();
            StringBuffer restaurantFacility = new StringBuffer();
            StringBuffer serviceFacility = new StringBuffer();
            StringBuffer entertainmentFacility = new StringBuffer();
            StringBuffer creditcard =new StringBuffer();
            StringBuffer trafficInfor =new StringBuffer();
           
           
            try {
                 SAXReader reader = new SAXReader();
                 Document doc = reader.read(sr);
               
                
                 Node cityName = doc.selectSingleNode( "//City" );
                 Node hotelName =doc.selectSingleNode( "//ItemDetail/Item" );
                 Node hotelAdress =doc.selectSingleNode("//Address");
                 Node hotelStarRating =doc.selectSingleNode("//StarRating");
                 Node Tel =doc.selectSingleNode("//Tel");
                 Node Fax =doc.selectSingleNode("//Fax");
                 Node Floors =doc.selectSingleNode("//Floors");
                 Node OpenDate =doc.selectSingleNode("//OpenDate");
                 Node introduction =doc.selectSingleNode("//Reports/Report[@Type='introduction']");
                 List<Node> traffic = doc.selectNodes("//Reports/Report[@Type='traffic']/Item");
                  for (Node node : traffic) 
                  { 
                       String StartArea = node.valueOf("StartArea"); 
                     String StartName = node.valueOf("StartName");
                     String Distance = node.valueOf("Distance"); 
                     String DistanceCode=((Element) node).element("Distance").attribute("Unit").getText();
                     String HowToArrival = node.valueOf("HowToArrival");
                     trafficInfor.append(StartArea+StartName+Distance.trim()+DistanceCode+HowToArrival);
                  }
//                 List<Node> restaurant = doc.selectNodes("//Facilities/Facility[@Type='restaurant']/Item");
//                 for (Node node : restaurant) 
//                 { 
//                     restaurantFacility.append(node.getText().trim()+",");
//                 }  
                 List<Node> room = doc.selectNodes("//Facilities/Facility[@Type='room']/Item");
                 for (Node node : room) 
                 { 
                     roomFacility.append(node.getText().trim()+",");
                 }  
                 List<Node> entertainment = doc.selectNodes("//Facilities/Facility[@Type='entertainment']/Item");
                 for (Node node : entertainment) 
                 { 
                     entertainmentFacility.append(node.getText().trim()+",");
                 }  
                 List<Node> service = doc.selectNodes("//Facilities/Facility[@Type='service']/Item");
                 for (Node node : service) 
                 { 
                     serviceFacility.append(node.getText().trim()+",");
                 }  
                 List<Node> Creditcard = doc.selectNodes("//AcceptableCreditcards/Creditcard");
                 for (Node node : Creditcard) 
                 { 
                     creditcard.append(node.getText().trim()+",");
                 }
                
                 result.setCityName(cityName.getText());
                 result.setHotelName(hotelName.getText());
                 result.setAddress(hotelAdress.getText());
                 result.setStars(hotelStarRating.getText());
                 result.setTel1(Tel.getText());
                 result.setFax(Fax.getText());
                 result.setFloors( avoidNull(Floors.getText()));
                 result.setShortBrief(introduction.getText());
                 result.setTrafficGuide(trafficInfor.toString().substring(0,trafficInfor.toString().length()-1));
                 result.setRoomService(roomFacility.toString().substring(0, roomFacility.toString().length()-1));
                 result.setServiceItem(serviceFacility.toString().substring(0, serviceFacility.toString().length()-1));
                 result.setFunFitness(entertainmentFacility.toString().substring(0,entertainmentFacility.toString().length()-1));
                 result.setCreditCardTypeName(creditcard.toString().substring(0,creditcard.toString().length()-1));
               }catch (Exception e)
               {
                   e.printStackTrace();
                   log.debug(e.toString());
               }
                  return result;
    }
   
   
   
   
   
    //得到链接
    public static String getConnection(String url,String content)
    {
        PrintWriter out = null;
        InputStream input = null;
        StringBuffer  retCode=new StringBuffer() ;
        try {
           
            URL server = null;
            server = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) server
                    .openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-type", "text/xml");
            connection.setDoOutput(true);
            out = new PrintWriter(connection.getOutputStream());
            out.print(content);
            out.flush();
            out.close();
            out = null;

            input = connection.getInputStream();
            BufferedReader bf = new BufferedReader(new InputStreamReader(input));
            String temp;
            while((temp=bf.readLine())!=null)
            {
             retCode.append(temp);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            closeUrl(input, out);
        }

        return retCode.toString();
  
    }
    private static void closeUrl(InputStream input, Writer output) {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (output != null) {
            try {
                output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值