CN105868374A - Method for converting xml data into json data based on javascript - Google Patents
Method for converting xml data into json data based on javascript Download PDFInfo
- Publication number
- CN105868374A CN105868374A CN201610198153.0A CN201610198153A CN105868374A CN 105868374 A CN105868374 A CN 105868374A CN 201610198153 A CN201610198153 A CN 201610198153A CN 105868374 A CN105868374 A CN 105868374A
- Authority
- CN
- China
- Prior art keywords
- data
- node
- xml
- stored
- json
- 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.)
- Pending
Links
- 238000000034 method Methods 0.000 title claims abstract description 52
- 238000006243 chemical reaction Methods 0.000 claims abstract description 5
- 238000007405 data analysis Methods 0.000 abstract 1
- 230000005540 biological transmission Effects 0.000 description 1
- 238000010586 diagram Methods 0.000 description 1
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F16/00—Information retrieval; Database structures therefor; File system structures therefor
- G06F16/80—Information retrieval; Database structures therefor; File system structures therefor of semi-structured data, e.g. markup language structured data such as SGML, XML or HTML
- G06F16/84—Mapping; Conversion
- G06F16/88—Mark-up to mark-up conversion
Landscapes
- Engineering & Computer Science (AREA)
- Theoretical Computer Science (AREA)
- Data Mining & Analysis (AREA)
- Databases & Information Systems (AREA)
- Physics & Mathematics (AREA)
- General Engineering & Computer Science (AREA)
- General Physics & Mathematics (AREA)
- Information Retrieval, Db Structures And Fs Structures Therefor (AREA)
Abstract
The invention discloses a method for converting xml data into json data based on javascript, belonging to the technical field of data format conversion; the method analyzes the xml object data by using the operation in the javascript through a recursive calling method, and then reassigns the original data to json object data which is provided for a module needing data, thereby achieving the purpose of changing the data format. The method is simple to operate, and the problem of object data format in data analysis is solved. The method is suitable for browsers of different versions, and is particularly suitable for the Web field of JavaScript development.
Description
Technical field
The present invention discloses a kind of method that xml data based on javascript transfer json data to, belongs to data form
Switch technology field.
Background technology
Two kinds of main data transfer mode xml modes and json mode, json phase is had at present in page data transmits
Compared with xml, the volume of data is little, the speed of transmission faster, the most convenient with JavaScript, it is easier at parsing
Reason.But owing to json is later for rise time, in the system that some are old, back-end data form is using xml form, and uses always
When background system data originally do New function exploitation, if all xml data being converted to json data in rear end, work
Amount is big, and the most loaded down with trivial details.Although currently having some methods to data structure manipulations, but xml nodal community can not be solved
Analysis, or resolve unreasonable.After parsing between xml structure damage.It is not directly applicable in plug-in unit.
And the invention provides a kind of method that xml data based on javascript transfer json data to, utilize
To operation in javascript, by recursive call method, xml object data is resolved, then by former data again assignment
For json object data, it is provided that give the module needing data, thus reach the purpose in the change of data form.The method operation letter
Single, solve the object data format problem in data parsing.It is applicable to the browser of different editions, is particularly suited for
The Web field of JavaScript exploitation.
Summary of the invention
The present invention is directed to the problems of the prior art, it is provided that a kind of xml data based on javascript transfer json number to
According to method, web develop in xml property value can well be processed, transfer xml data object to json data pair
As.
The concrete scheme that the present invention proposes is:
A kind of method that xml data based on javascript transfer json data to:
Xml data parsing to input becomes xml object data,
Set up data parsing event, incoming new Object and xml object data,
Judge whether root node contains property value according to incoming new Object and xml object data, property value processed,
It is stored in new Object with the form of child node,
Obtain root node value, it may be judged whether for sky, nodal value is stored in back end, it is thus achieved that the child node of root node, it is judged that son
Data are stored in new Object according to result by the number of node,
Data after parsing return with json object form.
Judge whether the number of child node, cycle criterion child node contain next stage node, if afterbody node,
Data are all stored among new Object.
Whether cycle criterion contains next stage node, if child node contains next stage node, creates array or number of objects
Data are stored in new Object by group successively, until having circulated all nodes.
Described method, concretely comprises the following steps
Obtain the xml data needing to represent;
Judge whether incoming data are empty, create the object that will return;
Create conversion method, process the value of nodal community;
Cycle criterion processes the data with or without next stage node, and data are inserted object;
Data are inserted object by the data of circular recursion node, return final object.
Usefulness of the present invention is:
The inventive method: the xml data parsing to input becomes xml object data, sets up data parsing event, incoming new Object
And xml object data, judge whether root node contains property value according to incoming new Object and xml object data, to attribute
Value processes, and is stored in new Object with the form of child node, obtains root node value, it may be judged whether for sky, deposited by nodal value
Enter back end, it is thus achieved that the child node of root node, it is judged that the number of child node, according to result, data are stored in object, resolve it
After data with json object form return;
Utilize the operation in javascript, by recursive call method, xml object data is resolved, then by former data
Again json object data it is entered as, it is provided that give the module needing data, thus reach the purpose in the change of data form.The party
Method is simple to operate, solves the object data format problem in data parsing.It is applicable to the browser of different editions, especially suitable
Web field in JavaScript exploitation.
Accompanying drawing explanation
Fig. 1 the inventive method steps flow chart schematic diagram.
Detailed description of the invention
A kind of method that xml data based on javascript transfer json data to:
Xml data parsing to input becomes xml object data,
Set up data parsing event, incoming new Object and xml object data,
Judge whether root node contains property value according to incoming new Object and xml object data, property value processed,
It is stored in new Object with the form of child node,
Obtain root node value, it may be judged whether for sky, nodal value is stored in back end, it is thus achieved that the child node of root node, it is judged that son
Data are stored in new Object according to result by the number of node,
Data after parsing return with json object form.
Utilizing said method, in conjunction with accompanying drawing, the present invention will be further described.
In said method, to obtaining root node value, it may be judged whether for sky, nodal value is stored in back end, it is thus achieved that root saves
Data are stored in new Object according to result by the child node of point, it is judged that the number of child node:
Wherein judge whether the number of child node, cycle criterion child node contain next stage node, if afterbody node,
Data are all stored among new Object;
If child node contains next stage node, create array or object array, successively data are stored in new Object, until
All nodes are circulated.
According to above-mentioned method, concrete operation step is as follows:
The xml data represented by the ajax method acquisition needs of Jquery:
function testxmlAjax() {
$.ajax({
Type: " post ", the mode of // data acquisition
Url: " js/nodes1000.xml ", the address of // access
DataType:'xml', the parameter type of // acceptance
ProcessData:false, // parameter does not carry out serializing process
{ // call error performs function to error:function (data)
Alert (" call and make mistakes ");
},
Success:function (data) // call successfully acquisition xml categorical data
var result = toJson(data);// call inventive method xml categorical data is converted into json
}
});
}
Judge whether incoming data are empty, that establishment will return object:
function toJson(obj) {
if (obj == null) return null;// determine whether sky, directly return for sky
var retObj = new Object;// new Object, storing and resolving data out
buildObjectNode(retObj, obj);// call parsing according to method, conversion data form
return retObj;// return the data being parsed
... ... ...
}
Create conversion method, the value of process nodal community:
// by incoming to the xml data of acquisition and the object created
function buildObjectNode(cycleOBJ,elNode){
The attribute of // acquisition node
var nodeAttr = elNode.attributes;
if (nodeAttr != null) {
if (nodeAttr.length && cycleOBJ == null)
cycleOBJ = new Object;
for (var i = 0; i < nodeAttr.length; i++) {
// nodal community is stored in object
cycleOBJ[nodeAttr[i].name] = nodeAttr[i].value;
}
}
... ... ...
Cycle criterion processes with or without the data of next stage node, and data insert object:
var nodeText = "text";
Whether // decision node value is empty
if (elNode.text == null)
nodeText = "textContent";
// definition child node
var nodeChilds = elNode.childNodes;
if (nodeChilds != null) {
if(nodeChilds.length && cycleOBJ == null) cycleOBJ = new Object;
// circulation son node number evidence
for (var i = 0; i < nodeChilds.length; i++) {
if (nodeChilds[i].tagName != null) {
// process the data that next stage node is sky
if(nodeChilds[i].childNodes[0]!=null&&nodeChilds
[i].
childNodes.length <= 1 && (nodeChilds[i].childNodes[0].
nodeType== 3|| nodeChilds[i].childNodes[0].nodeType == 4))
{
// process the data that nodal value is sky
if (cycleOBJ[nodeChilds[i].tagName] == null) {
cycleOBJ[nodeChilds[i].tagName] = nodeChilds[i][nodeText];
}else{
// node is not stored in the (typeof (cycleOBJ [nodeChilds [i] of if in newly-built object for empty data
.tagName])=="object"&&cycleOBJ
[nodeChilds[i].tagName].length) {
cycleOBJ[nodeChilds[i].tagName][cycleOBJ[nodeChilds[i].tagName].length] =
nodeChilds[i][nodeText];} else {
cycleOBJ[nodeChilds[i].tagName] = [cycleOBJ[nodeChilds[i]
.tagName]];
cycleOBJ[nodeChilds[i].tagName][1] = nodeChilds[i][nodeText];
}
}
}
... ... ...
}
Data are inserted object by the data of circular recursion node, return final object:
// process the not unique node of child node nodes
if (nodeChilds[i].childNodes.length) {
// tagName attribute is empty new Object Recursion process data
if (cycleOBJ[nodeChilds[i].tagName] == null) {
cycleOBJ[nodeChilds[i].tagName] = new Object;
buildObjectNode(cycleOBJ[nodeChilds[i].tagName], nodeChilds[i]);
}else{
When // tagName attribute number is unique, Recursion process node
if(cycleOBJ[nodeChilds[i].tagName].length){
cycleOBJ[nodeChilds[i].tagName][cycleOBJ[nodeChilds
[i].tagName].length] = new Object;
// Recursion process data
buildObjectNode(cycleOBJ[nodeChilds[i].tagName][cycleOBJ
[nodeChilds[i].tagName].length - 1], nodeChilds[i]);
}else{
When // tagName attribute number is not unique, Recursion process node
cycleOBJ[nodeChilds[i].tagName] = [cycleOBJ
[nodeChilds[i].tagName]];
cycleOBJ[nodeChilds[i].tagName][1] = new Object;
buildObjectNode(cycleOBJ[nodeChilds[i].tagName][1], nodeChilds[i]);
}
}
} else {
// node child node is unique, directly data is stored in object
cycleOBJ[nodeChilds[i].tagName] = nodeChilds[i][nodeText];
}
The inventive method realizes convenient, well processes xml data, has preferable ease for use.This method is to node
Property value has carried out good process and ensure that the structure of data, it is possible to solve after xml turns json object owing to structure change is led
Cause can not the problem of direct application data.
Claims (4)
1. the method that xml data based on javascript transfer json data to, is characterized in that
Xml data parsing to input becomes xml object data,
Set up data parsing event, incoming new Object and xml object data,
Judge whether root node contains property value according to incoming new Object and xml object data, property value processed,
It is stored in new Object with the form of child node,
Obtain root node value, it may be judged whether for sky, nodal value is stored in back end, it is thus achieved that the child node of root node, it is judged that son
Data are stored in new Object according to result by the number of node,
Data after parsing return with json object form.
Method the most according to claim 1, is characterized in that judging whether the number of child node, cycle criterion child node contain
Data, if afterbody node, are all stored among new Object by next stage node.
Method the most according to claim 2, is characterized in that whether cycle criterion contains next stage node, if child node contains
Next stage node, creating array or object array, data being stored in new Object successively, until having circulated all nodes.
Method the most according to claim 1, is characterized in that concretely comprising the following steps
Obtain the xml data needing to represent;
Judge whether incoming data are empty, create the object that will return;
Create conversion method, process the value of nodal community;
Cycle criterion processes the data with or without next stage node, and data are inserted object;
Data are inserted object by the data of circular recursion node, return final object.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201610198153.0A CN105868374A (en) | 2016-04-01 | 2016-04-01 | Method for converting xml data into json data based on javascript |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201610198153.0A CN105868374A (en) | 2016-04-01 | 2016-04-01 | Method for converting xml data into json data based on javascript |
Publications (1)
Publication Number | Publication Date |
---|---|
CN105868374A true CN105868374A (en) | 2016-08-17 |
Family
ID=56626669
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN201610198153.0A Pending CN105868374A (en) | 2016-04-01 | 2016-04-01 | Method for converting xml data into json data based on javascript |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN105868374A (en) |
Cited By (2)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN108280049A (en) * | 2017-10-25 | 2018-07-13 | 国云科技股份有限公司 | A kind of rapid generation of nested tables |
CN109875521A (en) * | 2019-04-18 | 2019-06-14 | 厦门纳龙科技有限公司 | A kind of analysis of ECG data and system |
Citations (2)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
US20110161465A1 (en) * | 2009-12-30 | 2011-06-30 | Bmc Software, Inc. | Method and System to Automatically Adapt Web Services from One Protocol/Idiom to Another Protocol/Idiom |
CN104410667A (en) * | 2014-10-31 | 2015-03-11 | 北京思特奇信息技术股份有限公司 | Method and system for conversion from XML message to JSON message |
-
2016
- 2016-04-01 CN CN201610198153.0A patent/CN105868374A/en active Pending
Patent Citations (2)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
US20110161465A1 (en) * | 2009-12-30 | 2011-06-30 | Bmc Software, Inc. | Method and System to Automatically Adapt Web Services from One Protocol/Idiom to Another Protocol/Idiom |
CN104410667A (en) * | 2014-10-31 | 2015-03-11 | 北京思特奇信息技术股份有限公司 | Method and system for conversion from XML message to JSON message |
Cited By (2)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN108280049A (en) * | 2017-10-25 | 2018-07-13 | 国云科技股份有限公司 | A kind of rapid generation of nested tables |
CN109875521A (en) * | 2019-04-18 | 2019-06-14 | 厦门纳龙科技有限公司 | A kind of analysis of ECG data and system |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
CN104410667B (en) | Conversion method and system of a kind of XML message to JSON messages | |
CN102184184B (en) | Method for acquiring webpage dynamic information | |
GB2361341A (en) | Document creation and delivery | |
CN105354020A (en) | Json format data analytic method and data receiving end | |
CN104636389A (en) | Hbase database real-time query achieving method and system | |
CN105335516A (en) | Construction method of universal acquisition system | |
CN101441629A (en) | Automatic acquiring method of non-structured web page information | |
CN101819584B (en) | Light weight intelligent webpage content analysis method | |
CN103226599A (en) | Method and system for accurately extracting webpage content | |
CN112860256B (en) | Visual configuration system and method for Linux kernel equipment tree | |
CN104683472A (en) | Data transmission method supporting large data volume | |
CN100511140C (en) | Method for script language calling multiple output parameter interface by component software system | |
CN107247582A (en) | It is a kind of to automatically generate the method that CAN sends and receives code | |
CN105868374A (en) | Method for converting xml data into json data based on javascript | |
CN107222461B (en) | Communication processing method and device | |
CN103269521B (en) | Intelligent terminal data fast sharing method | |
CN110806973A (en) | Automatic generation method and device of interface message | |
CN104143110A (en) | Two-dimensional code generating method with web site information | |
CN112328231B (en) | Quick conversion method between python dictionary and structure | |
CN106484488B (en) | Integrated cloud Compilation Method and system | |
CN107977419B (en) | Nuclear power station DCS operation and maintenance support file identification method and system | |
CN102841919A (en) | Method and system for analyzing expressions in conversion text | |
CN109446146B (en) | State transition sequence generation method of application layer communication protocol | |
CN101516086B (en) | Service matching method for mobile communication | |
CN103713930A (en) | Method and device for graphically defining process map |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
C06 | Publication | ||
PB01 | Publication | ||
C10 | Entry into substantive examination | ||
SE01 | Entry into force of request for substantive examination | ||
RJ01 | Rejection of invention patent application after publication | ||
RJ01 | Rejection of invention patent application after publication |
Application publication date: 20160817 |