Example usage for org.dom4j.io SAXReader SAXReader

List of usage examples for org.dom4j.io SAXReader SAXReader

Introduction

In this page you can find the example usage for org.dom4j.io SAXReader SAXReader.

Prototype

public SAXReader() 

Source Link

Usage

From source file:cms.service.template.TemplateXml.java

public boolean parseXml(String xml) {

    List<Element> records = new ArrayList<Element>();
    dbobject = "";
    strBulksql = "";
    childidlist = "";
    if (ApplicationConstants.GENERATE_LOG) {
        logger.info("XML Data=" + xml);
    }/*from  w  w  w . j  ava  2  s  .c o m*/
    try {
        doc = new SAXReader().read(new StringReader(xml));
        records.addAll(doc.getRootElement().elements());
        setInput(xml);

        for (Element record : records) {
            objid = "";
            parseRecord(record);
        }

        //logger.info("after Calling makesql()");
        if (!isparent && colcount > 0)
            strBulksql += "\n\t\tupdate Table_" + dbobject + " set " + getRelation() + "="
                    + (dbtype.equalsIgnoreCase("Oracle") ? "parentid" : "@parentid") + " where objid in("
                    + childidlist + (getDbType().equalsIgnoreCase("oracle") ? ");\n" : ")\n");

    } catch (DocumentException e) {
        String error = ">>>Exception:<<<" + this.getClass().getName() + ">>> Failed in reading XML=" + xml;
        logger.info(e.getMessage());
        logger.info(error);
    } catch (Exception e) {
        e.printStackTrace(System.err);
        return (false);
    }
    return (true);
}

From source file:cms.service.util.ItemUtility.java

License:Open Source License

public Element replaceVariable(Element elm, String var, String value) {
    Element elmNew = null;/* w w  w .  ja  va  2  s  .c  om*/

    try {
        String xml = elm.asXML().replaceAll(var, value);
        elmNew = new SAXReader().read(new StringReader(xml)).getRootElement();
    } catch (DocumentException e) {
        logger.info(">>>Exception:<<<" + this.getClass().getName() + ">>> Failed to replace parameter " + var
                + " with value=" + value);
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        logger.info(">>>Exception:<<<" + this.getClass().getName() + ">>> Failed to replace parameter " + var
                + " with value=" + value + "for xml=" + elm.asXML());
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return (elmNew);
}

From source file:cms.service.util.ItemUtility.java

License:Open Source License

public Element replaceAttributeValue(Element elm, String var, String value) {
    Element elmNew = null;/*from  w  w  w. j a v  a  2 s  .  c om*/

    try {
        String xml = elm.asXML().replaceAll(var, value);
        elmNew = new SAXReader().read(new StringReader(xml)).getRootElement();
    } catch (DocumentException e) {
        logger.info(">>>Exception:<<<" + this.getClass().getName() + ">>> Failed to replace parameter " + var
                + " with value=" + value);
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        logger.info(">>>Exception:<<<" + this.getClass().getName() + ">>> Failed to replace parameter " + var
                + " with value=" + value + "for xml=" + elm.asXML());
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return (elmNew);
}

From source file:cms.service.util.ItemUtility.java

License:Open Source License

public Element replaceAllGlobalData(Element elm, String key, String value) {
    Element elmNew = null;/*  w ww . ja  va  2 s .  c o  m*/
    String xml = elm.asXML().replaceAll("@" + key, value);
    //System.out.println(xml);
    try {
        elmNew = new SAXReader().read(new StringReader(xml)).getRootElement();
    } catch (DocumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return (elmNew);
}

From source file:cms.service.util.ItemUtility.java

License:Open Source License

public Document getDocument(String xml) {

    Document doc = null;/*from www  .  j  a v a2  s .  co  m*/

    try {
        doc = new SAXReader().read(new StringReader(xml));

    } catch (DocumentException e) {
        logger.info(">>>Exception:<<<" + this.getClass().getName()
                + ">>> Failed in reading XML Input data to the Webservice ");
    }

    return doc;
}

From source file:cms.service.util.ItemUtility.java

License:Open Source License

public Element getRootElementFromXML(String xml) {
    Element elmNew = null;//from w w  w .j a v a2 s . co m

    try {
        elmNew = new SAXReader().read(new StringReader(xml)).getRootElement();
    } catch (DocumentException e) {
        logger.info(">>>Exception:<<<" + this.getClass().getName()
                + ">>> Failed in reading XML Input data to the Webservice ");
        logger.info("XML=" + xml);
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return (elmNew);
}

From source file:cn.buk.api.service.CtripHotelServiceImpl.java

License:LGPL

@Override
public String importCityInfo(String filename) {
    SAXReader reader = new SAXReader();

    File file = new File(filename);
    if (!file.exists())
        return "not exist.";
    Document document;// ?XML
    try {/* w  ww .j  av a  2s.  com*/
        document = reader.read(file);
    } catch (DocumentException e) {
        e.printStackTrace();
        return "error";
    }

    List flightDataList = document.selectNodes("//CityDetails/*");
    List<City> cities = new ArrayList<City>();
    String temp;
    for (Iterator it = flightDataList.iterator(); it.hasNext();) {
        Element fltElement = (Element) it.next();

        City city = new City();
        cities.add(city);

        city.setCityCode(fltElement.element("CityCode").getText());
        city.setOpenApiId(Integer.parseInt(fltElement.element("City").getText()));
        city.setCityName(fltElement.element("CityName").getText());
        city.setCityEnglishName(fltElement.elementText("CityEName").trim());
        temp = fltElement.elementText("Country").trim();
        if (temp.equalsIgnoreCase("1"))
            city.setCountryCode("CN");
        else
            city.setCountryCode(temp);
        city.setProvinceId(Integer.parseInt(fltElement.elementText("Province")));
        city.setAirport(fltElement.element("Airport").getText());
    }

    int count = 0;
    for (City city : cities) {
        if (city.getCityCode() == null || city.getCityCode().trim().length() == 0)
            continue;
        if (cityDao.create(city) == 1)
            count++;
    }

    return "create " + count + " record(s).";
}

From source file:cn.buk.api.service.CtripHotelServiceImpl.java

License:LGPL

@Override
public String importDistrictInfo(String filename) {
    SAXReader reader = new SAXReader();

    File file = new File(filename);
    if (!file.exists())
        return "not exist.";
    Document document;// ?XML
    try {//w ww.j  a  va2s.com
        document = reader.read(file);
    } catch (DocumentException e) {
        e.printStackTrace();
        return "error";
    }

    List list = document.selectNodes("//LocationDetails/*");
    List<District> districts = new ArrayList<District>();
    for (Iterator it = list.iterator(); it.hasNext();) {
        Element element = (Element) it.next();

        District district = new District();
        districts.add(district);

        district.setName(element.element("LocationName").getText());
        district.setEnglishName(element.element("LocationEName").getText());
        district.setDistrictId(Integer.parseInt(element.element("Location").getText()));
        district.setCityId(Integer.parseInt(element.element("LocationCity").getText()));
    }

    int count = 0;
    for (District district : districts) {
        if (hotelDao.createDistrict(district) == 1)
            count++;
    }

    return "create " + count + " record(s).";
}

From source file:cn.buk.api.service.CtripHotelServiceImpl.java

License:LGPL

@Override
public String importZoneInfo(String filename) {
    SAXReader reader = new SAXReader();

    File file = new File(filename);
    if (!file.exists())
        return "not exist.";
    Document document;// ?XML
    try {/*from w  w w  .  j a v  a 2  s .  c om*/
        document = reader.read(file);
    } catch (DocumentException e) {
        e.printStackTrace();
        return "error";
    }

    List list = document.selectNodes("//ZoneDetails/*");
    List<Zone> zones = new ArrayList<Zone>();
    int maxLength = 0;
    for (Iterator it = list.iterator(); it.hasNext();) {
        Element element = (Element) it.next();

        Zone zone = new Zone();
        zones.add(zone);

        zone.setZoneId(Integer.parseInt(element.element("Zone").getText()));
        zone.setCityId(Integer.parseInt(element.element("City").getText()));
        zone.setName(element.element("ZoneName").getText());
        zone.setEnglishName(element.element("ZoneEName").getText());
        zone.setDescription(element.element("ZoneDesc").getText());
        if (zone.getDescription().length() > maxLength)
            maxLength = zone.getDescription().length();
        zone.setMapUse(element.element("ZoneMapuse").getText());
        zone.setMapRange(element.element("ZoneRange").getText());
        zone.setMapPic(element.element("ZoneMapPic").getText());
    }

    int count = 0;
    for (Zone zone : zones) {
        if (hotelDao.createZone(zone) == 1)
            count++;
    }
    System.out.println(maxLength);

    return "create " + count + " record(s).";
}

From source file:cn.buk.api.service.CtripHotelServiceImpl.java

License:LGPL

/**
 * ??//from ww  w.  j  a  v  a  2s  .  com
 * @param cityId ?
 * @return ??
 */
@Override
public String searchHotel(int cityId) {
    if (cityId <= 0)
        return "ER#CITYID IS " + cityId;

    //headerAPI?
    Cache cache = getCache();
    String cacheKey = ConfigData.OTA_HotelSearch_Request;
    net.sf.ehcache.Element cacheElement = cache.get(cacheKey);
    if (cacheElement != null) {
        Element headerElement = (Element) cacheElement.getValue();

        int accessCount = Integer.parseInt(headerElement.attributeValue("AccessCount"));
        int currentCount = Integer.parseInt(headerElement.attributeValue("CurrentCount")) + 1;
        logger.info("AccessCount=" + headerElement.attributeValue("AccessCount") + ", CurrentCount="
                + headerElement.attributeValue("CurrentCount") + ", ResetTime="
                + headerElement.attributeValue("ResetTime"));
        if (currentCount >= accessCount) {
            try {
                logger.info("Sleep for one minute.");
                Thread.sleep(60000);
            } catch (InterruptedException ex) {
                logger.warn(Thread.currentThread().getName() + " is interrupted.");
                return "ER#Thread.sleep is interrupted.";
            }
        }
    }

    HotelRequestBody request = new HotelRequestBody();
    request.createHotelRequestRQ();
    request.getHotelRequestRQ().getCriteria().getCriterion().getHotelRef().setHotelCityCode(cityId);
    String xml;

    try {
        JAXBContext jc = JAXBContext.newInstance(HotelRequestBody.class);

        Marshaller m = jc.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.setProperty(Marshaller.JAXB_ENCODING, "utf-8");
        m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

        DocumentResult documentResult = new DocumentResult();
        m.marshal(request, documentResult);

        org.dom4j.Document document = documentResult.getDocument();
        org.dom4j.Element requestElement = document.getRootElement();
        Element ele = requestElement.element("OTA_HotelSearchRQ");
        ele.addNamespace("", "http://www.opentravel.org/OTA/2003/05");

        org.dom4j.Document doc1 = DocumentHelper.createDocument();

        org.dom4j.Element rootElement = createRequestHeaderElement(doc1, ConfigData.OTA_HotelSearch_Request);
        rootElement.add(requestElement);

        xml = doc1.asXML();
    } catch (JAXBException ex) {
        logger.error(ex.getMessage());
        return "ER";
    }

    if (serviceStopped)
        return "ER#Service stopped.";
    String response = execApiRequest(xml, ConfigData.OTA_HotelSearch_Url, "requestXML");
    //?
    SAXReader reader = new SAXReader();

    Document document;// ?XML
    try {
        document = reader.read(new StringReader(response));

        if (serviceStopped)
            return "ER#Service stopped.";
        response = processHotelSearchResponse(document);
    } catch (Exception ex) {
        logger.error(ex.getMessage());
        return "ER";
    }

    return response;
}