Beispiel #1
0
  /**
   * 查询商品明细
   *
   * @param xml
   * @return
   * @throws DocumentException
   */
  public static Item getItem(String xml) throws Exception {
    Item item = new Item();
    Document document = formatStr2Doc(xml);
    Element rootElt = document.getRootElement();
    Element element = rootElt.element("Item");
    item.setTitle(element.elementText("Title"));
    item.setCurrency(element.elementText("Currency"));
    item.setCountry(element.elementText("Country"));
    item.setSite(element.elementText("Site"));
    item.setPostalCode(element.elementText("PostalCode"));
    item.setLocation(element.elementText("Location"));
    item.setItemID(element.elementText("ItemID"));
    item.setHitCounter(element.elementText("HitCounter"));
    item.setAutoPay(element.elementText("AutoPay").equals("true") ? true : false);
    item.setGiftIcon(element.elementText("GiftIcon"));
    item.setListingDuration(element.elementText("ListingDuration"));
    item.setQuantity(Integer.parseInt(element.elementText("Quantity")));
    item.setPayPalEmailAddress(element.elementText("PayPalEmailAddress"));
    item.setGetItFast("false".equals(element.elementText("GetItFast")) ? false : true);
    item.setPrivateListing("true".equals(element.elementText("PrivateListing")) ? true : false);
    item.setDispatchTimeMax(Integer.parseInt(element.elementText("DispatchTimeMax")));
    item.setListingDuration(element.elementText("ListingDuration"));
    item.setDescription(element.elementText("Description"));
    item.setSKU(element.elementText("SKU"));
    item.setConditionID(
        Integer.parseInt(
            element.elementText("CategoryID") == null
                ? "1000"
                : element.elementText("CategoryID")));
    PrimaryCategory pc = new PrimaryCategory();
    pc.setCategoryID(element.element("PrimaryCategory").elementText("CategoryID"));

    item.setPrimaryCategory(pc);
    String listType = "";
    if ("FixedPriceItem".equals(element.elementText("ListingType"))) {
      if (element.element("Variations") != null) {
        listType = "2";
      } else {
        listType = element.elementText("ListingType");
      }
    } else {
      listType = element.elementText("ListingType");
    }
    item.setListingType(listType);
    // 自定义属性
    Element elspe = element.element("ItemSpecifics");
    if (elspe != null) {
      ItemSpecifics itemSpecifics = new ItemSpecifics();
      Iterator<Element> itnvl = elspe.elementIterator("NameValueList");
      List<NameValueList> linvl = new ArrayList<NameValueList>();
      while (itnvl.hasNext()) {
        Element nvl = itnvl.next();
        NameValueList nvli = new NameValueList();
        nvli.setName(nvl.elementText("Name"));
        List<String> listr = new ArrayList();
        Iterator<Element> itval = nvl.elementIterator("Value");
        while (itval.hasNext()) {
          listr.add(itval.next().getText());
        }
        nvli.setValue(listr);
        linvl.add(nvli);
      }
      itemSpecifics.setNameValueList(linvl);
    }
    // 图片信息
    Element pice = element.element("PictureDetails");
    if (pice != null) {
      PictureDetails pd = new PictureDetails();
      if (pice.elementText("GalleryType") != null) {
        pd.setGalleryType(pice.elementText("GalleryType"));
      }
      if (pice.elementText("PhotoDisplay") != null) {
        pd.setPhotoDisplay(pice.elementText("PhotoDisplay"));
      }
      if (pice.elementText("GalleryURL") != null) {
        String url = pice.elementText("GalleryURL");
        if (url.indexOf("?") > 0) {
          url.substring(0, url.indexOf("?"));
        }
        pd.setGalleryURL(url);
      }
      Iterator<Element> itpicurl = pice.elementIterator("PictureURL");
      List<String> urlli = new ArrayList();
      while (itpicurl.hasNext()) {
        Element url = itpicurl.next();
        String urlstr = url.getStringValue();
        if (urlstr.indexOf("?") > 0) {
          urlstr = urlstr.substring(0, urlstr.indexOf("?"));
        }
        urlli.add(urlstr);
      }
      pd.setPictureURL(urlli);
      item.setPictureDetails(pd);
    }
    // 取得退货政策并封装
    Element returne = element.element("ReturnPolicy");
    ReturnPolicy rp = new ReturnPolicy();
    rp.setRefundOption(returne.elementText("RefundOption"));
    rp.setReturnsWithinOption(returne.elementText("ReturnsWithinOption"));
    rp.setReturnsAcceptedOption(returne.elementText("ReturnsAcceptedOption"));
    rp.setDescription(returne.elementText("Description"));
    rp.setShippingCostPaidByOption(returne.elementText("ShippingCostPaidByOption"));
    item.setReturnPolicy(rp);
    // 买家要求
    BuyerRequirementDetails brd = new BuyerRequirementDetails();
    MaximumItemRequirements mirs = new MaximumItemRequirements();
    Element buyere = element.element("BuyerRequirementDetails");
    if (buyere != null) {
      Element maxiteme = buyere.element("MaximumItemRequirements");
      if (maxiteme != null) {
        if (maxiteme.elementText("MaximumItemCount") != null) {
          mirs.setMaximumItemCount(Integer.parseInt(maxiteme.elementText("MaximumItemCount")));
        }
        if (maxiteme.elementText("MinimumFeedbackScore") != null) {
          mirs.setMinimumFeedbackScore(
              Integer.parseInt(maxiteme.elementText("MinimumFeedbackScore")));
        }
        brd.setMaximumItemRequirements(mirs);
      }

      Element maxUnpaid = buyere.element("MaximumUnpaidItemStrikesInfo");
      if (maxUnpaid != null) {
        MaximumUnpaidItemStrikesInfo muis = new MaximumUnpaidItemStrikesInfo();
        String count = maxUnpaid.elementText("Count");
        muis.setCount(Integer.parseInt(count));
        muis.setPeriod(maxUnpaid.elementText("Period"));
        brd.setMaximumUnpaidItemStrikesInfo(muis);
      }

      Element maxPolicy = buyere.element("MaximumBuyerPolicyViolations");
      if (maxPolicy != null) {
        MaximumBuyerPolicyViolations mbpv = new MaximumBuyerPolicyViolations();
        mbpv.setCount(Integer.parseInt(maxPolicy.elementText("Count")));
        mbpv.setPeriod(maxPolicy.elementText("Period"));
        brd.setMaximumBuyerPolicyViolations(mbpv);
      }
      if (buyere.elementText("LinkedPayPalAccount") != null) {
        brd.setLinkedPayPalAccount(
            buyere.elementText("LinkedPayPalAccount").equals("true") ? true : false);
      }
      if (buyere.elementText("ShipToRegistrationCountry") != null) {
        brd.setShipToRegistrationCountry(
            buyere.elementText("ShipToRegistrationCountry").equals("true") ? true : false);
      }
      item.setBuyerRequirementDetails(brd);
    }
    // 运输选项
    ShippingDetails sd = new ShippingDetails();
    Element elsd = element.element("ShippingDetails");
    Iterator<Element> itershipping = elsd.elementIterator("ShippingServiceOptions");
    List<ShippingServiceOptions> lisso = new ArrayList();
    // 国内运输
    while (itershipping.hasNext()) {
      Element shipping = itershipping.next();
      ShippingServiceOptions sso = new ShippingServiceOptions();
      sso.setShippingService(shipping.elementText("ShippingService"));
      if (shipping.elementText("ShippingServiceCost") != null) {
        sso.setShippingServiceCost(
            new ShippingServiceCost(
                shipping.attributeValue("currencyID"),
                Double.parseDouble(shipping.elementText("ShippingServiceCost"))));
      }
      sso.setShippingServicePriority(
          Integer.parseInt(shipping.elementText("ShippingServicePriority")));
      if (shipping.elementText("FreeShipping") != null) {
        sso.setFreeShipping(shipping.elementText("FreeShipping").equals("true") ? true : false);
      }
      ShippingServiceAdditionalCost ssac = new ShippingServiceAdditionalCost();
      ssac.setValue(
          Double.parseDouble(
              shipping.elementText("ShippingServiceAdditionalCost") != null
                  ? shipping.elementText("ShippingServiceAdditionalCost")
                  : "0"));
      sso.setShippingServiceAdditionalCost(ssac);
      ShippingSurcharge ss = new ShippingSurcharge();
      ss.setValue(
          Double.parseDouble(
              shipping.elementText("ShippingSurcharge") != null
                  ? shipping.elementText("ShippingSurcharge")
                  : "0"));
      sso.setShippingSurcharge(ss);
      lisso.add(sso);
    }
    if (lisso.size() > 0) {
      sd.setShippingServiceOptions(lisso);
    }
    // 不运送到的国家
    Iterator<Element> excEl = elsd.elementIterator("ExcludeShipToLocation");
    List<String> liex = new ArrayList<String>();
    while (excEl.hasNext()) {
      Element els = excEl.next();
      liex.add(els.getText());
    }
    if (liex.size() > 0) {
      sd.setExcludeShipToLocation(liex);
    }
    // 国际运输
    Iterator<Element> iteInt = elsd.elementIterator("InternationalShippingServiceOption");
    List<InternationalShippingServiceOption> liint =
        new ArrayList<InternationalShippingServiceOption>();
    while (iteInt.hasNext()) {
      Element intel = iteInt.next();
      InternationalShippingServiceOption isso = new InternationalShippingServiceOption();
      isso.setShippingService(intel.elementText("ShippingService"));
      isso.setShippingServiceCost(
          new ShippingServiceCost(
              intel.attributeValue("currencyID"),
              Double.parseDouble(intel.elementText("ShippingServiceCost"))));
      Iterator<Element> iteto = intel.elementIterator("ShipToLocation");
      List<String> listr = new ArrayList();
      while (iteto.hasNext()) {
        listr.add(iteto.next().getText());
      }
      isso.setShipToLocation(listr);
      isso.setShippingServicePriority(
          Integer.parseInt(intel.elementText("ShippingServicePriority")));

      ShippingServiceAdditionalCost ssac = new ShippingServiceAdditionalCost();
      ssac.setValue(
          Double.parseDouble(
              intel.elementText("ShippingServiceAdditionalCost") != null
                  ? intel.elementText("ShippingServiceAdditionalCost")
                  : "0"));
      isso.setShippingServiceAdditionalCost(ssac);
      liint.add(isso);
    }
    if (liint.size() > 0) {
      sd.setInternationalShippingServiceOption(liint);
    }
    // 计算所需的长宽高
    Element re = elsd.element("CalculatedShippingRate");
    if (re != null) {
      CalculatedShippingRate csr = new CalculatedShippingRate();
      if (re.elementText("InternationalPackagingHandlingCosts") != null) {
        csr.setPackagingHandlingCosts(
            new PackagingHandlingCosts(
                re.attributeValue("currencyID"),
                Double.parseDouble(re.elementText("InternationalPackagingHandlingCosts"))));
      }
      if (re.elementText("OriginatingPostalCode") != null) {
        csr.setOriginatingPostalCode(re.elementText("OriginatingPostalCode"));
      }
      if (re.elementText("PackageDepth") != null) {
        csr.setPackageDepth(Double.parseDouble(re.elementText("PackageDepth")));
      }
      if (re.elementText("PackageLength") != null) {
        csr.setPackageLength(Double.parseDouble(re.elementText("PackageLength")));
      }
      if (re.elementText("PackageWidth") != null) {
        csr.setPackageWidth(Double.parseDouble(re.elementText("PackageWidth")));
      }
      if (re.elementText("ShippingIrregular") != null) {
        csr.setShippingIrregular(Boolean.parseBoolean(re.elementText("ShippingIrregular")));
      }
      if (re.elementText("ShippingPackage") != null) {
        csr.setShippingPackage(re.elementText("ShippingPackage"));
      }
      if (re.elementText("WeightMajor") != null) {
        csr.setWeightMajor(Double.parseDouble(re.elementText("WeightMajor")));
      }
      if (re.elementText("WeightMinor") != null) {
        csr.setWeightMinor(Double.parseDouble(re.elementText("WeightMinor")));
      }
      sd.setCalculatedShippingRate(csr);
    }
    sd.setShippingType(elsd.elementText("ShippingType"));
    item.setShippingDetails(sd);
    // 卖家信息
    Seller seller = new Seller();
    Element elsel = element.element("Seller");
    seller.setUserID(elsel.elementText("UserID"));
    seller.setEmail(elsel.elementText("Email"));
    item.setSeller(seller);
    // 多属性
    Element vartions = element.element("Variations");
    if (vartions != null) {
      Iterator<Element> elvar = vartions.elementIterator("Variation");
      List<Variation> livar = new ArrayList();
      while (elvar.hasNext()) {
        Element ele = elvar.next();
        Variation var = new Variation();
        var.setSKU(ele.elementText("SKU"));
        var.setQuantity(
            Integer.parseInt(ele.elementText("Quantity"))
                - Integer.parseInt(ele.element("SellingStatus").elementText("QuantitySold")));
        var.setStartPrice(
            new StartPrice(
                ele.attributeValue("currencyID"),
                Double.parseDouble(ele.elementText("StartPrice"))));
        Element elvs = ele.element("VariationSpecifics");
        Iterator<Element> elnvl = elvs.elementIterator("NameValueList");
        List<NameValueList> linvl = new ArrayList();
        while (elnvl.hasNext()) {
          Element elment = elnvl.next();
          NameValueList nvl = new NameValueList();
          nvl.setName(elment.elementText("Name"));
          List<String> li = new ArrayList<String>();
          li.add(elment.elementText("Value"));
          nvl.setValue(li);
          linvl.add(nvl);
        }
        List<VariationSpecifics> livs = new ArrayList();
        VariationSpecifics vs = new VariationSpecifics();
        vs.setNameValueList(linvl);
        livs.add(vs);
        var.setVariationSpecifics(livs);
        livar.add(var);
      }
      Variations vtions = new Variations();
      vtions.setVariation(livar);
      // 多属性值
      Element elvss = vartions.element("VariationSpecificsSet");
      Iterator<Element> itele = elvss.elementIterator("NameValueList");
      List<NameValueList> linvl = new ArrayList();
      while (itele.hasNext()) {
        Element nvlel = itele.next();
        NameValueList nvl = new NameValueList();
        nvl.setName(nvlel.elementText("Name"));
        Iterator<Element> itvalue = nvlel.elementIterator("Value");
        List<String> livalue = new ArrayList();
        while (itvalue.hasNext()) {
          Element value = itvalue.next();
          livalue.add(value.getText());
        }
        nvl.setValue(livalue);
        linvl.add(nvl);
      }
      VariationSpecificsSet vss = new VariationSpecificsSet();
      vss.setNameValueList(linvl);
      vtions.setVariationSpecificsSet(vss);
      // 多属性图片信息
      Pictures pic = new Pictures();
      Element elpic = vartions.element("Pictures");
      pic.setVariationSpecificName(elpic.elementText("VariationSpecificName"));
      Iterator<Element> iturl = elpic.elementIterator("VariationSpecificPictureSet");
      List<VariationSpecificPictureSet> livsps = new ArrayList();
      while (iturl.hasNext()) {
        Element urle = iturl.next();
        VariationSpecificPictureSet vsps = new VariationSpecificPictureSet();
        vsps.setVariationSpecificValue(urle.elementText("VariationSpecificValue"));
        Iterator<Element> url = urle.elementIterator("PictureURL");
        List li = new ArrayList();
        while (url.hasNext()) {
          Element e = url.next();
          String urlstr = e.getText();
          if (urlstr.indexOf("?") > 0) {
            urlstr = urlstr.substring(0, urlstr.indexOf("?"));
          }
          li.add(urlstr);
        }
        vsps.setPictureURL(li);
        livsps.add(vsps);
      }
      pic.setVariationSpecificPictureSet(livsps);
      vtions.setPictures(pic);

      item.setVariations(vtions);
    } else {
      Element el = element.element("StartPrice");
      item.setStartPrice(
          new StartPrice(el.attributeValue("currencyID"), Double.parseDouble(el.getText())));
      if (element.element("BuyItNowPrice") != null) {
        item.setBuyItNowPrice(Double.parseDouble(element.elementText("BuyItNowPrice")));
      }
      if (element.element("ReservePrice") != null) {
        item.setReservePrice(Double.parseDouble(element.elementText("ReservePrice")));
      }
    }
    return item;
  }