Skip to content

Instantly share code, notes, and snippets.

@cbmeeks
Created April 6, 2011 12:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cbmeeks/905550 to your computer and use it in GitHub Desktop.
Save cbmeeks/905550 to your computer and use it in GitHub Desktop.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package getdata;
import java.math.BigDecimal;
import java.util.*;
/**
*
* @author cbmeeks
*/
public class Hotel {
private String hotelID;
private String hotelFileName;
private String hotelName;
private String cityID;
private String cityFileName;
private String cityName;
private String countryCode;
private String countryFileName;
private String countryName;
private Integer starRating;
private BigDecimal latitude;
private BigDecimal longitude;
private Integer popularity;
private String address;
private String currencyCode;
private Float lowRate;
private String facilities;
private Integer numberOfReviews;
private Float overallRating;
private Float cleanlinessRating;
private Float serviceRating;
private Float facilitiesRating;
private Float locationRating;
private Float diningRating;
private Float roomsRating;
private Integer propertyType;
private Integer chainID;
private Integer checkin;
private Integer checkout;
private Set<String> images;
private List<Description> description;
public Hotel() {
images = new LinkedHashSet<String>();
description = new ArrayList<Description>();
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getChainID() {
return chainID;
}
public void setChainID(Integer chainID) {
this.chainID = chainID;
}
public Integer getCheckin() {
return checkin;
}
public void setCheckin(Integer checkin) {
this.checkin = checkin;
}
public Integer getCheckout() {
return checkout;
}
public void setCheckout(Integer checkout) {
this.checkout = checkout;
}
public String getCityFileName() {
return cityFileName;
}
public void setCityFileName(String cityFileName) {
this.cityFileName = cityFileName;
}
public String getCityID() {
return cityID;
}
public void setCityID(String cityID) {
this.cityID = cityID;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public Float getCleanlinessRating() {
return cleanlinessRating;
}
public void setCleanlinessRating(Float cleanlinessRating) {
this.cleanlinessRating = cleanlinessRating;
}
public String getCountryCode() {
return countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
public String getCountryFileName() {
return countryFileName;
}
public void setCountryFileName(String countryFileName) {
this.countryFileName = countryFileName;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
public String getCurrencyCode() {
return currencyCode;
}
public void setCurrencyCode(String currencyCode) {
this.currencyCode = currencyCode;
}
public Float getDiningRating() {
return diningRating;
}
public void setDiningRating(Float diningRating) {
this.diningRating = diningRating;
}
public String getFacilities() {
return facilities;
}
public void setFacilities(String facilities) {
this.facilities = facilities;
}
public Float getFacilitiesRating() {
return facilitiesRating;
}
public void setFacilitiesRating(Float facilitiesRating) {
this.facilitiesRating = facilitiesRating;
}
public String getHotelFileName() {
return hotelFileName;
}
public void setHotelFileName(String hotelFileName) {
this.hotelFileName = hotelFileName;
}
public String getHotelID() {
return hotelID;
}
public void setHotelID(String hotelID) {
this.hotelID = hotelID;
}
public String getHotelName() {
return hotelName;
}
public void setHotelName(String hotelName) {
this.hotelName = hotelName;
}
public BigDecimal getLatitude() {
return latitude;
}
public void setLatitude(BigDecimal latitude) {
this.latitude = latitude;
}
public Float getLocationRating() {
return locationRating;
}
public void setLocationRating(Float locationRating) {
this.locationRating = locationRating;
}
public BigDecimal getLongitude() {
return longitude;
}
public void setLongitude(BigDecimal longitude) {
this.longitude = longitude;
}
public Float getLowRate() {
return lowRate;
}
public void setLowRate(Float lowRate) {
this.lowRate = lowRate;
}
public Integer getNumberOfReviews() {
return numberOfReviews;
}
public void setNumberOfReviews(Integer numberOfReviews) {
this.numberOfReviews = numberOfReviews;
}
public Float getOverallRating() {
return overallRating;
}
public void setOverallRating(Float overallRating) {
this.overallRating = overallRating;
}
public Integer getPopularity() {
return popularity;
}
public void setPopularity(Integer popularity) {
this.popularity = popularity;
}
public Integer getPropertyType() {
return propertyType;
}
public void setPropertyType(Integer propertyType) {
this.propertyType = propertyType;
}
public Float getRoomsRating() {
return roomsRating;
}
public void setRoomsRating(Float roomsRating) {
this.roomsRating = roomsRating;
}
public Float getServiceRating() {
return serviceRating;
}
public void setServiceRating(Float serviceRating) {
this.serviceRating = serviceRating;
}
public Integer getStarRating() {
return starRating;
}
public void setStarRating(Integer starRating) {
this.starRating = starRating;
}
public Set<String> getImages() {
return images;
}
public void setImages(Set<String> images) {
this.images = images;
}
public List<Description> getDescription() {
return description;
}
public void setDescription(List<Description> description) {
this.description = description;
}
}
public final class Main {
public Main() {
String folder;
XMLParser xmlparser = new XMLParser();
for (int i = 0; i < 99; i++) {
if (i < 10) {
folder = "C:\\temp\\HotelDatabase_EN\\0" + i + "\\";
} else {
folder = "C:\\temp\\HotelDatabase_EN\\" + i + "\\";
}
xmlparser.ParseFolder(folder);
}
}
public static void main(String[] args) {
new Main();
}
<?xml version="1.0" encoding="utf-8"?>
<Hotel>
<HotelID>1041000</HotelID>
<HotelFileName>La_Palapa_Hotel_Acapulco</HotelFileName>
<HotelName>La Palapa Hotel Acapulco</HotelName>
<CityID>17</CityID>
<CityFileName>Acapulco</CityFileName>
<CityName>Acapulco</CityName>
<CountryCode>MX</CountryCode>
<CountryFileName>Mexico</CountryFileName>
<CountryName>Mexico</CountryName>
<StarRating>4</StarRating>
<Latitude>16.845658000000</Latitude>
<Longitude>-99.850806000000</Longitude>
<Popularity>46</Popularity>
<Address>Avenida Fragata Yucatan No. 210</Address>
<CurrencyCode>MXN</CurrencyCode>
<LowRate>901.9368</LowRate>
<Facilities>3|7|20|21|31|32|41|70|71|211</Facilities>
<NumberOfReviews>4</NumberOfReviews>
<OverallRating>2.9</OverallRating>
<CleanlinessRating>3.5</CleanlinessRating>
<ServiceRating>2.2</ServiceRating>
<FacilitiesRating>3.2</FacilitiesRating>
<LocationRating>2.9</LocationRating>
<DiningRating>2.5</DiningRating>
<RoomsRating>3</RoomsRating>
<PropertyType>0</PropertyType>
<ChainID>0</ChainID>
<Checkin>14</Checkin>
<Checkout>11</Checkout>
<Images>
<Image>40520526</Image>
<Image>40520527</Image>
<Image>40520528</Image>
<Image>40520529</Image>
<Image>40520530</Image>
</Images>
<Descriptions>
<Description>
<Name>General Description</Name>
<Value>Guests will find that many area attractions are just minutes from the La Palapa Hotel Acapulco. The convention center is close by for guest convenience. In addition, a selection of golf courses, shopping centers, and dining establishments are nearby. Guests of the La Palapa Hotel Acapulco can make use of a shuttle service during their stay. Many attractions are just minutes away from the hotel, and guests arriving by private vehicle can make use of an on-site parking garage.
Guests of the La Palapa Hotel Acapulco will have a choice between one hundred forty four standard rooms and twenty-six junior suites. Each room includes such amenities as air-conditioning, cable television, telephones, Internet access, and private bathrooms with showers.
The La Palapa Hotel Acapulco includes such amenities as a restaurant, a bar, a swimming pool, a selection of shops, and a children’s club. The hotel also offers a banquet facility, a currency exchange service, on-call medical services, childcare, a tour desk, and laundry services. Guests can also make use of a hotel safe, a complimentary breakfast buffet, and elevator access to guestrooms.</Value>
</Description>
</Descriptions>
</Hotel>
/*
*/
package getdata;
import com.utils.Common;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.*;
import java.math.*;
/**
*
* @author cbmeeks
*/
public class XMLParser {
private String xmlFileName = "";
private Hotel hotel;
private Element element;
public XMLParser() {
this.hotel = new Hotel();
this.element = null;
}
public XMLParser(String filename) {
this.xmlFileName = filename;
this.hotel = new Hotel();
this.element = null;
}
public Hotel getHotel() {
return this.hotel;
}
public void ParseFolder(String folder) {
try {
File dir = new File(folder);
String[] files = dir.list();
Common.PrintLn("Parsing folder: " + folder);
if (files != null) {
for (int i = 0; i < files.length; i++) {
ParseAndSave(folder + files[i]);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
/*
* Parse a single XML file into a Hotel object
*/
public void ParseAndSave() {
ParseAndSave(xmlFileName);
}
public void ParseAndSave(String filename) {
File file;
DocumentBuilderFactory dbFactory;
DocumentBuilder builder;
Document doc;
try {
// Load file
file = new File(filename);
dbFactory = DocumentBuilderFactory.newInstance();
builder = dbFactory.newDocumentBuilder();
doc = builder.parse(file);
doc.getDocumentElement().normalize();
// Get Hotel node
NodeList nlHotel = doc.getElementsByTagName("Hotel");
// Parse children
for (int i = 0; i < nlHotel.getLength(); i++) { // this should only iterate once because there is only one Hotel node
Node node = nlHotel.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
element = (Element) node;
hotel.setHotelID(getTextFromTag("HotelID"));
hotel.setHotelFileName(getTextFromTag("HotelFileName"));
hotel.setHotelName(getTextFromTag("HotelName"));
hotel.setCityID(getTextFromTag("CityID"));
hotel.setCityFileName(getTextFromTag("CityFileName"));
hotel.setCityName(getTextFromTag("CityName"));
hotel.setCountryCode(getTextFromTag("CountryCode"));
hotel.setCountryFileName(getTextFromTag("CountryFileName"));
hotel.setCountryName(getTextFromTag("CountryName"));
hotel.setStarRating(getIntegerFromTag("StarRating"));
hotel.setLatitude(getBigDecimalFromTag("Latitude"));
hotel.setLongitude(getBigDecimalFromTag("Longitude"));
hotel.setPopularity(getIntegerFromTag("Popularity"));
hotel.setAddress(getTextFromTag("Address"));
hotel.setCurrencyCode(getTextFromTag("CurrencyCode"));
hotel.setLowRate(getFloatFromTag("LowRate"));
hotel.setFacilities(getTextFromTag("Facilities"));
hotel.setNumberOfReviews(getIntegerFromTag("NumberOfReviews"));
hotel.setOverallRating(getFloatFromTag("OverallRating"));
hotel.setCleanlinessRating(getFloatFromTag("CleanlinessRating"));
hotel.setServiceRating(getFloatFromTag("ServiceRating"));
hotel.setFacilitiesRating(getFloatFromTag("FacilitiesRating"));
hotel.setLocationRating(getFloatFromTag("LocationRating"));
hotel.setDiningRating(getFloatFromTag("DiningRating"));
hotel.setRoomsRating(getFloatFromTag("RoomsRating"));
hotel.setPropertyType(getIntegerFromTag("PropertyType"));
hotel.setChainID(getIntegerFromTag("ChainID"));
hotel.setCheckin(getIntegerFromTag("Checkin"));
hotel.setCheckout(getIntegerFromTag("Checkout"));
// Get images
NodeList nlImages = element.getElementsByTagName("Images");
for (int c = 0; c < nlImages.getLength(); c++) {
Node nImage = nlImages.item(c);
if (nImage.getNodeType() == Node.ELEMENT_NODE) {
Element image = (Element) nImage;
hotel.getImages().add(image.getTextContent());
}
}
// Get descriptions
NodeList nlDesc = element.getElementsByTagName("Description");
for (int c = 0; c < nlDesc.getLength(); c++) {
Node cNode = nlDesc.item(c);
Element cElement = (Element) cNode;
if (cElement.getNodeType() == Node.ELEMENT_NODE) {
NodeList nlDetails = cElement.getElementsByTagName("Name");
Element name = (Element) nlDetails.item(0);
nlDetails = cElement.getElementsByTagName("Value");
Element value = (Element) nlDetails.item(0);
Description desc = new Description(name.getTextContent(), value.getTextContent());
if (desc != null) {
hotel.getDescription().add(desc);
}
}
}
}
}
// Persist
AddHotelToMongo();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private String getTextFromTag(String tagName) {
NodeList nl = element.getElementsByTagName(tagName);
Element e = (Element) nl.item(0);
return Common.getString(e.getTextContent());
}
private Integer getIntegerFromTag(String tagName) {
NodeList nl = element.getElementsByTagName(tagName);
Element e = (Element) nl.item(0);
return Common.getInteger(e.getTextContent());
}
private Float getFloatFromTag(String tagName) {
NodeList nl = element.getElementsByTagName(tagName);
Element e = (Element) nl.item(0);
return Common.getFloat(e.getTextContent());
}
private BigDecimal getBigDecimalFromTag(String tagName) {
NodeList nl = element.getElementsByTagName(tagName);
Element e = (Element) nl.item(0);
return Common.getBigDecimal(e.getTextContent());
}
private void AddHotelToMongo() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment