XML VS JSON
XML
.Data interchange format
.Extensible Markup Language:
- Text-based, self describing data presentation
- Meta language (i.e. use this language to define your own data structure)
.Platform independent
.Widely supported
.Specified by W3C for the World Wide Web
XML Basics I
.XML is based on markup
∘Opening and closing tags
<tag>…</tag>
∙A tag has …
∘… a name
∘… optional content between opening and closing tag
∘… an optional namespace
∘… optional attributes
<tag key=“value”>…</tag>
XML Example
<book bookid="1234">
<title>XML in 5 minutes</title>
<author>T.Knowsitall</author>
<price>0.50</price>
<availability>1</availability>
<bookoftheyear/>
</book>
XML Basics II
∙XML documents must be well-formed
∘Begin with the XML declaration
<?xml version="1.0"?>
∘Have one unique root element
∘All start tags must match end tags (case sensitive)
∘All elements must be closed
∘All elements must be properly nested
∘All attribute values must be quoted
∘XML entities must be used for special characters
•e.g. < for <
XML Structure
∙XML document: elements in a hierarchical structure
∘XML is good at encoding tree structures
∙Structure somewhat evident for humans
∘Only syntax, semantics left for guessing
∘Verbose text representation
∙How to encode arbitrary graphs in XML?
∘E.g. a graph with circular references?
XML Validation
∙XML documents can be validated
∘Predefine structure of a document
∘Check if document matches structure
∙1. DTDs (Document Type Definitions)
∘Extended context-free grammar
∘Part of XML specification – not covered here
∙2. XML schema
∘Define structure of a document in XML
∘Support for data types (built-in and user-defined)
XML Schema Example
<element name="book">
<attribute name="bookid" type="decimal"/>
<complexType>
<sequence>
<element name="title" type="string"/>
<element name="author" type="string"/>
<element name="price" type="string"/>
<element name="availability" type="string"/>
<element name="bookoftheyear" minOccurs="0" maxOccurs="1"/>
</sequence>
</complexType>
</element>
APIs for Using XML
∙How to read data from an XML document?
∙Document Object Model (DOM)
∘Parses XML as tree into memory
∘Easy traversal via API (e.g. go to child element)
∙Simple API for XML (SAX)
∘Throws events for each XML element (e.g. here comes a tag, here comes an attribute, etc.)
∘Not „simple“ to use at all
∙XPath & XPointer: address elements in document
XPath / XPointer
∙XPath: Language for accessing XML elements
∘The Root element: /*
∘The fifth child element under an element named "FOOB": FOOB[5]
∘The element FOOB whose BAZ attribute is equal to
“hello":
FOOB[ @BAZ = “hello" ]
∙XPointer: another language, e.g.
∘<foobar id="foo"><bar/><baz><bom/></baz></foobar>
∘element(foo/2/1)
XML Summary
∙Meta language for defining other languages
∘i.e. data structure in your application
∙Data interchange format
∘Breaks documents into hierarchical parts
∘Gives the parts names
∘Lets you write rules for how parts form a document
∙Inefficient
∘Expensive conversion from binary data to text
∘Text format uses lot of space
XML Myths
∙XML is simple
∙XML is human-readable
∙Proof: MS Word Save Unpack XML
∙Data interchange format
∙Originates from JavaScript for Web usage
∘But has been also implemented for other programming languages
∙Text format like XML
∘But no query languages unlike XML
∙Convert data object to JSON & send
∘Receive & parse JSON to data object
JSON Example
{
"bookid": 1234,
"title": "JSON in 5 minutes",
"author": "T.Knowsitall",
"price": 0.50,
"availability": 1,
"bookoftheyear": true,
}
JSON Data Types
∙Numbers: (un)signed integer, float/double
∙String
∙Boolean
∙Array
∘"someArray": [ 1, 2, 3, 4 ]
∙Object (key/value map)
∘{ "foo": "bar", "moep": { "nested": "structure" } }
∙null
∙JSON is simpler and easier than XML
∙XML has more features
∘Query languages
∘Document transformation via XSLT
∘Validation via XML Schema
∙Both are text formats
∘JSON slightly less verbose
For Further more study go to this https://www.w3schools.com/
https://www.json.org/
Follow me on....
Twitter: UsmanAzmat87
Facebook: Musman.azmat
Linkedln: osman-khan-633180106/
Google+: u/0/102433731988304229241
JavaScript Object Notation (JSON)
∙Originates from JavaScript for Web usage
∘But has been also implemented for other programming languages
∙Text format like XML
∘But no query languages unlike XML
∙Convert data object to JSON & send
∘Receive & parse JSON to data object
JSON Example
{
"bookid": 1234,
"title": "JSON in 5 minutes",
"author": "T.Knowsitall",
"price": 0.50,
"availability": 1,
"bookoftheyear": true,
}
JSON Data Types
∙Numbers: (un)signed integer, float/double
∙String
∙Boolean
∙Array
∘"someArray": [ 1, 2, 3, 4 ]
∙Object (key/value map)
∘{ "foo": "bar", "moep": { "nested": "structure" } }
∙null
XML vs. JSON
∙XML has more features
∘Query languages
∘Document transformation via XSLT
∘Validation via XML Schema
∙Both are text formats
∘JSON slightly less verbose
For Further more study go to this https://www.w3schools.com/
https://www.json.org/
Follow me on....
Twitter: UsmanAzmat87
Facebook: Musman.azmat
Linkedln: osman-khan-633180106/
Google+: u/0/102433731988304229241
Comments
Post a Comment