Persistent Data Danny (Yanming) Feng Adapted from previous lectures by Sid Chi-Kin Chau, Michael Curtotti and Eric McCreath
Persistent Data
Danny (Yanming) FengAdapted from previous lectures by
Sid Chi-Kin Chau, Michael Curtotti and Eric McCreath
2
I. OverviewII. Bespoke and SerialisationIII. XML and JSON
ANU College of Engineering & Computer Science - COMP2100/6442
ANU College of Engineering & Computer Science - COMP2100/6442
Goals
Explore XML and JSON as approaches to data persistence
Demonstrate the approaches by looking at simple implementations
ANU College of Engineering & Computer Science - COMP2100/6442
XML Application example: .docx
ANU College of Engineering & Computer Science - COMP2100/6442
XML Application example: .docx
ANU College of Engineering & Computer Science - COMP2100/6442
XML Implementation Example Tree Structure Root Element Child Element Attribute <customer id=”123”
status=”active”>
ANU College of Engineering & Computer Science - COMP2100/6442
Two major options for XML in Java Two approaches – DOM and SAX
SAX – Simple API for XML
DOM – Document Object Model (structured around XML standard)
SAX treats xml as stream and allows extraction of data as stream is read- preferable for very large documents (gigabyte)
SAX very fast and efficient as compared as DOM
Java DOM reads in entire XML tree and generate the node object
ANU College of Engineering & Computer Science - COMP2100/6442
XML - DOM Root ElementCompany
ElementEmployee
ElementEmployee
AttributeNon-Technical
AttributeTechnical
ElementFirstNameElement
FirstNameElementPhone
ElementLastName
ElementFirstName
ElementPhone
ElementLastName
ElementFirstName
ElementPhone
TextSid
Text012345
TextSid
TextChau
Text543210
TextTom
TextJerry
ANU College of Engineering & Computer Science - COMP2100/6442
Using DOM DOM requires a number of steps to save data to file: - DocumentBuilderFactory used to generate a DocumentBuilder - Document created from a DocumentBuilder object - Then a matter of creating appropriate elements and appending them in appropriate tree (e.g. gender element a child of person element – append text node as gender element) - Then further steps to get it ready for a output file Similar series of steps for DOM loading - DocumentBuilderFactory - Document Builder - Document - Class data structures
ANU College of Engineering & Computer Science - COMP2100/6442
Advantages and Disadvantages+ Robust, Extensible+ Involved to implement+ Human readable+ Portable, Platform independent and programming language independent+ XML supports Unicode (international encoding) + Easy format verification- XML syntax is verbose and redundant- XML file sizes are usually big because of above- Doesn’t support Array
ANU College of Engineering & Computer Science - COMP2100/6442
JSON JavaScript Object Notation (JSON), like XML, is also an
open standard format that is widely used.
Originally designed for sending data between web client and server, but also very useful for data storage.
Built around attribute-value pairs and produces smaller and more readable documents than XML.
JSON example: {“age”:21,”name”:”Marry”}
ANU College of Engineering & Computer Science - COMP2100/6442
Advantages and Disadvantages+ More lightweight+ Straightforward to implement+ Support array and null+ can easily distinguish boolean, number, and string type
- Lacking language features of XML(e.g. xml attributes..)- Not native support in Java
ANU College of Engineering & Computer Science - COMP2100/6442
JSON syntax and types {“attribute-name”:{JSON object}}
{“attribute-name”:”string”}
{“attribute-name”:[array]}
{“attribute-name”:1} (number)
{“attribute-name”:true} (boolean)
{“attribute-name”:null}
ANU College of Engineering & Computer Science - COMP2100/6442
Example Exam QuestionsQ1: Describe the JSON format. Illustrate your description with a JSON document that could be used for storing a person's name, phone and address.
Q2: What are the advantages of storing persistent data using XML or JSON files rather than using Serializable Java Objects?
Q3: In the context of persistent storage which statement is correct?A. The kind of data we are using is irrelevant to our choice of data persistence.B. Bespoke methods often lack robustness.C. JSON uses the same tag syntax as XML for marking up data.D. JSON runs an SQL database to store data.
15
Recommended Reading
ANU College of Engineering & Computer Science - COMP2100/6442
- Kay Horstmann, Big Java (Chapters on XML)
- W3C XML standards pages https://www.w3.org/standards/
- JSON https://www.json.org/
- The JavaScript Object Notation (JSON) Data Interchange Format https://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf
- 10 JSON Examples to Use in Your Projects https://www.sitepoint.com/10-example-json-files/
All demo code will be on Git
If you have any question feel free to post it on the course forum
My email: [email protected]
Thank you!