XML Json
XML Json
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
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