0% found this document useful (0 votes)
74 views15 pages

XML Json

XML and JSON are two standard formats for representing data. XML uses tags to structure hierarchical data, while JSON uses JavaScript syntax to structure data as objects and arrays. Both formats can be parsed and generated programmatically using DOM, stream, or serialization/deserialization approaches. Common languages have libraries that make it easy to work with XML and JSON.

Uploaded by

M Alwi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views15 pages

XML Json

XML and JSON are two standard formats for representing data. XML uses tags to structure hierarchical data, while JSON uses JavaScript syntax to structure data as objects and arrays. Both formats can be parsed and generated programmatically using DOM, stream, or serialization/deserialization approaches. Common languages have libraries that make it easy to work with XML and JSON.

Uploaded by

M Alwi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

XML & JSON

CS 240 – Advanced Programming Concepts


Background
• XML and JSON are two standard, textual data formats for
representing arbitrary data
– XML stands for “eXtensible Markup Language”
– JSON stands for “JavaScript Object Notation”
• Both are commonly used in practice
• XML came first
• JSON, which uses JavaScript syntax, became popular for
representing data in web applications and services
– If you’re using JavaScript, JSON is an obvious choice
• Both formats are reasonable choices, although some people have
strong biases
• Most programming languages have libraries for parsing and
generating both XML and JSON
• You should be familiar with both

2
Structure of XML Documents
• Header
• Examples (your
• Root Element
browser will
• Start Tags / End Tags
interpret some of
• Element Contents the tags, so select
– Child Elements
“View Page
– Text
– Both (mixed contents)
Source”)
• Element Attributes – Element-heavy
• Comments – Attribute-heavy
• Entity References – Hybrid

3
Predefined Entities

Entity name Character Specified By


quot " "
amp & &
apos ' '
lt < &lt;
gt > &gt;

4
Structure of JSON Documents
• Supported data types: Objects, Arrays,
Numbers, Strings, Boolean, Null
• Objects delimited by { … } with comma-
separated properties in between
– { “name”: “Bob”, “age”: 32, “alive”: true }
• Arrays delimited by [ … ] with comma-
separated elements in between
– [ “testing”, 1, 2, 3, { “gpa”: 3.4 } ]
• Examples
– Verbose
– Simple 5
Parsing XML & JSON Data
• Most languages provide both XML and JSON parsers, so there’s no
need to write your own
• Three Major Types of Parsers
– DOM Parsers
• Convert XML or JSON text to an in-memory tree data structure (the tree is
called a DOM, or “document object model”)
• After running the parser to create a DOM, traverse the DOM to extract the
data you want
– Stream Parsers
• Tokenizers that return one token at a time from the XML or JSON data file
– Serializers / Deserializers
• Use a library to convert from XML or JSON to Java Objects (and vice versa)
• Jackson for XML
• Gson or Jackson for JSON

6
JSON Parsing Examples
• Source File: cd_catalog.json
• Domain Object: CD
• DOM Parser Example
– JsonDomParserExample.java
• Stream Parser Example
– JsonStreamParserExample.java

7
JSON Parsing Examples (cont.)
• Deserialization using the Gson library from
Google
• Simple Deserialization Example
– Source File: cd_catalog_simple.json
– Domain Objects: Catalog, CD
– JsonSimpleObjectDeserializationExample.java
• Complex Deserialization Example
– Source File: cd_catalog.json
– Domain Objects: Catalog, CD
– JsonObjectDeserializationExample.java

8
XML Parsing Examples
• Source File: cd_catalog.xml
• Domain Object: CD
• DOM Parser Example
– XmlDomParserExample.java
• Stream Parser Example
– XmlStreamParserExample.java

9
XML Parsing Examples (cont.)
• Deserialization using Jackson
– Source File: cd_catalog.xml
– Domain Objects: Catalog, CD
– XmlObjectDeserializationExample.java

10
Generating XML & JSON Data
• Programs often need to generate (or create) XML
and JSON data
• You can print XML or JSON data yourself (it’s just
text), but it’s better to use a library that handles
tricky special cases like escaping special
characters)
• Three Ways to Generate XML or JSON
– Create DOM tree in memory, and then tell the tree to
write itself to text
– Write the data as a stream, one token at a time
– Use a “serializer” class that converts Java objects to
XML or Json
11
JSON Generation Examples
• Domain Objects: CDFactory (used to generate
Java objects to be converted), Catalog, CD
• DOM Generator Example
– JsonDomGenerationExample.java
• Stream Generator Example
– JsonStreamGenerationExample.java

12
JSON Generation Examples (cont.)
• Serialization using Gson
• Simple Serialization Example
– Domain Objects: CDFactory, Catalog, CD
– JsonSimpleObjectSerializationExample.java
• Complex Deserialization Example
– Domain Objects: CDFactory, Catalog, CD
– JsonObjectSerializationExample.java

13
XML Generation Examples
• Domain Objects: CDFactory, Catalog, CD
• DOM Generator Example
– XmlDomGenerationExample.java
• Stream Generator Example
– XmlStreamGenerationExample.java

14
XML Generation Examples (cont.)
• Serialization using the Jackson library
• Domain Objects: CDFactory, Catalog, CD
• XmlObjectSerializationExample.java

15

You might also like