您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

解析XML和JSON内容的一点技巧的实例代码分享

2025/5/25 18:01:34发布20次查看
解析xml和json内容的一点技巧概述在没有统一标准的情况下,一个系统对接多个外部系统往往会遇到请求接口响应数据异构的情况,有可能返回的是xml,也有可能返回
json。除了返回类型不同,内容结构也不尽相同。以xml类型为例,
接口1返回内容
<root> <bizkey>16112638767472747178067</bizkey> <returnmsg>ok</returnmsg> <returncode>200</returncode> ... </root>
接口2返回内容
<root> <bid>16112638767472747178068</bid> <note>成功</note> <returnstatus>1</returnstatus> ... </root>
如果在我们系统中为每种格式的内容针对处理显然是不合理的,上面的内容中我们只是关心三种信息,分别是业务id、状态值和描述信息,那么可不可以抽象这三种信息,
获得这些信息后再进行业务逻辑处理。
解析xml和json根据业务抽象我们需要从xml或者json内容中获得三种信息,我们这里将会使用xpath和jsonpath的方式来解析。比如获得接口1的重要信息,
我们可以设定三个xpath表达式,
{ bid: "/root/bizkey", code: "/root/returncode", description: "/root/returnmsg" }
bid,code和description对应我们系统自己定义的字段名。
解析json内容也是同理的,只不过定义的是jsonpath表达式。
分两步走处理数据内容假设我们从原始的xml和json数据中获得了bid,code和description信息,
从接口1获得
{ bid: '16112638767472747178067', code: '200', description: 'ok' }
从接口2获得
{ bid: '16112638767472747178068', code: '1', description: '成功' }
假设我们从接口1文档获知状态值200表示请求成功,从接口2文档获知状态值1表示请求成功,虽然他们都表示请求成功,但是我们还是不能
把他们原原本本地保存到我们的业务相关表中(当然这些响应数据还是需要保存到另外的记录表中的,至少方便排查问题)。
假设我们的业务相关表是这样设计的
字段名类型描述
bid string 业务id
code int 状态值,0=初始,1=请求中,2=成功,3=失败
description string 描述
因此,我们还必须定义规则把接口1返回的状态值200转换为我们系统的2,把接口2返回的状态值1转换为我们系统的2。
总结一下,两步走解析xml和json数据内容
根据xpath或者jsonpath表达式解析获得重要信息
根据规则转换状态值
第一步解析数据获得重要信息以xml为例,
public class xmlparseutils { private documentbuilderfactory dbfactory = documentbuilderfactory.newinstance(); private xpathfactory xpathfactory = xpathfactory.newinstance(); /** * * @param param 数据内容 * @param paths 表达式 * @return * @throws exception */ public map<string,object> parse(string param, map<string,string> paths) throws exception{ inputsource inputsource = new inputsource(new stringreader(param)); document document = dbfactory.newdocumentbuilder().parse(inputsource); map<string,object> map = maps.newhashmap(); for(string key : paths.keyset()) { xpath xpath = xpathfactory.newxpath(); node node = (node) xpath.evaluate(paths.get(key), document, xpathconstants.node); if(node == null) { throw new exception("node not found, xpath is " + paths.get(key)); } map.put(key, node.gettextcontent()); } return map; } }
parse函数的返回类型也可以是map<string,string>,暂且用map<string,object>。
第二步根据规则转换状态值这一步稍稍有点麻烦,不过我们先不考虑代码实现,反正你能想到的可能别人已经帮你实现了。首先我们根据接口文档定义规则,写出规则表达式(或者其他的什么),
又是表达式。假设接口1的返回的状态值比较简单,只有200表示成功,其他情况都是失败,那么我们可以这样定义规则,
code.equals("200") ? 2: 3
或者
<#if code == "200"> 2 <#else> 3 <#/if>

亦或者
function handle(arg) { if(arg == 200) { return 2; } return 3; } handle(${code})
以上根据同一份文档定义了三种不同类型的状态值转换规则,肯定需要三种不同的实现。下面一一说明,
三目表达式code.equals("200") ? 2: 3是一个三目表达式,我们将使用jexl引擎来解析,利用第一步解析数据获得重要信息的结果,我们可以这样做
public object evaluatebyjexl(string expression, map<string,object> context) { jexlengine jexl = new jexlbuilder().create(); jexlexpression e = jexl.createexpression(expression); jexlcontext jc = new mapcontext(context); return e.evaluate(jc); }
freemarker模板<#if code == "200"> 2 <#else> 3 <#/if>

处理这段模板我们可以这么做
/** * * @param param freemarker模板 * @param context * @return * @throws exception */ public string render(string param, map<string,object> context) throws exception { configuration cfg = new configuration(); stringtemplateloader stringloader = new stringtemplateloader(); stringloader.puttemplate("mytemplate",param); cfg.settemplateloader(stringloader); template template = cfg.gettemplate("mytemplate","utf-8"); stringwriter writer = new stringwriter(); template.process(context, writer); return writer.tostring(); }
如果freemarker模板比较复杂,从模板预编译成template可能会消耗更多的性能,就要考虑把template缓存起来。
javascript代码段function handle(arg) { if(arg == 200) { return 2; } return 3; } handle(${code})
这段js代码中存在${code},首先它需要使用freemarker渲染得到真正的handle方法的调用参数,然后
public object evaluate(string expression) throws exception { scriptenginemanager manager = new scriptenginemanager(); scriptengine engine = manager.getenginebyname("javascript"); return engine.eval(expression); }
scriptenginemanager的性能估计不太乐观,毕竟是一个语言的引擎。
不同转换规则实现的比较类型实现优点缺点
三目表达式 jexl 简单(easy) 简单(simple)
freemarker模板 freemarker -- --
javascript代码段 freemarker + scriptengine 直观 过程复杂,性能问题
看起来freemarker是一个不错的选择。
至此两步走小技巧已经实现了,都是利用了现成的代码实现。
或许我们会这样的挑战,在做状态值转换时需要知道当前系统某个业务状态值的情况,
此时freemarker表达式可能是这样的,
<# assign lastcode = getlastcode(code)> <#if lastcode == "2"> 2 <#elseif code == "200"> 2 <#else> 3 <#/if>
这里我们可以使用freemarker的特性,自定义java函数或工具类,在模板中调用。
以上就是解析xml和json内容的一点技巧的实例代码分享的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product