XML:
xml wellformed and
valid xml
A "Well Formed" XML document has correct XML
syntax.
The syntax rules were described in the previous chapters:
XML documents must
have a root element
XML elements must
have a closing tag
XML tags are case
sensitive
XML elements must
be properly nested
XML attribute
values must be quoted
Dtd---consist of elements and attributes
What Is JSON?
JSON is a Java library that helps convert Java objects into
a string representation. This string, when eval()ed in JavaScript, produces an
array that contains all of the information that the Java object contained.
JSON's object notation grammar is suitable for encoding many nested object
structures. Since this grammar is much smaller than its XML counterpart, and
given the convenience of the eval() function, it is an ideal choice for fast
and efficient data transport between browser and server.
JSON is designed to be used in conjunction with JavaScript
code making HTTP requests. Since server-side code can be written in a variety
of languages, JSON is available in many different languages such as C#, Python,
PHP, and of course, Java!
JSON: The
Fat-Free Alternative to XML
Extensible Markup Language (XML) is a text format derived
from Standard Generalized Markup Language (SGML). Compared to SGML, XML is
simple. HyperText Markup Language (HTML), by comparison, is even simpler. Even
so, a good reference book on HTML is an inch thick. This is because the
formatting and structuring of documents is a complicated business.
Most of the excitement around XML is around a new role as an
interchangeable data serialization format. XML provides two enormous advantages
as a data representation language:
It is text-based.
It is position-independent.
These together encouraged a higher level of
application-independence than other data-interchange formats. The fact that XML
was already a W3C standard meant that there wasn't much left to fight about (or
so it seemed).
Unfortunately, XML is not well suited to data-interchange,
much as a wrench is not well-suited to driving nails. It carries a lot of
baggage, and it doesn't match the data model of most programming languages.
When most programmers saw XML for the first time, they were shocked at how ugly
and inefficient it was. It turns out that that first reaction was the correct
one. There is another text notation that has all of the advantages of XML, but
is much better suited to data-interchange. That notation is JavaScript Object
Notation (JSON).
The most informed opinions on XML (see for example
xmlsuck.org) suggest that XML has big problems as a data-interchange format,
but the disadvantages are compensated for by the benefits of interoperability
and openness.
JSON promises the same benefits of interoperability and
openness, but without the disadvantages.Let's compare XML and JSON on the
attributes that the XML community considers important.
From http://www.simonstl.com/articles/whyxml.htm
Simplicity
XML is simpler than SGML, but JSON is much simpler than XML.
JSON has a much smaller grammar and maps more directly onto the data structures
used in modern programming languages.
Extensibility
JSON is not extensible because it does not need to be. JSON
is not a document markup language, so it is not necessary to define new tags or
attributes to represent data in it.
Interoperability
JSON has the same interoperability potential as XML.
JSON is at least as open as XML, perhaps more so because it
is not in the center of corporate/political standardization struggles.
From http://www.karto.ethz.ch/neumann/caving/cavexml/why_xml.html
In summary these are some of the advantages of XML.
XML is human readable
JSON is much easier for human to read than XML. It is easier
to write, too. It is also easier for machines to read and write.
XML can be used as an exchange format to enable users to
move their data between similar applications
The same is true for JSON.
XML provides a structure to data so that it is richer in
information
The same is true for JSON.
XML is easily processed because the structure of the data is
simple and standard
JSON is processed more easily because its structure is
simpler.
There is a wide range of reusable software available to
programmers to handle XML so they don't have to re-invent code
JSON, being a simpler notation, needs much less specialized
software. In the languages JavaScript and Python, the JSON notation is built
into the programming language; no additional software is needed at all. In
other languages, only a small amount of JSON-specific code is necessary. For
example, a package of three simple classes that makes JSON available to Java is
available for free from JSON.org.
XML separates the presentation of data from the structure of
that data.
XML requires translating the structure of the data into a
document structure. This mapping can be complicated. JSON structures are based
on arrays and records. That is what data is made of. XML structures are based
on elements (which can be nested), attributes (which cannot), raw content text,
entities, DTDs, and other meta structures.
A common exchange format
JSON is a better data exchange format. XML is a better
document exchange format. Use the right tool for the right job.
Many views of the one data
JSON does not provide any display capabilities because it is
not a document markup language.
XML and JSON have this in common.
Complete integration of all traditional databases and
formats
(Statements about XML are sometimes given to a bit of
hyperbole.) XML documents can contain any imaginable data type - from classical
data like text and numbers, or multimedia objects such as sounds, to active
formats like Java applets or ActiveX components.
JSON does not have a <[CDATA[]]> feature, so it is not
well suited to act as a carrier of sounds or images or other large binary
payloads. JSON is optimized for data. Besides, delivering executable programs
in a data-interchange system could introduce dangerous security problems.
Internationalization
XML and JSON both use Unicode.
Open and extensible
XML’s one-of-a-kind open structure allows you to add other
state-of-the-art elements when needed. This means that you can always adapt
your system to embrace industry-specific vocabulary.
Those vocabularies can be automatically converted to JSON,
making migration from XML to JSON very straightforward.
From http://www.xmlspy.com/manual/whyxml.htm
XML is easily readable by both humans and machines
JSON is easier to read for both humans and machines.
XML is object-oriented
Actually, XML is document-oriented. JSON is data-oriented.
JSON can be mapped more easily to object-oriented systems.
XML is being widely adopted by the computer industry
JSON is just beginning to become known. Its simplicity and
the ease of converting XML to JSON makes JSON ultimately more adoptable.
What really makes one data interchange format better than
the other? As long as it serves the needs of the developers using it, then the
format can at least be said to be adequate. The primary usage of JSON is for
data delivery between browsers and servers. Even though it can technically be
stored in files and the like, JSON is rarely used outside of the web
environment. XML can also be used for data delivery between browsers and
servers but also is stored in files and used to extract data from database. For
this comparision, I’ll just consider the browser/server usage.
Syntax: JSON
syntax is quite light and definitely less verbose than XML, with all of its
start and end tags. When it comes down to pure byte-size, JSON can represent
the same data as XML using fewer characters.
Weight: Since
JSON syntax requires fewer characters, it is lighter on the wire than XML. The
question is if this really matters. Any large data set is going to be large
regardless of the data format you use. Add to that the fact that most servers
gzip or otherwise compress content before sending it out, the difference
between gzipped JSON and gzipped XML isn’t nearly as drastic as the difference
between standard JSON and XML.
Browser Parsing:
On the browser, there’s no such thing as a JSON parser. However, there is
eval(), which interprets JavaScript code and returns the result. Since JSON
syntax is a subset of JavaScript syntax, a JSON string can be evaluated using
eval() and quickly turned into an object that is easy to work with and
manipulate. XML parsing on the browser is spotty at best. Each browser
implements some different way of dealing with XML and none of them are terribly
efficient. In the end, the XML ends up as a DOM document that must be navigated
to retrieve data. Native JavaScript objects are much easier to work with in
JavaScript than DOM documents, although the playing field would level out
considerably if every browser supported ECMAScript for XML (E4X), which makes
working with XML data as easy as working with JavaScript objects.
Server Parsing:
On the server, parsing is just about equal between JSON and XML. Most
server-side frameworks have XML parsing capabilities and many now are starting
to add JSON parsing capabilities as well. On the server, these parsers are
essentially equal, parsing a text format into an object model. JSON holds no
advantage over XML in this realm.
Querying: This is where XML really shines. Using XPath, it’s
possible to get direct access to a part of multiple parts of an XML data
structure; no such interface exists for JSON. To get data from a JSON
structure, you must know exactly where it is or else iterate over everything
until you find it.
Format Changes:
So you have your data in one format but you want it in another. If the data is
in XML, you can write an XSLT template and run it over the XML to output the
data into another format: HTML, SVG, plain text, comma-delimited, even JSON.
XSLT support in browsers is pretty good, even offering JavaScript-level access
to it. XSLT support on the server is excellent. When you have data in JSON, it’s
pretty much stuck there. There’s no easy way to change it into another data
format. Of course it’s possible to walk the structure and do with it as you
please, but the built-in support isn’t there as it is with XSLT for XML.
Security: Since
the only way to parse JSON into JavaScript objects is to use eval(), it opens
up a huge security hole. The eval() function will execute any arbitrary
JavaScript code and so is dangerous to use in production systems. Invalid JSON
that contains valid JavaScript code could execute and wreak havoc on an
application. The solution, of course, is to build a true JSON parser for
browsers, but we’re not there yet. On the other hand, XML data is completely
safe. There is never a possibility that parsing XML data will result in code
being executed.
With all of this considered, the two main advantages that
JSON has over XML are 1) the speed and ease with which it’s parsed and 2) the
ease of simple data retrieval from JavaScript object. Note that both of these
advantages exist on the browser side of things; on the server-side they are
essentially equal, unless you take querying and format changes into account, in
which case XML is the clear winner. I don’t think that code size really is an
advantage when you’re talking about gzipped data. I also believe that if
ECMAScript for XML is implemented in all browsers, that JSON’s advantages go
away.
DOM -Tree model parser(Object based) (Tree
of nodes).
-DOM loads the file into
the memory and then parse the file.
-Has memory constraints
since it loads the whole XML file before parsing.
-DOM is read and write (can
insert or delete the node).
-If the XML content is
small then prefer DOM parser.
-Backward and forward
search is possible for searching the tags and evaluation of the information
inside the tags. So this gives the ease of navigation.
-Slower at run time.
SAX
-Event based parser
(Sequence of events).
-SAX parses the file at it
reads i.e. Parses node by node.
-No memory constraints as
it does not store the XML content in the memory.
-SAX is read only i.e.
can’t insert or delete the node.
-Use SAX parser when memory
content is large.
-SAX reads the XML file
from top to bottom and backward navigation is not possible.
-Faster at run time.
WebService
JAX-WS -
is Java API for the XML-Based Web Services - a standard way to develop a Web-
Services in SOAP notation (Simple Object Access
Protocol).
Calling
of the Web Services is performed via remote procedure calls. For the exchange
of information between the client and the Web Service is used SOAP protocol.
Message exchange between the client and the server performed through XML- based SOAP
messages.
Clients
of the JAX-WS Web- Service need a WSDL file
to generate executable code that the clients can use to call Web- Service.
JAX-RS -
Java API for RESTful Web Services. RESTful Web Services are represented as
resources and can be identified by Uniform Resource Identifiers (URI). Remote procedure call in this case is represented
a HTTP- request and the necessary data is passed as
parameters of the query. Web Services RESTful - more flexible, can use several
different MIME- types. Typically used for XML data exchange or JSON (JavaScript Object Notation) data
exchange.
·
JAX-WS: addresses advanced QoS
requirements commonly occurring in enterprise computing. When compared to
JAX-RS, JAX-WS makes it easier to support the WS-* set of protocols, which
provide standards for security and reliability, among other things, and
interoperate with other WS-* conforming clients and servers.
·
JAX-RS: makes it easier to write web
applications that apply some or all of the constraints of the REST style to
induce desirable properties in the application, such as loose coupling
(evolving the server is easier without breaking existing clients), scalability
(start small and grow), and architectural simplicity (use off-the-shelf
components, such as proxies or HTTP routers). You would choose to use JAX-RS
for your web application because it is easier for many types of clients to
consume RESTful web services while enabling the server side to evolve and
scale. Clients can choose to consume some or all aspects of the service and
mash it up with other web-based services.