直接贴一段业务代码,这段代码是解析请求返回的xml格式字符串,为了取出其中的值便于下一步的使用。

 @RequestMapping(value="/search",produces = {"text/html;charset=utf-8"})
public String searchGoods(@RequestParam(value="itemBarCode", required = false)String itemBarCode,
@RequestParam(value="typenumber", required = false)String typenumber,
@RequestParam(value="mts", required = false)String mts,
@RequestParam(value="mte", required = false)String mte,
@RequestParam(value="ocdsupport", required = false)String ocdsupport,
@RequestParam(value="yunfeimoban", required = false)String yunfeimoban,
@RequestParam(value="goodsname", required = false)String goodsname,
@RequestParam(value="goodstate", required = false)String goodstate,
@RequestParam(value="kucun2", required = false)String kucun2,
@RequestParam(value="kucun1", required = false)String kucun1,
@RequestParam(value="barcode", required = false)String barcode,
@RequestParam(value="type", required = false)String type,
@RequestParam(value="innertype", required = false)String innertype,
@RequestParam(value="storenumber", required = false)String storenumber,
@RequestParam(value="namelabel", required = false)String namelabel,
@RequestParam(value="checkstate", required = false)String checkstate,
@RequestParam(value="storename", required = false)String storename,
@RequestParam(value="pageInfo", required = false)String pageInfo) throws ApiException {
StoreKeyInfo storeKeyInfo = storeKeyInfoService.getStoreKeyInfo(storename);
Map<Integer,String> maps = new HashMap<Integer,String>();
SdkClient sdkClient = null;
if(storeKeyInfo!=null) {
String appKey = storeKeyInfo.getAppKey();
String appSecret = storeKeyInfo.getAppSecret();
String session = storeKeyInfo.getAuthenCode();
sdkClient = new SdkClient(appKey, appSecret, session, version,format,
connectTimeout,readTimeout,needEnableParser,needCheckRequest);
}else{
System.out.println("找不到店铺:店铺名字为空");
}
ItemsListGet itemsListGet=new ItemsListGet();
//开始设置查询条件,有的参数就设置,没有就不设置 if(typenumber!=null && !typenumber.equals("")){
itemsListGet.setItm(typenumber);
}
if(mts!=null && !mts.equals("")){
itemsListGet.setMts(mts);
}
if(mte!=null && !mte.equals("")){
itemsListGet.setMte(mte);
}
if(ocdsupport!=null && !ocdsupport.equals("")){
itemsListGet.setIs_cod(ocdsupport);
}
if(goodsname!=null && !goodsname.equals("")){
itemsListGet.setName(goodsname);
}
if(goodstate!=null && !goodstate.equals("")){
itemsListGet.setIts(goodstate);
}
if(kucun1!=null && !kucun1.equals("")){
itemsListGet.setSlv(kucun1);
}
if(kucun2!=null && !kucun2.equals("")){
itemsListGet.setShv(kucun2);
}
if(barcode!=null && !barcode.equals("")){
itemsListGet.setBar_code(barcode);
}
if(innertype!=null && !innertype.equals("")){
itemsListGet.setSc(innertype);
}
if(namelabel!=null && !namelabel.equals("")){
itemsListGet.setItb(namelabel);
}
if(checkstate!=null && !checkstate.equals("")){
itemsListGet.setDatatype(checkstate);
}
ItemsListGetRequest itemsListGetRequest=new ItemsListGetRequest();
itemsListGetRequest.setItemsListGet(itemsListGet);
ItemsListGetResponse result=sdkClient.excute(itemsListGetRequest); String[] ids = null;
String[] updatetimeStrs = null;
String[] datatypeStrs = null;
String pageSize = null;
int index11 = 0;
int index22 = 0;
System.out.println("返回的数据是:"+result.getBody());
index11 = result.getBody().indexOf("<pageTotal><![CDATA[");
index22 = result.getBody().indexOf("]]></pageTotal>");
pageSize = result.getBody().substring(index11 + 20, index22);
int page = Integer.valueOf(pageSize);
System.out.println("总页码"+pageSize);
// 将页码控制在10页,其他的点击页面11页,才调用
if (page >= 1) {
page = 1;
}
//如果没有点击页码,就按照这个查询
if(pageInfo !=null && !pageInfo.equals("") && pageInfo.equals("0")) { int insertIndex = 0; //如果没有传入PageInfo,默认第一页。
for (int pageindex = 1; pageindex <= page; pageindex++) {
itemsListGet.setP(String.valueOf(pageindex));
itemsListGetRequest = new ItemsListGetRequest();
itemsListGetRequest.setItemsListGet(itemsListGet);
result = sdkClient.excute(itemsListGetRequest);
System.out.println("返回的信息"+result.getBody());
int count = countStr(result.getBody(), "<itemID><![CDATA["); ids= new String[count];
updatetimeStrs =new String[count];
datatypeStrs = new String[count]; int[] indexbegin = new int[1000000];
int[] indexbegin2 = new int[1000000];
int[] indexbegin3 = new int[1000000];
for (int i = 1; i <= count; i++) {
indexbegin[i - 1] = getIndex(result.getBody(), i, "<itemID>");
indexbegin2[i - 1] = getIndex(result.getBody(), i, "<updateTime>");
indexbegin3[i - 1] = getIndex(result.getBody(), i, "<datatype>");
}
int[] indexend = new int[1000000];
int[] indexend2 = new int[1000000];
int[] indexend3 = new int[1000000];
for (int i = 1; i <= count; i++) {
indexend[i - 1] = getIndex(result.getBody(), i, "</itemID>");
indexend2[i - 1] = getIndex(result.getBody(), i, "</updateTime>");
indexend3[i - 1] = getIndex(result.getBody(), i, "</datatype>");
} for (; insertIndex - (pageindex - 1) * 20 < count; insertIndex++) {
int index1 = 0;
int index2 = 0;
int index41 = 0;
int index42 = 0;
int index31 = 0;
int index32 = 0;
index1 = indexbegin[insertIndex - (pageindex - 1) * 20];
index2 = indexend[insertIndex - (pageindex - 1) * 20];
ids[insertIndex] = result.getBody().substring(index1 + 17, index2 - 3); index41 = indexbegin2[insertIndex - (pageindex - 1) * 20];
index42 = indexend2[insertIndex - (pageindex - 1) * 20];
updatetimeStrs[insertIndex] = result.getBody().substring(index41 + 21, index42 - 3); index31 = indexbegin3[insertIndex - (pageindex - 1) * 20];
index32 = indexend3[insertIndex - (pageindex - 1) * 20];
datatypeStrs[insertIndex] = result.getBody().substring(index31 + 19, index32 - 3);
}
} }else if(pageInfo !=null && !pageInfo.equals("") && !pageInfo.equals("0")){
//如果有点击页码就根据页码单页查询
page = Integer.valueOf(pageInfo);
itemsListGet.setP(String.valueOf(page));
itemsListGetRequest = new ItemsListGetRequest();
itemsListGetRequest.setItemsListGet(itemsListGet);
result = sdkClient.excute(itemsListGetRequest); updatetimeStrs = new String[page * 20];
datatypeStrs = new String[page * 20];
System.out.println("返回的信息"+result.getBody());
int count = countStr(result.getBody(),"<itemID><![CDATA[");
ids = new String[count]; //查询具体某一页的数据
int[] indexbegin = new int[1000000];
int[] indexbegin22 = new int[1000000];
int[] indexbegin33 = new int[1000000];
for(int i=1;i<=count;i++){
indexbegin[i-1] = getIndex(result.getBody(),i,"<itemID>");
indexbegin22[i - 1] = getIndex(result.getBody(), i, "<updateTime>");
indexbegin33[i - 1] = getIndex(result.getBody(), i, "<datatype>");
}
int[] indexend = new int[1000000];
int[] indexend22 = new int[1000000];
int[] indexend33 = new int[1000000];
for(int i=1;i<=count;i++){
indexend[i-1] = getIndex(result.getBody(),i,"</itemID>");
indexend22[i - 1] = getIndex(result.getBody(), i, "</updateTime>");
indexend33[i - 1] = getIndex(result.getBody(), i, "</datatype>");
}
for(int i=0;i<count;i++){
int index1 = 0;
int index2 = 0;
int index41 = 0;
int index42 = 0;
int index31 = 0;
int index32 = 0;
index1 = indexbegin[i];
index2 = indexend[i];
ids[i] = result.getBody().substring(index1+17,index2-3); index41 = indexbegin22[i];
index42 = indexend22[i];
updatetimeStrs[i] = result.getBody().substring(index41 + 21, index42 - 3); index31 = indexbegin33[i];
index32 = indexend33[i];
datatypeStrs[i] = result.getBody().substring(index31 + 19, index32 - 3);
}
}
String returnstr = "";
//存储ids.length条信息
String[] strs = new String[ids.length];
List<ItemVO> listVO = new ArrayList<ItemVO>();
System.out.println("id个数"+ids.length);
for(int i=0;i<ids.length;i++){
if(ids[i]!=null&&!ids[i].equals("undefined")) {
String id = ids[i];
//根据Id获取item商品信息,其中包括
//商品ID 图片 商品名称 店铺名称 当当价库存 商品状态 审核状态 最后修改时间
ItemVO itemVO = new ItemVO();
//审核状态 和最后修改时间去ItemList获取
itemVO.setCheckstate(datatypeStrs[i]);
itemVO.setLastupdatetime(updatetimeStrs[i]);
itemVO.setStorename(storename);
//商品ID 图片 商品名称 店铺名称 当当价库存 商品状态 去Item接口获取
ItemGetRequest request = new ItemGetRequest();
ItemGet get = new ItemGet();
get.setIt(id);
request.setItemsGet(get);
ItemGetResponse re = sdkClient.excute(request); //开始解析单个的商品
String itemInfoStr = re.getBody();
System.out.println("查询到的商品返回信息"+re.getBody());
String itemId = getAttribute(itemInfoStr, "<itemID>", "</itemID>");
itemVO.setStoreId(itemId);
String itemName = getAttribute(itemInfoStr, "<itemName>", "</itemName>");
itemVO.setGoodsname(itemName);
String unitPrice = getAttribute(itemInfoStr, "<unitPrice>", "</unitPrice>");
itemVO.setUnitPrice(Float.parseFloat(unitPrice));
String stockCount = getAttribute(itemInfoStr, "<stockCount>", "</stockCount>");
itemVO.setStockCount(Integer.parseInt(stockCount));
String itemState = getAttribute(itemInfoStr, "<itemState>", "</itemState>");
itemVO.setGoodstate(itemState);
//解析一下图片
int countpic = countStr(re.getBody(), "<pic");
String pistrs = "";
int[] beginpic = new int[countpic];
int[] endpic = new int[countpic];
for (int h = 1; h <= countpic; h++) {
beginpic[h - 1] = getIndex(re.getBody(), h, "<pic");
endpic[h - 1] = getIndex(re.getBody(), h, "</pic");
}
for (int j = 0; j < countpic; j++) {
int begin0 = beginpic[j];
int end0 = endpic[j];
pistrs += re.getBody().substring(begin0 + 15, end0 - 3);
}
itemVO.setPicture(pistrs);
if (i < ids.length - 1)
returnstr += itemVO.toString() + "#";
else
returnstr += itemVO.toString();
}
}
returnstr+="@"+pageSize;
System.out.println("return数据:"+returnstr);
return returnstr;
} public String getAttribute(String src,String pattern1,String pattern2){
String result = null;
int index1 = 0;
int index2 = 0;
index1 =src.indexOf(pattern1);
index2 = src.indexOf(pattern2);
result = src.substring(index1+pattern1.length()+9,index2-3);
return result;
} public static int getIndex(String string, int i, String str) { Matcher slashMatcher = Pattern.compile(str).matcher(string);
int mIdx = 0;
while (slashMatcher.find()) {
mIdx++;
if (mIdx == i) {
break;
}
}
return slashMatcher.start();
} private int countStr(String str,String sToFind) {
int num = 0;
while (str.contains(sToFind)) {
str = str.substring(str.indexOf(sToFind) + sToFind.length());
num ++;
}
return num;
}

  

Java解析XML字符串,取出其中<aaaa><![CDATA[(XXX)]]></aaa>里面的XXX值,也可以使用xml解析的其他方式,这是最简单的字符串解析的更多相关文章

  1. vue :class 可以接收 字符串 数组 和 对象 对象里面的key值 根据true或false 显示不显示

    vue :class 可以接收 字符串 数组 和 对象 对象里面的key值 根据true或false 显示不显示 https://cn.vuejs.org/v2/guide/class-and-sty ...

  2. Java 从 Redis中取出的Json字符串 带斜杠的问题解决方案

    Java 从 Redis中取出的Json字符串 带斜杠的问题: { "code": 200, "message": "成功", " ...

  3. PHP处理XML文档,没有CDATA部分数据处理

    在博客备份时,导出了所有文章,导出是xml文档,文章内容在CDATA部分. 这里介绍下XML中CDATA: 所有 XML 文档中的文本均会被解析器解析.只有 CDATA 区段(CDATA sectio ...

  4. java,url长链接生成短链接,短链接生成器,自定义字符串,对字符串md5混合KEY加密,根据短链接获得key值,不重复的随机数,不重复的随机字符串

    java,url长链接生成短链接,短链接生成器,自定义字符串,对字符串md5混合KEY加密,根据短链接获得key值,不重复的随机数,不重复的随机字符串 package com.zdz.test; im ...

  5. C# 后台解析json,简单方法 字符串序列化为对象,取值

    如果后台是一个JSON的字符串格式如下: string str = "{\"Success\":true,\"Msg\":\"成功!\&qu ...

  6. 简单使用JDOM解析XML

    原文:http://liuwentao.iteye.com/blog/59978 使用JDOM解析XML一.前言JDOM是Breet Mclaughlin和Jason Hunter两大Java高手的创 ...

  7. mybatis 的 dao 接口跟 xml 文件里面的 sql 是如何建立关系的?一步步解析

    序言 在开始正文之前,首先解释Dao接口和XML文件里的SQL是如何一一对应的? 一句话讲完就是:mybatis 会先解析这些xml 文件,通过 xml 文件里面的命名空间 (namespace)跟d ...

  8. json解析出现:java.lang.ClassCastException: net.sf.ezmorph.bean.MorphDynaBean cannot be cast to XXX

    感谢大佬:https://blog.csdn.net/one_ink/article/details/99817676 一.出错原因 当我们利用json解析中的toBean方法时,如果它的属性里面包含 ...

  9. js 解析XML 在Edge浏览器下面 无法准确读到节点属性值

    js 解析XML 在Edge浏览器下面 无法准确读到节点属性值 Dom.documentElement.childNodes[j].attributes[2]  这个是大众写法 在win10的edge ...

随机推荐

  1. nltk词性标注

    将词汇按它们的词性(parts-of-speech,POS)分类以及相应的标注它们的过程被称为词性标注(part-of-speech tagging, POS tagging)或干脆简称标注.词性也称 ...

  2. 简单的权限管理php

    转发自https://www.cnblogs.com/shenzikun1314/p/6604867.html#4262295 首先,要明白的基础理论是用户,角色,权限之间的关系是一对多,还是多对多. ...

  3. Vue-cli项目部署到Nginx

    项目环境: 0. Nginx使用 以windows版为例,下载niginx压缩包并解压到任意目录,双击nginx.exe,在浏览器中访问http://localhost,如果出现Welcome to ...

  4. Django Forms ChoiceField 选项更新问题

    今天使用django的forms,发现单选后台新增数据后,前端表单选项不能刷新 class UserForm(forms.Form): name = forms.ChoiceField( choice ...

  5. Computer: Use the mouse to open the analog keyboard

    Xx_Introduction Please protection,respect,love,"China's Internet Security Act"! For learni ...

  6. 关于useGeneratedKeys的使用

    今天认识到一个新参数 useGeneratedKeys  再这里我记录分享一下 在我们进行两表关联的时候,添加表1Specification 表2Specification_option 表一: 表二 ...

  7. nginx安装及配置访问本地文件

    第一步安装nginx windows可以直接去官网下载,解压就能用 http://nginx.org/en/download.html ubuntu用命令行 sudo apt-get install ...

  8. Nginx日志常见时间变量解析

    $request_time 官方解释:request processing time in seconds with a milliseconds resolution; time elapsed b ...

  9. admin配置与Mysql数据库连接

    admin配置管理数据库的框架:web版的数据库管理页面初始化数据库: python manage.py makemigrations python manage.py migrate启动项目:(创建 ...

  10. pytest系列(二):筛选用例新姿势,mark 一下,你就知道。

    pytest系列(一)中给大家介绍了pytest的特性,以及它的编写用例的简单至极. 那么在实际工作当中呢,我们要写的自动化用例会比较多,不会都放在一个py文件里. 如下图所示,我们编写的用例存放在不 ...