CN104410667B - Conversion method and system of a kind of XML message to JSON messages - Google Patents
Conversion method and system of a kind of XML message to JSON messages Download PDFInfo
- Publication number
- CN104410667B CN104410667B CN201410602656.0A CN201410602656A CN104410667B CN 104410667 B CN104410667 B CN 104410667B CN 201410602656 A CN201410602656 A CN 201410602656A CN 104410667 B CN104410667 B CN 104410667B
- Authority
- CN
- China
- Prior art keywords
- attribute
- node
- xml
- json
- module
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Active
Links
Classifications
-
- H—ELECTRICITY
- H04—ELECTRIC COMMUNICATION TECHNIQUE
- H04L—TRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
- H04L69/00—Network arrangements, protocols or services independent of the application payload and not provided for in the other groups of this subclass
- H04L69/08—Protocols for interworking; Protocol conversion
Landscapes
- Engineering & Computer Science (AREA)
- Computer Security & Cryptography (AREA)
- Computer Networks & Wireless Communication (AREA)
- Signal Processing (AREA)
- Information Transfer Between Computers (AREA)
- Document Processing Apparatus (AREA)
Abstract
The present invention relates to the conversion method and system of a kind of XML message to JSON messages, comprise the following steps:Step 1:XML message to be converted is obtained, XML message is parsed into XML object;Step 2:The attribute of each node in XML object is obtained one by one;Step 3:Check in present node and whether only include content of text or empty element, if it is, performing step 4, otherwise, perform step 5;Step 4:The attribute of present node is modified, modification finishes, and performs step 6;Step 5:The attribute of next node is obtained, performs step 3;Step 6:XML object is converted into JSON objects, and JSON objects are converted into JSON messages.XML format and JSON forms are carried out conversion process automatically, are transparent so to application developer, have been multiplexed former program code, reduce the risk of project by the present invention by the modification of Development Framework.
Description
Technical field
The present invention relates to a kind of message forwarding method, the conversion method of more particularly to a kind of XML message to JSON messages and
System.
Background technology
Company prepares in new project using new Technical Architecture, and the requirement of this Technical Architecture is that web applications in foreground are adjusted
The calling that must be all serviced with background service by ESB, the performance that this layers of ESB will be very high is so required, otherwise, can be by
In the performance of all calling systems of this layer of performance impact of ESB, the project of finally resulting in faces excessive risk, therefore, it is necessary to tries every possible means to carry
High ESB performance.
By way of ESB carries out service call it is called by way of web service at present, the number of calling
It is XML format according to form.Recognize the popular framework style mode in current internet using RESTful by online enquiries
Mode, while data format is JSON forms.Pass through the pressure testing results of technology prototype, it was demonstrated that really using RESTful+
JSON mode is better than web service+XML mode performance.
Therefore, finally by the mode of ESB calling services by the way of RESTful+JSON, RESTful has perhaps at present
More Open Frameworks are supported, are not introduced herein.
The content of the invention
The technical problems to be solved by the invention be to provide it is a kind of according to predetermined form by XML message to JSON messages
Conversion method and system.
The technical scheme that the present invention solves above-mentioned technical problem is as follows:A kind of XML message to JSON messages conversion method,
Comprise the following steps:
Step 1:XML message to be converted is obtained, XML message is parsed into XML object;
Step 2:The attribute of each node in XML object is obtained one by one;
Step 3:Check in present node and whether only include content of text or empty element, if it is, step 4 is performed, otherwise,
Perform step 5;
Step 4:The attribute of present node is modified, modification finishes, and performs step 6;
Step 5:The attribute of next node is obtained, performs step 3;
Step 6:XML object is converted into JSON objects, and JSON objects are converted into JSON messages.
The beneficial effects of the invention are as follows:The present invention is automatically entered XML format and JSON forms by the modification of Development Framework
Row conversion process, it is transparent so to application developer, has been multiplexed former program code, reduce the risk of project.
On the basis of above-mentioned technical proposal, the present invention can also do following improvement.
Further, the step 4 specifically includes following steps:The attribute of present node is judged, if the attribute of node is
Character string type, the text data type by the attribute definition of the node for not resolved device parsing, if the attribute of node is
Double types and the value of the node are any class of sky element or nodal community position in addition to character string type and double
It is numeric type by the attribute definition of the node when value of type and node is sky element.
Further, it is specially that the category for obtaining each node in XML object one by one is circulated using Document in the step 2
Property.
Further, the attribute of node includes int, long, string and double.
Further, a kind of XML message to JSON messages converting system, including obtain parsing module, first obtain attribute
Module, check that module, modified module, second obtain attribute module and modular converter;
The acquisition parsing module, obtains XML message to be converted, and XML message is parsed into XML object;
Described first obtains attribute module, for obtaining the attribute of each node in XML object one by one;
The inspection module, for checking in present node whether only include content of text or empty element, if it is, performing
Modified module, otherwise, perform second and obtain attribute module;
The modified module, for the attribute of present node to be modified, modification finishes, and performs modular converter;
Described second obtains attribute module, for obtaining the attribute of next node, performs and checks module;
The modular converter, for XML object to be converted into JSON objects, and JSON objects are converted into JSON messages.
Further, the modified module is specifically used for the attribute for judging present node, if the attribute of node is character string
Type, the text data type by the attribute definition of the node for not resolved device parsing, if the attribute of node is double
Type and the value of the node are any type and section of sky element or nodal community position in addition to character string type and double
It is numeric type by the attribute definition of the node when value of point is sky element.
Further, described first obtain in attribute module specially using in Document circulations one by one acquisition XML object
The attribute of each node.
Further, the attribute of node includes int, long, string and double.
Brief description of the drawings
Fig. 1 is the inventive method flow chart of steps;
Fig. 2 is present system structure chart.
In accompanying drawing, the list of parts representated by each label is as follows:
1st, parsing module is obtained, 2, first obtains attribute module, and 3, check module, 4, modified module, 5, second obtains category
Property module, 6, modular converter.
Embodiment
The principle and feature of the present invention are described below in conjunction with accompanying drawing, the given examples are served only to explain the present invention, and
It is non-to be used to limit the scope of the present invention.
As shown in figure 1, it is the inventive method flow chart of steps;Fig. 2 is present system structure chart.
Embodiment 1
A kind of XML message comprises the following steps to the conversion method of JSON messages:
Step 1:XML message to be converted is obtained, XML message is parsed into XML object;
Step 2:The attribute of each node in XML object is obtained one by one;
Step 3:Check in present node and whether only include content of text or empty element, if it is, step 4 is performed, otherwise,
Perform step 5;
Step 4:The attribute of present node is modified, modification finishes, and performs step 6;
Step 5:The attribute of next node is obtained, performs step 3;
Step 6:XML object is converted into JSON objects, and JSON objects are converted into JSON messages.
The step 4 specifically includes following steps:The attribute of present node is judged, if the attribute of node is character string class
Type, the text data type by the attribute definition of the node for not resolved device parsing, if the attribute of node is double classes
The value of type and the node is any type and node of sky element or nodal community position in addition to character string type and double
Value when being sky element, be numeric type by the attribute definition of the node.
It is specially the attribute for obtaining each node in XML object one by one using Document circulations in the step 2.
The attribute of node includes int, long, string and double.
A kind of XML message to JSON messages converting system, including obtain parsing module 1, first obtain attribute module 2,
Check that module 3, modified module 4, second obtain attribute module 5 and modular converter 6;
The acquisition parsing module 1, obtains XML message to be converted, XML message is parsed into XML object;
Described first obtains attribute module 2, for obtaining the attribute of each node in XML object one by one;
The inspection module 3, for checking in present node whether only include content of text or empty element, if it is, holding
Row modified module 4, otherwise, perform second and obtain attribute module 5;
The modified module 4, for the attribute of present node to be modified, modification finishes, and performs modular converter 6;
Described second obtains attribute module 5, for obtaining the attribute of next node, performs and checks module 3;
The modular converter 6, for XML object to be converted into JSON objects, and JSON objects are converted into JSON messages.
Modified module 4 described in modular converter 6 is specifically used for the attribute for judging present node, if the attribute of node is character
String type, the text data type by the attribute definition of the node for not resolved device parsing, if the attribute of node is
Double types and the value of the node are any class of sky element or nodal community position in addition to character string type and double
It is numeric type by the attribute definition of the node when value of type and node is sky element.
It is specially to obtain in XML object each to save one by one using Document circulations in the first acquisition attribute module 2
The attribute of point.
The attribute of node includes int, long, string and double.
The present invention also provides a kind of JSON messages to the conversion method of XML message, comprises the following steps:
Step a:JSON character strings are changed into generation JSON objects;
Step b:According to the empty XML object of JSON objects generation;
Step c:According to the XML object of sky, the parameter in JSON objects is stored in the XML object of sky, obtains character
String XML.
Under using the framework style based on RESTful+JSON, because a large amount of programs write in the past are all based on web
Service+XML mode.In order to avoid Technical Architecture change to cause a large amount of modifications of application program, it is necessary to which foreground is adjusted as far as possible
JSON forms are converted into the incoming XML of service form, JSON forms reconvert will be returned and return XML format, therefore, this is just related to
And to the mutual conversion between XML format and JSON forms.Final purpose is exactly transparent to application program, reduces project wind
Danger.
The analysis changed between XML format and JSON forms and implementation process:
1. the XML format and standard JSON forms of the company of analysis
The XML format node attribute of agreement has 4 types, is int, long, string, double respectively, specific case
Under such as:
<BATCH_TYPE type=" int ">1</BATCH_TYPE>
<TEMPLATE_ID type=" long ">1</TEMPLATE_ID>
<AUTO_CONFIRM type=" string ">N</AUTO_CONFIRM>
<SALE_PRICE type=" double ">100</SALE_PRICE>
In the JSON forms of standard character string must be caused with double quotation marks come, remaining need not, such as character string " a ".
Based on above premise, addition double quotation marks is needed to be converted into JSON type=in XML format " string " data
Form, namely need individually parsing XML file, for nodal community be type=" string " data addition double quotation marks turn
Change JSON form into.Other kinds of data can directly use the raw value in XML.Conversely, turn from JSON forms
Gaining XML format also has above-mentioned requirement.
2. XML format is converted into JSON forms
First, analysis default XML format is converted into JSON forms are what a kind of situations, specific data format fragment citing
It is as follows:
XML format data:<POOL_ID type=" string ">11</POOL_ID>
JSON data after conversion:
And the result that actually we want is " POOL_ID ":"11".Therefore, it is necessary to which XML format first is carried out into one
The process of " pretreatment ", i.e., the attribute of XML data format is removed, become<POOL_ID>11</POOL_ID>Form.
Secondly, for the ease of controlling transfer process, org.json storehouses is have selected as conversion API, be implemented as follows:
XML xml=new XML ();
JSONObjectjso=xml.toJSONObject (xmlstr);
String jsonstr=jso.toString ();
1) org.json storehouses are increased income, it is possible to achieve the conversion of java objects and json objects, referring to:http://
www.json.org/java/index.html.The conversion between json and xml can also be realized.
2) above code API is that org.json storehouses provide.
3.JSON forms are converted into XML format
It is implemented as follows:
JSONObjectjo=new JSONObject (result);
XML xml=new XML ();
String xmlstr=xml.toString (jo);
Above code library is all that org.json storehouses provide.
The implementation steps of the present invention are as follows:
1. environmental preparation
The jar bags that xml and json is changed introduce engineering project
2. xml forms are converted into json forms
String jsonStr=Xml2JsonForSitech.xmlToJson (xmlStr)
3. json forms are converted into xml forms
JSONObjectjo=new JSONObject (jsonStr);
XML xml=new XML ();
String xmlstr=xml.toString (jo).
The foregoing is only presently preferred embodiments of the present invention, be not intended to limit the invention, it is all the present invention spirit and
Within principle, any modification, equivalent substitution and improvements made etc., it should be included in the scope of the protection.
Claims (6)
1. a kind of XML message is to the conversion method of JSON messages, it is characterised in that comprises the following steps:
Step 1:XML message to be converted is obtained, XML message is parsed into XML object;
Step 2:The attribute of each node in XML object is obtained one by one;
Step 3:Check in present node and whether only include content of text or empty element, if it is, performing step 4, otherwise, perform
Step 5;
Step 4:Judge the attribute of present node, if the attribute of node is character string type, be by the attribute definition of the node
The text data type of not resolved device parsing, if the value that the attribute of node is double types and the node be sky element,
Or nodal community is when for any type in addition to character string type and double and the value of node being sky element, by the node
Attribute definition be numeric type, perform step 6;
Step 5:The attribute of next node is obtained, performs step 3;
Step 6:XML object is converted into JSON objects, and JSON objects are converted into JSON messages.
2. XML message according to claim 1 is to the conversion method of JSON messages, it is characterised in that:Have in the step 2
Body is the attribute for obtaining each node in XML object one by one using Document circulations.
3. XML message according to claim 1 is to the conversion method of JSON messages, it is characterised in that:The attribute bag of node
Include int, long, string and double.
4. a kind of XML message is to the converting system of JSON messages, it is characterised in that:Including obtaining parsing module (1), the first acquisition
Attribute module (2), check that module (3), modified module (4), second obtain attribute module (5) and modular converter (6);
The acquisition parsing module (1), obtains XML message to be converted, XML message is parsed into XML object;
Described first obtains attribute module (2), for obtaining the attribute of each node in XML object one by one;
The inspection module (3), for checking in present node whether only include content of text or empty element, if it is, performing
Modified module (4), otherwise, perform second and obtain attribute module (5);
The modified module (4), for judging the attribute of present node, if the attribute of node is character string type, this is saved
The text data type that the attribute definition of point parses for not resolved device, if the attribute of node is double types and the node
Value be that sky element or nodal community for any type in addition to character string type and double and the value of node are empty members
It is numeric type by the attribute definition of the node when plain;
Described second obtains attribute module (5), for obtaining the attribute of next node, performs and checks module (3);
The modular converter (6), for XML object to be converted into JSON objects, and JSON objects are converted into JSON messages.
5. XML message according to claim 4 is to the converting system of JSON messages, it is characterised in that:Described first obtains
It is specially the attribute for obtaining each node in XML object one by one using Document circulations in attribute module (2).
6. XML message according to claim 4 is to the converting system of JSON messages, it is characterised in that:The attribute bag of node
Include int, long, string and double.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201410602656.0A CN104410667B (en) | 2014-10-31 | 2014-10-31 | Conversion method and system of a kind of XML message to JSON messages |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201410602656.0A CN104410667B (en) | 2014-10-31 | 2014-10-31 | Conversion method and system of a kind of XML message to JSON messages |
Publications (2)
Publication Number | Publication Date |
---|---|
CN104410667A CN104410667A (en) | 2015-03-11 |
CN104410667B true CN104410667B (en) | 2017-12-12 |
Family
ID=52648265
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN201410602656.0A Active CN104410667B (en) | 2014-10-31 | 2014-10-31 | Conversion method and system of a kind of XML message to JSON messages |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN104410667B (en) |
Families Citing this family (16)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN105069116A (en) * | 2015-08-11 | 2015-11-18 | 北京思特奇信息技术股份有限公司 | Method and system for converting json string into array |
CN105117379A (en) * | 2015-09-11 | 2015-12-02 | 广州华多网络科技有限公司 | Automatic program text converting method and device |
CN105868374A (en) * | 2016-04-01 | 2016-08-17 | 浪潮电子信息产业股份有限公司 | Method for converting xml data into json data based on javascript |
CN109117207B (en) * | 2017-06-21 | 2021-11-23 | 深圳联友科技有限公司 | Data processing method of business process model |
CN107463356A (en) * | 2017-08-17 | 2017-12-12 | 北京云纵信息技术有限公司 | The execution method and apparatus of flow of task |
CN107918542B (en) * | 2017-11-23 | 2022-02-08 | 北京恒华伟业科技股份有限公司 | Character string generation method and device |
CN109358845B (en) * | 2017-12-27 | 2021-11-09 | 深圳Tcl新技术有限公司 | Method, tool and storage medium for writing JS code based on XMPP protocol |
CN109976713A (en) * | 2017-12-28 | 2019-07-05 | 中国电信股份有限公司 | Script processing method and device for Service Assembly |
CN108628634A (en) * | 2018-05-09 | 2018-10-09 | 平安普惠企业管理有限公司 | Message configuration method, device, computer equipment and storage medium |
CN111626018B (en) * | 2019-02-28 | 2023-07-28 | 北京数聚鑫云信息技术有限公司 | Method and device for constructing JSON formatted data |
CN111597390A (en) * | 2020-03-17 | 2020-08-28 | 用友网络科技股份有限公司 | Data format conversion framework |
CN112434491A (en) * | 2020-11-27 | 2021-03-02 | 杭州信雅达泛泰科技有限公司 | HTML table parsing system and parsing method thereof for JSON |
CN115378854B (en) * | 2022-07-07 | 2023-11-14 | 浙江众合科技股份有限公司 | Binary data message modification method based on custom JSON rule |
CN115292624B (en) * | 2022-10-08 | 2023-08-04 | 成都同步新创科技股份有限公司 | General message processing method and device based on HTTP protocol |
CN116882366B (en) * | 2023-09-07 | 2024-01-19 | 粤芯半导体技术股份有限公司 | Structure conversion method and device for marking type message, electronic product and medium |
CN117454846B (en) * | 2023-12-25 | 2024-03-26 | 广州睿帆科技有限公司 | Method for converting XSD file into JSON file |
Citations (4)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN102185901A (en) * | 2011-04-19 | 2011-09-14 | 北京神州数码思特奇信息技术股份有限公司 | Client message conversion method |
CN102279752A (en) * | 2011-08-31 | 2011-12-14 | 北京华电万通科技有限公司 | Device and method for rendering ultra-large scene in real time based on Web three-dimension (3D) |
CN102394874A (en) * | 2011-10-26 | 2012-03-28 | 深圳天源迪科信息技术股份有限公司 | TUXEDO (Transaction for UNIX has been Extended for Distributed Operation) data protocol conversion method |
CN103379127A (en) * | 2012-04-12 | 2013-10-30 | 阿里巴巴集团控股有限公司 | Data interaction method and apparatus between server and client |
-
2014
- 2014-10-31 CN CN201410602656.0A patent/CN104410667B/en active Active
Patent Citations (4)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN102185901A (en) * | 2011-04-19 | 2011-09-14 | 北京神州数码思特奇信息技术股份有限公司 | Client message conversion method |
CN102279752A (en) * | 2011-08-31 | 2011-12-14 | 北京华电万通科技有限公司 | Device and method for rendering ultra-large scene in real time based on Web three-dimension (3D) |
CN102394874A (en) * | 2011-10-26 | 2012-03-28 | 深圳天源迪科信息技术股份有限公司 | TUXEDO (Transaction for UNIX has been Extended for Distributed Operation) data protocol conversion method |
CN103379127A (en) * | 2012-04-12 | 2013-10-30 | 阿里巴巴集团控股有限公司 | Data interaction method and apparatus between server and client |
Non-Patent Citations (2)
Title |
---|
Convert XML to JSON in PHP;Senthil Nathan;《IBM developerWorks》;20070605;全文 * |
利用JSON降低XML数据冗余的研究;陈玮;《计算机应用与软件》;20120915;第189-190页 * |
Also Published As
Publication number | Publication date |
---|---|
CN104410667A (en) | 2015-03-11 |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
CN104410667B (en) | Conversion method and system of a kind of XML message to JSON messages | |
CN110889270B (en) | Form page document generation method, form page rendering method and device | |
CN109379326B (en) | XML message rule checking method, equipment and storage medium | |
CN102831190B (en) | A kind of method that CML files are browsed in low side devices | |
CN102982010A (en) | Method and device for abstracting document structure | |
CN104331366A (en) | Test case design efficiency promotion method and system | |
CN103942137A (en) | Browser compatibility testing method and device | |
EP2782025A2 (en) | Communication apparatus, method, system and program for encoding/decoding structured documents | |
CN104899015A (en) | Service component management system and method thereof | |
CN104360937A (en) | Intelligent parameter checking device | |
CN103279541B (en) | A kind of method reducing web browser load and device thereof | |
CN104717179A (en) | Communication service processing method and device | |
CN101382893A (en) | On-line assembling method for component based on Web service | |
CN103677788A (en) | Reuse framework generating method, device and application system based on J2EE distributed architecture | |
CN108664546B (en) | XML data structure conversion method and device | |
CN105630563A (en) | Dynamic model configuration method of relay protection device specification based on XML (Extensible Markup Language) | |
CN105069116A (en) | Method and system for converting json string into array | |
CN112199556A (en) | Automatic XML Schema file format conversion method, system and related equipment | |
US20100115129A1 (en) | Conditional processing method and apparatus | |
CN101944133B (en) | XML metadata objectification analysis method and system | |
CN102508887A (en) | System and method for resolving digital television interaction service markup language | |
CN102306163A (en) | Dynamic integration technology based on B2B (business to business) platform | |
CN111104122B (en) | Method for mapping xml service logic to java service logic | |
CN202143092U (en) | Message processing system | |
CN103561113A (en) | Web Service interface generating method and device |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
C06 | Publication | ||
PB01 | Publication | ||
C10 | Entry into substantive examination | ||
SE01 | Entry into force of request for substantive examination | ||
GR01 | Patent grant | ||
GR01 | Patent grant |