Home >Backend Development >XML/RSS Tutorial >Example code sharing of some techniques for parsing XML and JSON content
In the absence of a unified standard When a system connects to multiple external systems, it often encounters heterogeneous response data from the request interface. It may return XML or
JSON. In addition to the different return types, the content structure is also different. Taking the XML type as an example,
Interface 1 returns content
<root> <bizKey>16112638767472747178067</bizKey> <returnMsg>OK</returnMsg> <returnCode>200</returnCode> ... </root>
Interface 2 returns content
<root> <bid>16112638767472747178068</bid> <note>成功</note> <returnStatus>1</returnStatus> ... </root>
It is obviously unreasonable to process each format of content in our system. In the above content, we only care about three types of information, namely business ID, status value and description information. Can we abstract these three types of information?
After obtaining this information, we can perform business logic processing.
According to business abstraction, we need to obtain three types of information from XML or JSON content. We will use XPath and JSONPath to parse here. . For example, to obtain important information about interface 1,
we can set three XPath expressions,
{ bid: "/root/bizKey", code: "/root/returnCode", description: "/root/returnMsg" }
bid
, code
and description
Corresponds to the field name defined by our system.
The same goes for parsing JSON content, except that the JSONPath expression is defined.
Suppose we obtain bid
,code
and from the original XML and JSON data description
Information,
obtained from interface 1
{ bid: '16112638767472747178067', code: '200', description: 'OK' }
obtained from interface 2
{ bid: '16112638767472747178068', code: '1', description: '成功' }
Assume we get the status value from the interface 1 document 200
indicates that the request was successful , we learned from the interface 2 document that the status value 1
indicates that the request was successful. Although they all indicate that the request was successful, we still cannot
save them intact into our business-related tables (of course these response data It still needs to be saved in another record table, at least to facilitate troubleshooting).
Assume that our business-related tables are designed like this
Type | Description | ||
---|---|---|---|
string | Business ID | ||
int | Status value, 0=initial, 1=requesting, 2=success, 3=failure | ||
string | Description |
类型 | 实现 | 优点 | 缺点 |
---|---|---|---|
三目表达式 | 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函数或工具类,在模板中调用。
The above is the detailed content of Example code sharing of some techniques for parsing XML and JSON content. For more information, please follow other related articles on the PHP Chinese website!