Article Directory
What is a data exchange format?
In the company's web project, client and server exchange common data formats JSON
, XML
and so on ftl template engine. At present most of the company's project server response to client requests are returned JSON data format.
Generally divided clients fall into two categories: mobile terminal , PC terminal .
ios
And 安卓
, using the means of communication is http
agreement + json
a restful style, is currently the most Internet companies to take the way of the project.
xml
Is used less and less, since the transmission resource consuming (broadband), heavier-weight. Therefore, under normal circumstances, the bank projects using http
protocol + xml
majority.
JSON
First of all we need to know what is JSON?
JSON
(JavaScript Object Notation
, The JS objects notation) is a lightweight data-interchange format . It is based on a subset of ECMAScript (European Computer Society norms established by js), using completely independent of the programming language of the text format to store and represent data . Simple and clear hierarchy make JSON an ideal data-interchange language. Easy to read and write, but also easy for machines to parse and generate, and effectively improve the efficiency of network transmission .
In simple terms: JSON
is a lightweight data interchange format, structure clear and easy to use, compared to XML
heavyweight data exchange format is more easy to use, so the client and server side using JSON
the way of data exchange format to communicate.
JSON data structure as follows:
{
"fruit": [
{
"name": "苹果",
"address": "烟台",
"price": "12.9",
"unit": "kg"
},
{
"name": "香蕉",
"address": "海南",
"price": "6.5",
"unit": "kg"
}
]
}
json
Data presented in two formats, the object / array .
In "{"
the beginning "}"
end, "{}"
wrapped as a json
target object to which a value key value
is stored format. "[]"
It represents json
an array, which is loaded with a number of json
objects.
Commonly used JSON parsing framework
fastjson
(Ali), gson
(Google), jackson
(SpringMVC own)
For example fastJson of API
XML
First of all we need to know what XML is?
Extensible markup language subset, standard generalized markup language ( Extensible Markup Language
abbreviated XML
). Is a marker for an electronic document to have a structured markup language. The main role is to (describe) to store some data, or used for configuration files, such as spring
projects on the use of the xml
file.
Use xml
should pay attention to the file header structure, need to declare the version number, encoding. xml
Presented by way tag, nested layers, it can be seen as a tree structure.
XML data structure as follows:
<?xml version="1.0" encoding="UTF-8"?>
<students>
<student id="1">
<name>小明</name>
<address>深圳</address>
<sex>1</sex>
</student>
... ...
</students>
XML parsing mode
Dom4j
, Sax
, Pull
, generally these types.
dom4j
Not suitable for parsing large files, because it is at once the file is loaded into memory, so there may be out of memory, sax
it is based on the event to parse the xml, so he can parse xml large files, it is precisely because of this so dom4j
we can be flexible xml CRUD and navigation, but not so strong sax flexibility, so sax
often used to parse large xml
files, but to to xml
file some flexible ( crud
) operation to use dom4j
.
The company projects parsing xml
I use dom4j
, using dom4j
mainly operating Document
object. Creating SAXReader
the object read by the target file, get Document
after the object is very convenient, and then get root, that root
node, the operation is after some iterations to obtain operational attributes / text and the like.