java.lang.OutOfMemoryError:Java堆空间错误,同时在IntelliJ IDEA中通过Stax解析器解析5mb数据

时间:2018-08-20 10:50:33

标签: java xml intellij-idea stax

我正在尝试使用Java中的StAx解析器解析xml的5mb。使用intellij的想法, 在研究中,他们要求我在自定义vm选项中增加堆大小,我将512m增加到2048m,但是它不起作用。让我知道还有其他解决方案...

以下代码正尝试使用StAx解析器进行解析 // StAX xml解析为HotelSearchResult

    public HotelSearchDto processRequestXmlparsing(String xml) throws ParserConfigurationException, IOException, SAXException, XMLStreamException {
        HotelSearchDto hotelSearchDto = new HotelSearchDto();
        ObjectMapper mapper = new ObjectMapper();
        ObjectNode objectNode = mapper.createObjectNode();
        String code = "";
        String message = null;
        try{
            long startTime = System.currentTimeMillis();
            List<SubsectionInformation> subInformationList = new ArrayList<>();
            byte[] byteArray = xml.getBytes("UTF-8");
            ByteArrayInputStream inputStream = new ByteArrayInputStream(byteArray);
            XMLInputFactory inputFactory = XMLInputFactory.newInstance();
            XMLEventReader eventReader = inputFactory.createXMLEventReader(inputStream);
            SearchCriteriaDto searchCriteriaDto = new SearchCriteriaDto();
            Hotel hotel = new Hotel();
            BaseInfo baseInfo = new BaseInfo();
            CommunicationInfo communicationInfo = new CommunicationInfo();
            HotelRating hotelRating = new HotelRating();
            HotelAmenity hotelAmenity = new HotelAmenity();
            DetailedAmenity detailedAmenity = new DetailedAmenity();
            List<DetailedAmenity> detailedAmenities = new ArrayList<DetailedAmenity>();
            Amenitie amenitie = new Amenitie();
            List<HotelAmenity> hotelAmenityList= new ArrayList<HotelAmenity>();
            List<Amenitie> amenitieList = new ArrayList<Amenitie>();
            List<Hotel> hotelList = new ArrayList<Hotel>();
            List<RoomRate> roomRates = new ArrayList<RoomRate>();
            RoomRate roomRate = new RoomRate();
            Offers offers = new Offers();
            List<RateBrakeDown> rateBrakeDownList = new ArrayList<RateBrakeDown>();
            RateBrakeDown rateBrakeDown = new RateBrakeDown();
            List<PricingElement> pricingElementList = new ArrayList<PricingElement>();
            PricingElement pricingElement = new PricingElement();
            RoomType roomType = new RoomType();
            PromoOffer promoOffer = new PromoOffer();

            while (eventReader.hasNext()) {
                XMLEvent event = eventReader.nextEvent();

                // If we have an item element, we create a new item
                if (event.isStartElement()) {
                   if (event.asStartElement().getName().getLocalPart()
                           .equals("booking-date")) {
                       searchCriteriaDto.setBookingDate(eventReader.nextEvent().toString());
                       continue;
                   }
                   if (event.asStartElement().getName().getLocalPart()
                           .equals("check-in-date")) {
                       searchCriteriaDto.setCheckInDate(eventReader.nextEvent().toString());
                       continue;
                   }
                   if (event.asStartElement().getName().getLocalPart()
                           .equals("check-out-date")) {
                       searchCriteriaDto.setCheckOutDate(eventReader.nextEvent().toString());
                       continue;
                   }
                   if (event.asStartElement().getName().getLocalPart()
                           .equals("number-of-rooms")) {
                       searchCriteriaDto.setNumberOfRooms(eventReader.nextEvent().toString());
                       continue;
                   }
                   if (event.asStartElement().getName().getLocalPart()
                           .equals("number-of-nights")) {
                       searchCriteriaDto.setNumberOfNights(eventReader.nextEvent().toString());
                       continue;
                   }
                   if (event.asStartElement().getName().getLocalPart()
                           .equals("number-of-room-nights")) {
                       searchCriteriaDto.setNumberOfRoomNights(eventReader.nextEvent().toString());
                       continue;
                   }
                   if (event.asStartElement().getName().getLocalPart()
                           .equals("city")) {
                       searchCriteriaDto.setCity(eventReader.nextEvent().toString());
                       continue;
                   }
                   if (event.asStartElement().getName().getLocalPart()
                           .equals("state")) {
                       searchCriteriaDto.setState(eventReader.nextEvent().toString());
                       continue;
                   }
                   if (event.asStartElement().getName().getLocalPart()
                           .equals("country")) {
                       searchCriteriaDto.setCountry(eventReader.nextEvent().toString());
                       hotelSearchDto.setSearchCriteriaDto(searchCriteriaDto);
                       continue;
                   }
                   if (event.asStartElement().getName().getLocalPart()
                           .equals("currency")) {
                       hotelSearchDto.setCurrency(eventReader.nextEvent().toString());
                       continue;
                   }
                   if (event.asStartElement().getName().getLocalPart()
                           .equals("base-url")) {
                       hotelSearchDto.setBaseUrl(eventReader.nextEvent().toString());
                       continue;
                   }

                   if (event.asStartElement().getName().getLocalPart()
                           .equals("hotel-id")) {
                       hotel.setHotelId(eventReader.nextEvent().toString());
                       continue;
                   }

                   if (event.asStartElement().getName().getLocalPart()
                           .equals("hotel-info-copyright")) {
                       baseInfo.setHotelInfoCopyright(eventReader.nextEvent().toString());
                       continue;
                   }
                   if (event.asStartElement().getName().getLocalPart()
                           .equals("hotel-name")) {
                       baseInfo.setHotelName(eventReader.nextEvent().toString());
                       continue;
                   }
                   if (event.asStartElement().getName().getLocalPart()
                           .equals("address")) {
                       baseInfo.setAddress(eventReader.nextEvent().toString());
                       continue;
                   }
                   if (event.asStartElement().getName().getLocalPart()
                           .equals("locality")) {
                       baseInfo.setLocality(eventReader.nextEvent().toString());
                       continue;
                   }
                   if (event.asStartElement().getName().getLocalPart()
                           .equalsIgnoreCase("locality-id")) {
                       baseInfo.setLocalityId(eventReader.nextEvent().toString());
                       continue;
                   }
                   if (event.asStartElement().getName().getLocalPart()
                           .equalsIgnoreCase("locality-latitude")) {
                       baseInfo.setLocalityLatitude(eventReader.nextEvent().toString());
                       continue;
                   }
                   if (event.asStartElement().getName().getLocalPart()
                           .equalsIgnoreCase("locality-longitude")) {
                       baseInfo.setLocalityLongitude(eventReader.nextEvent().toString());
                       continue;
                   }
                   if (event.asStartElement().getName().getLocalPart()
                           .equalsIgnoreCase("city")) {
                       baseInfo.setCity(eventReader.nextEvent().toString());
                       continue;
                   }
                   if (event.asStartElement().getName().getLocalPart()
                           .equalsIgnoreCase("state")) {
                       baseInfo.setState(eventReader.nextEvent().toString());
                       continue;
                   }
                   if (event.asStartElement().getName().getLocalPart()
                           .equalsIgnoreCase("state-code")) {
                       baseInfo.setStateCode(eventReader.nextEvent().toString());
                       continue;
                   }
                   if (event.asStartElement().getName().getLocalPart()
                           .equalsIgnoreCase("country")) {
                       baseInfo.setCountry(eventReader.nextEvent().toString());
                       continue;
                   }
                   if (event.asStartElement().getName().getLocalPart()
                           .equalsIgnoreCase("country-code")) {
                       baseInfo.setCountryCode(eventReader.nextEvent().toString());
                       continue;
                   }
                   if (event.asStartElement().getName().getLocalPart()
                           .equalsIgnoreCase("zip")) {
                       baseInfo.setZip(eventReader.nextEvent().toString());
                       continue;
                   }
                    if (event.asStartElement().getName().getLocalPart()
                            .equalsIgnoreCase("phone")) {
                        communicationInfo.setPhone(eventReader.nextEvent().toString());
                        continue;
                    }
                    if (event.asStartElement().getName().getLocalPart()
                            .equalsIgnoreCase("fax")) {
                        communicationInfo.setFax(eventReader.nextEvent().toString());
                        continue;
                    }
                    if (event.asStartElement().getName().getLocalPart()
                            .equalsIgnoreCase("email")) {
                        communicationInfo.setEmail(eventReader.nextEvent().toString());
                        continue;
                    }
                    if (event.asStartElement().getName().getLocalPart()
                            .equalsIgnoreCase("website")) {
                        communicationInfo.setWebsite(eventReader.nextEvent().toString());
                        continue;
                    }
                    if (event.asStartElement().getName().getLocalPart()
                            .equalsIgnoreCase("owner-name")) {
                        communicationInfo.setOwnerName(eventReader.nextEvent().toString());
                        baseInfo.setCommunicationInfo(communicationInfo);
                        continue;
                    }
                   if (event.asStartElement().getName().getLocalPart()
                           .equalsIgnoreCase("chain")) {
                       baseInfo.setChain(eventReader.nextEvent().toString());
                       continue;
                   }
                   if (event.asStartElement().getName().getLocalPart()
                           .equalsIgnoreCase("overview")) {
                       baseInfo.setOverview(eventReader.nextEvent().toString());
                       continue;
                   }
                   if (event.asStartElement().getName().getLocalPart()
                           .equalsIgnoreCase("thumb-nail-image")) {
                       baseInfo.setThumbNailImage(eventReader.nextEvent().toString());
                       continue;
                   }
                if (event.asStartElement().getName().getLocalPart()
                        .equals("category")) {
                    hotelAmenity.setCategory(eventReader.nextEvent().toString());
                    continue;
                }
                   if (event.asStartElement().getName().getLocalPart()
                           .equalsIgnoreCase("amenity")) {
                       String amenityVal = eventReader.nextEvent().toString();
                       String[] amentityArray = new String[]{amenityVal};
//                       amenitie.setAmenity(amentityArray);
                       continue;
                   }
                    if (event.asStartElement().getName().getLocalPart()
                            .equalsIgnoreCase("name")) {
                        detailedAmenity.setName(eventReader.nextEvent().toString());
                        continue;
                    }
                    if (event.asStartElement().getName().getLocalPart()
                            .equalsIgnoreCase("chargeable")) {
                        detailedAmenity.setChargeable(Boolean.parseBoolean(eventReader.nextEvent().toString()));
                        continue;
                    }
                    if (event.asStartElement().getName().getLocalPart()
                            .equalsIgnoreCase("price-info")) {
                        detailedAmenity.setPriceInfo(eventReader.nextEvent().toString());
                        continue;
                    }
                    if (event.asStartElement().getName().getLocalPart()
                            .equalsIgnoreCase("unit-info")) {
                        detailedAmenity.setUnitInfo(eventReader.nextEvent().toString());
                        continue;
                    }
                    if (event.asStartElement().getName().getLocalPart()
                            .equalsIgnoreCase("misc-info")) {
                        detailedAmenity.setMiscInfo(eventReader.nextEvent().toString());
                        detailedAmenities.add(detailedAmenity);
                        amenitie.setDetailedAmenityList(detailedAmenities);
                        hotelAmenity.setAmenities(amenitie);
                        hotelAmenityList.add(hotelAmenity);
                        baseInfo.setHotelAmenityList(hotelAmenityList);
                        continue;
                    }
                    if (event.asStartElement().getName().getLocalPart()
                            .equalsIgnoreCase("is-veg")) {
                        baseInfo.setIsVeg(eventReader.nextEvent().toString());
                        continue;
                    }
                   if (event.asStartElement().getName().getLocalPart()
                           .equalsIgnoreCase("is-on-hold")) {
                       baseInfo.setIsOnHold(eventReader.nextEvent().toString());
                       continue;
                   }
                if (event.asStartElement().getName().getLocalPart()
                        .equals("rating-type")) {
                    hotelRating.setRatingType(eventReader.nextEvent().toString());
                    continue;
                }
                    if (event.asStartElement().getName().getLocalPart()
                            .equals("rating")) {
                        hotelRating.setRating(eventReader.nextEvent().toString());
                        continue;
                    }
                    if (event.asStartElement().getName().getLocalPart()
                            .equals("total-ratings")) {
                        hotelRating.setTotalRating(eventReader.nextEvent().toString());
                        continue;
                    }
                   if (event.asStartElement().getName().getLocalPart()
                           .equalsIgnoreCase("star-rating")) {
                       baseInfo.setStarRating(eventReader.nextEvent().toString());
                       continue;
                   }
                   if (event.asStartElement().getName().getLocalPart()
                           .equalsIgnoreCase("ct-recommendation")) {
                       baseInfo.setCtRecommendation(eventReader.nextEvent().toString());
                       continue;
                   }
                    if (event.asStartElement().getName().getLocalPart()
                            .equalsIgnoreCase("hotel-usp")) {
                        baseInfo.setHotelUsp(eventReader.nextEvent().toString());
                        continue;
                    }
                    if (event.asStartElement().getName().getLocalPart()
                            .equalsIgnoreCase("notice")) {
                        baseInfo.setNotice(eventReader.nextEvent().toString());
                        continue;
                    }
                    if (event.asStartElement().getName().getLocalPart()
                            .equalsIgnoreCase("ct-recommended")) {
                        baseInfo.setCtRecommended(Boolean.parseBoolean(eventReader.nextEvent().toString()));
                        continue;
                    }
                   if (event.asStartElement().getName().getLocalPart()
                           .equalsIgnoreCase("getaway-property")) {
                       baseInfo.setGetawayProperty(Boolean.parseBoolean(eventReader.nextEvent().toString()));
                       continue;
                   }
                   if (event.asStartElement().getName().getLocalPart()
                           .equalsIgnoreCase("lth-hotel")) {
                       baseInfo.setLthHotel(Boolean.parseBoolean(eventReader.nextEvent().toString()));
                       continue;
                   }
                   if (event.asStartElement().getName().getLocalPart()
                           .equalsIgnoreCase("faqs")) {
                       baseInfo.setFaqs(Boolean.parseBoolean(eventReader.nextEvent().toString()));
                       continue;
                   }
                   if (event.asStartElement().getName().getLocalPart()
                           .equalsIgnoreCase("restrictions")) {
                       baseInfo.setRestrictions(eventReader.nextEvent().toString());
                       continue;
                   }
                   if (event.asStartElement().getName().getLocalPart()
                           .equalsIgnoreCase("hotel-activities")) {
                       baseInfo.setRestrictions(eventReader.nextEvent().toString());
                       continue;
                   }
                   if (event.asStartElement().getName().getLocalPart()
                           .equalsIgnoreCase("view-360")) {
                       baseInfo.setView360(eventReader.nextEvent().toString());
                       continue;
                   }
                   if (event.asStartElement().getName().getLocalPart()
                           .equalsIgnoreCase("supplier-360")) {
                       baseInfo.setSupplier360(eventReader.nextEvent().toString());
                       continue;
                   }
                   if (event.asStartElement().getName().getLocalPart()
                           .equalsIgnoreCase("gstin_enabled")) {
                       baseInfo.setGstin_enabled(eventReader.nextEvent().toString());
                       continue;
                   }
                   if (event.asStartElement().getName().getLocalPart()
                            .equalsIgnoreCase("gstin")) {
                        baseInfo.setGstin(eventReader.nextEvent().toString());
                        continue;
                    }
                    if (event.asStartElement().getName().getLocalPart()
                            .equalsIgnoreCase("tds_enabled")) {
                        baseInfo.setTds_enabled(eventReader.nextEvent().toString());
                        continue;
                    }
                   if (event.asStartElement().getName().getLocalPart()
                           .equalsIgnoreCase("tds_rate")) {
                       baseInfo.setTds_rate(eventReader.nextEvent().toString());
                       hotel.setBaseInfo(baseInfo);
                       continue;
                   }
                    if (event.asStartElement().getName().getLocalPart()
                            .equalsIgnoreCase("amount")) {
                        pricingElement.setAmount(eventReader.nextEvent().toString());
                        continue;
                    }
                    if (event.asStartElement().getName().getLocalPart()
                            .equalsIgnoreCase("code")) {
                        pricingElement.setCode(eventReader.nextEvent().toString());
                        pricingElementList.add(pricingElement);
                        rateBrakeDown.setPricingElement(pricingElementList);
                        rateBrakeDownList.add(rateBrakeDown);
                        roomRate.setRateBrakeDown(rateBrakeDownList);
                        continue;
                    }
                    if (event.asStartElement().getName().getLocalPart()
                            .equalsIgnoreCase("base-rate-id")) {
                        roomRate.setBaseRateId(Long.parseLong(eventReader.nextEvent().toString()));
                        continue;
                    }
                    if (event.asStartElement().getName().getLocalPart()
                            .equalsIgnoreCase("is-special-rate")) {
                        roomRate.setSpecialRate(Boolean.parseBoolean(eventReader.nextEvent().toString()));
                        continue;
                    }
                    if (event.asStartElement().getName().getLocalPart()
                            .equalsIgnoreCase("deal-of-the-day")) {
                        roomRate.setDealOfTheDay(Boolean.parseBoolean(eventReader.nextEvent().toString()));
                        continue;
                    }
                    if (event.asStartElement().getName().getLocalPart()
                            .equalsIgnoreCase("opaque")) {
                        roomRate.setOpaque(Boolean.parseBoolean(eventReader.nextEvent().toString()));
                        continue;
                    }
                    if (event.asStartElement().getName().getLocalPart()
                            .equalsIgnoreCase("max-adults")) {
                        roomRate.setMaxAdults(Long.parseLong(eventReader.nextEvent().toString()));
                        continue;
                    }
                    if (event.asStartElement().getName().getLocalPart()
                            .equalsIgnoreCase("max-children")) {
                        roomRate.setMaxChildren(Long.parseLong(eventReader.nextEvent().toString()));
                        continue;
                    }
                    if (event.asStartElement().getName().getLocalPart()
                            .equalsIgnoreCase("max-gst-slab")) {
                        roomRate.setMaxGstSlab(eventReader.nextEvent().toString());
                        roomRates.add(roomRate);
                        hotel.setRoomRates(roomRates);
                        hotelList.add(hotel);
                        hotelSearchDto.setHotelList(hotelList);
                        System.out.println(hotelSearchDto.getHotelList().size());
                        continue;
                    }

               }
            }
            //Creating json object for xmlparsing elements
            if(code.equalsIgnoreCase("")) {
                Gson gson = new Gson();
                objectNode.put("HotelSearchDto", gson.toJson(hotelSearchDto));
            }else{
                objectNode.put("code", code);
                objectNode.put("message", message);
            }
            Long endtime = System.currentTimeMillis() - startTime;
            System.out.println(endtime);
        }catch(Exception e){
            e.printStackTrace();
        }
        return hotelSearchDto;
    }

1 个答案:

答案 0 :(得分:0)

IntelliJ,位置是File |。设置|构建,执行,部署|编译器构建进程堆大小。