LEG_xml解析

来源:互联网 发布:北京阿里云公司在哪 编辑:程序博客网 时间:2024/06/15 03:56
先保存,以后研究
public void testgetGpmsContractByXml() {String buffer = null;//将要上传的文件转换成byte[]类型,保存到FileRequest的bytes属性中try {File file1 = new File("E://gpms_xml.xml");FileInputStream fis1 = new FileInputStream(file1);ByteArrayOutputStream bos1 = new ByteArrayOutputStream(1024);byte[] b = new byte[1024];int n;while ((n = fis1.read(b)) != -1) {bos1.write(b, 0, n);}fis1.close();bos1.close();buffer = bos1.toString();GpmsContractServiceImpl gpmsContractServiceImpl =new GpmsContractServiceImpl();gpmsContractServiceImpl.getGpmsContractByXml(buffer);} catch (Exception e) {e.printStackTrace();}}


public GpmsPreSys getGpmsContractByXml(String contractXml){//接口类GpmsPreSys preSys = new GpmsPreSys();//解析接口传入的XML文件Map<String, Object> tables = ParseXml.parseGPMSErp(contractXml);preSys.setAppliyId(tables.get("appliy_id")==null?"":tables.get("appliy_id").toString());//申请人编码preSys.setContractId(tables.get("contract_id")==null?"":tables.get("contract_id").toString());//合同IDpreSys.setContractName(tables.get("contract_name")==null?"":tables.get("contract_name").toString());//合同名称//preSys.setChannelCode(tables.get("channel_code")==null?"":tables.get("channel_code").toString());//渠道编码preSys.setContractType1Sn(tables.get("contract_type1_sn")==null?"":tables.get("contract_type1_sn").toString());//一级合同编码preSys.setContractType2Sn(tables.get("contract_type2_sn")==null?"":tables.get("contract_type2_sn").toString());//二级合同编码preSys.setContractCode(tables.get("contract_code")==null?"":tables.get("contract_code").toString());//三级合同编码preSys.setUrl(tables.get("url")==null?"":tables.get("url").toString());//三级合同编码preSys.setProcId(tables.get("proc_id")==null?"":tables.get("proc_id").toString());//流程实例ID//获得brandif (tables.get("brand") != null) {@SuppressWarnings("unchecked")List<String> brandList = (List<String>) tables.get("brand");//产品listList<String> contractEntitySysList = new ArrayList<String>();for (String brandCode : brandList) {if (StringUtil.isNotEmpty(brandCode)) {contractEntitySysList.add(brandCode);//将获得的签约方信息添加到list}}//将brnad的list放入接口类实体preSys.setBrand(contractEntitySysList);} else {preSys.setBrand(null);}//获得productif (tables.get("product") != null) {@SuppressWarnings("unchecked")List<String> projectList = (List<String>) tables.get("product");//产品listList<String> contractEntitySysList = new ArrayList<String>();for (String productCode : projectList) {if (StringUtil.isNotEmpty(productCode)) {contractEntitySysList.add(productCode);//将获得的签约方信息添加到list}}//将project的list放入接口类实体preSys.setProcduct(contractEntitySysList);} else {preSys.setProcduct(null);}//获得channelif (tables.get("channel") != null) {@SuppressWarnings("unchecked")List<String> channeltList = (List<String>) tables.get("channel");//产品listList<String> contractEntitySysList = new ArrayList<String>();for (String channelCode : channeltList) {if (StringUtil.isNotEmpty(channelCode)) {contractEntitySysList.add(channelCode);//将获得的签约方信息添加到list}}//将project的list放入接口类实体preSys.setChannel(contractEntitySysList);} else {preSys.setChannel(null);}return preSys;}


@SuppressWarnings("rawtypes")public static Map<String, Object> parseGPMSErp(String xmlDoc) {logger.info(xmlDoc);Map<String, Object> tables = new HashMap<String, Object>();//创建一个新的字符串StringReader read = new StringReader(xmlDoc);//创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入InputSource source = new InputSource(read);//创建一个新的SAXBuilderSAXBuilder sb = new SAXBuilder();try {//通过输入源构造一个DocumentDocument doc = sb.build(source);//取的根元素Element root = doc.getRootElement();//得到根元素所有子元素的集合List tableNames = root.getChildren();Element table = null;List<String> brandList = new ArrayList<String>();List<String> productList = new ArrayList<String>();List<String> channelList = new ArrayList<String>();//循环tablefor (int i = 0; i < tableNames.size(); i++) {table = (Element) tableNames.get(i);//循环依次得到子元素//得到品牌if("brand".equals(table.getName())){List contractRows=table.getChildren();for(int p=0;p<contractRows.size();p++){Element contractRow=(Element)contractRows.get(p);brandList.add(contractRow.getValue().toString());}//得到产品}else if ("product".equals(table.getName())) {List contractRows = table.getChildren();for (int p = 0; p < contractRows.size(); p++) {Element contractRow = (Element) contractRows.get(p);productList.add(contractRow.getValue().toString());}// 得到渠道}else if ("channel".equals(table.getName())) {List contractRows = table.getChildren();for (int p = 0; p < contractRows.size(); p++) {Element contractRow = (Element) contractRows.get(p);channelList.add(contractRow.getValue().toString());}}else {tables.put(table.getName(), table.getValue().toString());}}tables.put("brand", brandList);tables.put("product", productList);tables.put("channel", channelList);       logger.info(JSONArray.fromObject(tables));} catch (JDOMException e) {//e.printStackTrace();logger.info(e.getMessage());} catch (IOException e) {//e.printStackTrace();logger.info(e.getMessage());}logger.info(tables);return tables;}



0 0
原创粉丝点击