package com.example.demo; import com.example.demo.model.T_TXD_FWB; import com.example.demo.model.T_TXD_FWBPARTY; import com.example.demo.service.T_TXD_FWB_Service; import com.example.demo.util.XML.XML2ENTITY; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import java.io.IOException; import java.math.BigDecimal; import java.math.BigInteger; import java.time.ZonedDateTime; import java.util.Date; import java.util.List; import java.util.Map; public class FWBDemo { protected static final Logger logger = LoggerFactory.getLogger(FWBDemo.class); @Autowired private static T_TXD_FWB_Service fwb_service; public static void main(String[] args) throws IOException, DocumentException { // FileInputStream fis = new FileInputStream("d://a.xml"); // byte[] b = new byte[fis.available()]; // fis.read(b); // String str = new String(b); String xml = "<MSG>\n" + "\t<META>\n" + "\t\t<SNDR>TXD</SNDR>\n" + "\t\t<DDTM>20181114040536</DDTM>\n" + "\t\t<TYPE>DFME</TYPE>\n" + "\t\t<STYP>FWB</STYP>\n" + "\t\t<SEQN>4643187</SEQN>\n" + "\t</META>\n" + "\t<MasterConsignment>\n" + "\t\t<ID>880-83213594</ID>\n" + "\t\t<TypeCode>741</TypeCode>\n" + "\t\t<NilCarriageValueIndicator>false</NilCarriageValueIndicator>\n" + "\t\t<DeclaredValueForCarriageAmount currencyID=\"CNY\">0</DeclaredValueForCarriageAmount>\n" + "\t\t<NilCustomsValueIndicator>true</NilCustomsValueIndicator>\n" + "\t\t<DeclaredValueForCustomsAmount currencyID=\"CNY\">NCV</DeclaredValueForCustomsAmount>\n" + "\t\t<NilInsuranceValueIndicator>true</NilInsuranceValueIndicator>\n" + "\t\t<InsuranceValueAmount currencyID=\"CNY\">XXX</InsuranceValueAmount>\n" + "\t\t<TotalChargePrepaidIndicator>true</TotalChargePrepaidIndicator>\n" + "\t\t<WeightTotalChargeAmount currencyID=\"CNY\">502</WeightTotalChargeAmount>\n" + "\t\t<ValuationTotalChargeAmount currencyID=\"CNY\">0.00</ValuationTotalChargeAmount>\n" + "\t\t<TotalDisbursementPrepaidIndicator>true</TotalDisbursementPrepaidIndicator>\n" + "\t\t<TotalPrepaidChargeAmount currencyID=\"CNY\">613.60</TotalPrepaidChargeAmount>\n" + "\t\t<TotalCollectChargeAmount currencyID=\"CNY\">0</TotalCollectChargeAmount>\n" + "\t\t<DestinationCurrencyTotalCollectChargeAmount currencyID=\"CNY\">0</DestinationCurrencyTotalCollectChargeAmount>\n" + "\t\t<IncludedTareGrossWeightMeasure unitCode=\"KGM\">558.0</IncludedTareGrossWeightMeasure>\n" + "\t\t<NetWeightMeasure/>\n" + "\t\t<GrossVolumeMeasure unitCode=\"MTQ\">4.44</GrossVolumeMeasure>\n" + "\t\t<TotalChargeableWeightMeasure unitCode=\"KGM\">558.0</TotalChargeableWeightMeasure>\n" + "\t\t<ConsignmentItemQuantity>1</ConsignmentItemQuantity>\n" + "\t\t<TotalPieceQuantity>74</TotalPieceQuantity>\n" + "\t\t<TotalLoadedPackageQuantity>74</TotalLoadedPackageQuantity>\n" + "\t\t<PackageInfo>编织袋</PackageInfo>\n" + "\t\t<FreightRateTypeCode>Q</FreightRateTypeCode>\n" + "\t\t<ConsignorParty>\n" + "\t\t\t<PrimaryID schemeAgencyID=\"1\">HNHH</PrimaryID>\n" + "\t\t\t<Name>河南汇海物流有限公司</Name>\n" + "\t\t\t<AccountID>INFOSKY:NULL</AccountID>\n" + "\t\t\t<PostalStructuredAddress>\n" + "\t\t\t\t<StreetName>郑州</StreetName>\n" + "\t\t\t\t<CityName>CGO</CityName>\n" + "\t\t\t\t<CountryID>CN</CountryID>\n" + "\t\t\t\t<SpecifiedAddressLocation/>\n" + "\t\t\t</PostalStructuredAddress>\n" + "\t\t\t<SpecifiedCargoAgentLocation/>\n" + "\t\t\t<DefinedTradeContact>\n" + "\t\t\t\t<DirectTelephoneCommunication>\n" + "\t\t\t\t\t<CompleteNumber>CGO</CompleteNumber>\n" + "\t\t\t\t</DirectTelephoneCommunication>\n" + "\t\t\t</DefinedTradeContact>\n" + "\t\t</ConsignorParty>\n" + "\t\t<ConsigneeParty>\n" + "\t\t\t<PrimaryID schemeAgencyID=\"2\">SK</PrimaryID>\n" + "\t\t\t<Name>海南顺丰速运有限公司</Name>\n" + "\t\t\t<AccountID>INFOSKY:NULL</AccountID>\n" + "\t\t\t<PostalStructuredAddress>\n" + "\t\t\t\t<StreetName>机场自提</StreetName>\n" + "\t\t\t\t<CityName>HAK</CityName>\n" + "\t\t\t\t<CountryID>CN</CountryID>\n" + "\t\t\t\t<SpecifiedAddressLocation/>\n" + "\t\t\t</PostalStructuredAddress>\n" + "\t\t\t<SpecifiedCargoAgentLocation/>\n" + "\t\t\t<DefinedTradeContact/>\n" + "\t\t</ConsigneeParty>\n" + "\t\t<FreightForwarderParty>\n" + "\t\t\t<Name>CGOSA</Name>\n" + "\t\t\t<AccountID>INFOSKY:NULL</AccountID>\n" + "\t\t\t<PostalStructuredAddress>\n" + "\t\t\t\t<CityName>CGO</CityName>\n" + "\t\t\t\t<CountryID>CN</CountryID>\n" + "\t\t\t\t<SpecifiedAddressLocation/>\n" + "\t\t\t</PostalStructuredAddress>\n" + "\t\t\t<SpecifiedCargoAgentLocation/>\n" + "\t\t\t<DefinedTradeContact/>\n" + "\t\t</FreightForwarderParty>\n" + "\t\t<AssociatedParty>\n" + "\t\t\t<PrimaryID/>\n" + "\t\t\t<Name>CGO</Name>\n" + "\t\t\t<AccountID>INFOSKY:NULL</AccountID>\n" + "\t\t\t<RoleCode>AGT</RoleCode>\n" + "\t\t\t<Role>Agent</Role>\n" + "\t\t\t<PostalStructuredAddress>\n" + "\t\t\t\t<CityName>CGO</CityName>\n" + "\t\t\t\t<CountryID>CN</CountryID>\n" + "\t\t\t\t<SpecifiedAddressLocation/>\n" + "\t\t\t</PostalStructuredAddress>\n" + "\t\t\t<SpecifiedCargoAgentLocation/>\n" + "\t\t\t<DefinedTradeContact/>\n" + "\t\t</AssociatedParty>\n" + "\t\t<AssociatedParty>\n" + "\t\t\t<PrimaryID/>\n" + "\t\t\t<Name>CGOHA</Name>\n" + "\t\t\t<AccountID>INFOSKY:NULL</AccountID>\n" + "\t\t\t<RoleCode>GHA</RoleCode>\n" + "\t\t\t<Role>Ground Handling Agent</Role>\n" + "\t\t\t<PostalStructuredAddress>\n" + "\t\t\t\t<CityName>CGO</CityName>\n" + "\t\t\t\t<CountryID>CN</CountryID>\n" + "\t\t\t\t<SpecifiedAddressLocation/>\n" + "\t\t\t</PostalStructuredAddress>\n" + "\t\t\t<SpecifiedCargoAgentLocation/>\n" + "\t\t\t<DefinedTradeContact/>\n" + "\t\t</AssociatedParty>\n" + "\t\t<OriginLocation>\n" + "\t\t\t<ID>CGO</ID>\n" + "\t\t</OriginLocation>\n" + "\t\t<FinalDestinationLocation>\n" + "\t\t\t<ID>HAK</ID>\n" + "\t\t</FinalDestinationLocation>\n" + "\t\t<SpecifiedLogisticsTransportMovement>\n" + "\t\t\t<StageCode>HU7304/Nov14</StageCode>\n" + "\t\t\t<ModeCode>4</ModeCode>\n" + "\t\t\t<Mode>Air Transport</Mode>\n" + "\t\t\t<ID>HU7304</ID>\n" + "\t\t\t<SequenceNumeric>1</SequenceNumeric>\n" + "\t\t\t<UsedLogisticsTransportMeans/>\n" + "\t\t\t<ArrivalEvent>\n" + "\t\t\t\t<OccurrenceArrivalLocation>\n" + "\t\t\t\t\t<ID>HAK</ID>\n" + "\t\t\t\t</OccurrenceArrivalLocation>\n" + "\t\t\t</ArrivalEvent>\n" + "\t\t\t<DepartureEvent>\n" + "\t\t\t\t<ScheduledOccurrenceDateTime>2018-11-14T00:00:00+08:00</ScheduledOccurrenceDateTime>\n" + "\t\t\t\t<OccurrenceDepartureLocation>\n" + "\t\t\t\t\t<ID>CGO</ID>\n" + "\t\t\t\t</OccurrenceDepartureLocation>\n" + "\t\t\t</DepartureEvent>\n" + "\t\t</SpecifiedLogisticsTransportMovement>\n" + "\t\t<IncludedAccountingNote>\n" + "\t\t\t<ContentCode>20010003普通</ContentCode>\n" + "\t\t\t<Content>20010003普通</Content>\n" + "\t\t</IncludedAccountingNote>\n" + "\t\t<AssociatedConsignmentCustomsProcedure/>\n" + "\t\t<ApplicableTradeCurrencyExchange>\n" + "\t\t\t<SourceCurrencyCode>CNY</SourceCurrencyCode>\n" + "\t\t\t<TargetCurrencyCode>CNY</TargetCurrencyCode>\n" + "\t\t\t<MarketID>S</MarketID>\n" + "\t\t\t<ConversionRate>1</ConversionRate>\n" + "\t\t</ApplicableTradeCurrencyExchange>\n" + "\t\t<ApplicableLogisticsServiceCharge/>\n" + "\t\t<ApplicableLogisticsAllowanceCharge>\n" + "\t\t\t<ID>MY</ID>\n" + "\t\t\t<Reason>燃油费</Reason>\n" + "\t\t\t<ActualAmount currencyID=\"CNY\">111.6</ActualAmount>\n" + "\t\t\t<PartyTypeCode>C</PartyTypeCode>\n" + "\t\t</ApplicableLogisticsAllowanceCharge>\n" + "\t\t<SignatoryCarrierAuthentication>\n" + "\t\t\t<ActualDateTime>2018-11-14T04:02:00</ActualDateTime>\n" + "\t\t\t<Signatory>牛青</Signatory>\n" + "\t\t\t<IssueAuthenticationLocation>\n" + "\t\t\t\t<Name>郑州</Name>\n" + "\t\t\t</IssueAuthenticationLocation>\n" + "\t\t</SignatoryCarrierAuthentication>\n" + "\t\t<IncludedMasterConsignmentItem>\n" + "\t\t\t<SequenceNumeric>1</SequenceNumeric>\n" + "\t\t\t<TypeCode listAgencyID=\"1\">PH</TypeCode>\n" + "\t\t\t<GrossWeightMeasure unitCode=\"KGM\">558.0</GrossWeightMeasure>\n" + "\t\t\t<GrossVolumeMeasure unitCode=\"MTQ\">4.44</GrossVolumeMeasure>\n" + "\t\t\t<PieceQuantity>74</PieceQuantity>\n" + "\t\t\t<TareWeightMeasure unitCode=\"KGM\">558.0</TareWeightMeasure>\n" + "\t\t\t<NatureIdentificationTransportCargo>\n" + "\t\t\t\t<Identification>手机机头(无电池) 电子主板 上衣 茶叶 大枣 皮带 票证 运动鞋 背包 灯座</Identification>\n" + "\t\t\t</NatureIdentificationTransportCargo>\n" + "\t\t\t<OriginCountry/>\n" + "\t\t\t<AssociatedUnitLoadTransportEquipment>\n" + "\t\t\t\t<OperatingParty/>\n" + "\t\t\t</AssociatedUnitLoadTransportEquipment>\n" + "\t\t\t<TransportLogisticsPackage>\n" + "\t\t\t\t<ItemQuantity>74</ItemQuantity>\n" + "\t\t\t\t<LinearSpatialDimension>\n" + "\t\t\t\t\t<Description>true</Description>\n" + "\t\t\t\t\t<WidthMeasure unitCode=\"CMT\">40</WidthMeasure>\n" + "\t\t\t\t\t<LengthMeasure unitCode=\"CMT\">30</LengthMeasure>\n" + "\t\t\t\t\t<HeightMeasure unitCode=\"CMT\">50</HeightMeasure>\n" + "\t\t\t\t</LinearSpatialDimension>\n" + "\t\t\t</TransportLogisticsPackage>\n" + "\t\t\t<ApplicableFreightRateServiceCharge>\n" + "\t\t\t\t<CategoryCode>Q</CategoryCode>\n" + "\t\t\t\t<CommodityItemID>P</CommodityItemID>\n" + "\t\t\t\t<ChargeableWeightMeasure unitCode=\"KGM\">558.0</ChargeableWeightMeasure>\n" + "\t\t\t\t<AppliedRate>0.9</AppliedRate>\n" + "\t\t\t\t<AppliedAmount currencyID=\"CNY\">502</AppliedAmount>\n" + "\t\t\t</ApplicableFreightRateServiceCharge>\n" + "\t\t\t<SpecifiedRateCombinationPointLocation/>\n" + "\t\t</IncludedMasterConsignmentItem>\n" + "\t\t<ReportedStatus>\n" + "\t\t\t<ReasonCode>FWB</ReasonCode>\n" + "\t\t\t<EventTime>\n" + "\t\t\t\t<OccurrenceDateTime>2018-11-14T04:03:02+08:00</OccurrenceDateTime>\n" + "\t\t\t\t<DateTimeTypeCode>Actual</DateTimeTypeCode>\n" + "\t\t\t</EventTime>\n" + "\t\t\t<SpecifiedLocation>\n" + "\t\t\t\t<ID>CGO</ID>\n" + "\t\t\t</SpecifiedLocation>\n" + "\t\t</ReportedStatus>\n" + "\t</MasterConsignment>\n" + "</MSG>"; Document doc = DocumentHelper.parseText(xml); long beginTime = System.currentTimeMillis(); try{ Map<String, Object> map = XML2ENTITY.Dom2Map(doc); T_TXD_FWB fwb= new T_TXD_FWB(); //fwb.setFid(getBigDecimal(1)); fwb.setMessageBakId(getBigDecimal(1)); // 头部解析 Map metamap = (Map) map.get("META"); String SEDR = (String) metamap.get("SNDR"); //报体解析入口 Map MasterConsignment = (Map) map.get("MasterConsignment"); fwb.setId(MasterConsignment.get("ID").toString()); fwb.setTypecode(MasterConsignment.get("TypeCode").toString()); fwb.setNilcarriagevaluecarriage(MasterConsignment.get("NilCarriageValueIndicator").toString()); fwb.setDeclaredvalue(getBigDecimal(MasterConsignment.get("DeclaredValueForCarriageAmount"))); //发货人节点 Map consiger = (Map) MasterConsignment.get("ConsignorParty"); fwb.setCrpPrimaryid(consiger.get("PrimaryID").toString()); fwb.setCrpName(consiger.get("Name").toString()); fwb.setCrpAccountid(consiger.get("AccountID").toString()); fwb.setPsaStreetname(((Map)consiger.get("PostalStructuredAddress")).get("StreetName").toString()); //这个是不是发货人节点名称 库里面没字段注释 插入的时候要核实 fwb.setPsaCityname(((Map)consiger.get("PostalStructuredAddress")).get("CityName").toString()); fwb.setPsaCountryid(((Map)consiger.get("PostalStructuredAddress")).get("CountryID").toString()); fwb.setPsaSpecifiedaddress(((Map)consiger.get("PostalStructuredAddress")).get("SpecifiedAddressLocation").toString()); //收货人节点 //航班节点,表中的航班字段在哪里没找到 Map flight = (Map) MasterConsignment.get("SpecifiedLogisticsTransportMovement"); String flightNo = flight.get("ID").toString(); String flightDevTime = ((Map)flight.get("DepartureEvent")).get("ScheduledOccurrenceDateTime").toString(); ZonedDateTime depZoneTime = ZonedDateTime.parse(flightDevTime); Date flightDate = Date.from(depZoneTime.toInstant()); //这个是入库航班日期格式 //货物节点 //插入主数据表 int fid = fwb_service.insert(fwb); //处理字表 //重复的AssociatedParty节点 Object obj_AssociatedParty = MasterConsignment.get("AssociatedParty"); if (obj_AssociatedParty.getClass().getName().equals("java.util.ArrayList")){//多个AssociatedParty List<Map> list_AssociatedParty = (List<Map>) MasterConsignment.get("AssociatedParty"); for (Map AssociatedParty : list_AssociatedParty){ AssociatedPartyHandle(AssociatedParty); } }else if (obj_AssociatedParty.getClass().getName().equals("java.util.HashMap")){ //一个报文,一个ArrivalEvent代表装载只有一个目的地 Map map_AssociatedParty = (Map) MasterConsignment.get("AssociatedParty"); AssociatedPartyHandle(map_AssociatedParty); } //重复的ReportedStatus节点 System.out.println(map.toString()); System.out.println("Use time:"+(System.currentTimeMillis()-beginTime)); }catch (Exception e){ logger.info("报文解析异常"+e.toString()+"\n报文内容"+doc.asXML()); } } static public void AssociatedPartyHandle(Map AssociatedParty){ //入库AssociatedParty实体处理 T_TXD_FWBPARTY assParty = new T_TXD_FWBPARTY(); assParty.setTxdFwbId(getBigDecimal(1)); } /** * Object转BigDecimal类型-MRZ-2018年5月14日09:56:26 * * @param value 要转的object类型 * @return 转成的BigDecimal类型数据 */ public static BigDecimal getBigDecimal(Object value) { BigDecimal ret = null; if (value != null) { if (value instanceof BigDecimal) { ret = (BigDecimal) value; } else if (value instanceof String) { ret = new BigDecimal((String) value); } else if (value instanceof BigInteger) { ret = new BigDecimal((BigInteger) value); } else if (value instanceof Number) { ret = new BigDecimal(((Number) value).doubleValue()); } else { throw new ClassCastException("Not possible to coerce [" + value + "] from class " + value.getClass() + " into a BigDecimal."); } } return ret; } }