直接贴一段业务代码,这段代码是解析请求返回的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. TensorFlow实现图像卷积并可视化示例

    图片尺寸要自己修改. 看起来好像没啥意思,不知道下一步能干什么,先卷了再说.由于weights是随机生成的(tf.random_normal作用:用于从服从指定正太分布的数值中取出随机数),所以每次卷 ...

  2. Laravel6实现第三方 微信登录

    目前很多的网站中都会存在很多的交互功能,从而降低用户的操作难度,特此带来微信的第三方登录的项目实战功能开发.对于本实例中的开发内容,就不在使用原生的内容,而是直接使用别人写好的封装的类库. 1. 安装 ...

  3. JS---DOM---点击操作---part2---14个案例

    案例1:点击按钮禁用文本框 <input type="button" value="禁用文本框" id="btn" /> < ...

  4. BlockStack常见词语

    Browser: 用户用来浏览并使用基于 blockstack 网络开发的 app. CLI: Cli 工具用来管理个人的 blockstack id. blockstack.js (and othe ...

  5. iOS中UICollectionView添加头视图

    参考链接:https://www.jianshu.com/p/ef57199bf34a 找了一堆的博客,写的都少了很重要的一步. //引入头部视图 -(UICollectionReusableView ...

  6. RabbitMQ Win10安装

    RabbitMQ是消息对列,主要是用于做消息代理.本质上说,它接受来自生产者的信息,并将它们传递给消费者.在两者之间,   它可以根据你给它的路由,缓冲规则有选择地进行传递消息.采用Erlang语言开 ...

  7. 复杂的POI导出Excel表格(多行表头、合并单元格)

    poi导出excel有两种方式: 第一种:从无到有的创建整个excel,通过HSSFWorkbook,HSSFSheet HSSFCell, 等对象一步一步的创建出工作簿,sheet,和单元格,并添加 ...

  8. Python 的字符编码

    配置: Python 2.7 + Sublime Text 2 + OS X 10.10 本文意在理清各种编码的关系并以此解决 Python 中的编码问题. 1 编码基本概念 只有先了解字符表.编码字 ...

  9. Unity3D 使用Socket处理数据并将数据 在UGUI、NGUI上显示出来

    Unity3d 不支持C#的线程直接调用Unity3D 主线程才能实现的功能.例如:给UGUI text 赋值.改变Color值等.怎样解决这个问题呢?使用一个Loom脚本. 按照惯例贴上代码. 首先 ...

  10. Vant-Weap通过事件获取值

    van-field框的使用 和通过事件获取值 <van-cell-group> <van-field value="{{username}}" label=&qu ...