UML XML and Java 1 Simple PIM Journal







![[ref. Wikipedia]: Well-formed documents: XML syntax • As long as only well-formedness is required, [ref. Wikipedia]: Well-formed documents: XML syntax • As long as only well-formedness is required,](https://slidetodoc.com/presentation_image_h2/160ba28345315f00cb1fe3aab75ae1af/image-8.jpg)
![[ref. Wikipedia]: Well-formed documents: XML syntax Continues… • • The text enclosed by the [ref. Wikipedia]: Well-formed documents: XML syntax Continues… • • The text enclosed by the](https://slidetodoc.com/presentation_image_h2/160ba28345315f00cb1fe3aab75ae1af/image-9.jpg)


![References [1] Serial Access with the Simple API for XML (SAX) http: //java. sun. References [1] Serial Access with the Simple API for XML (SAX) http: //java. sun.](https://slidetodoc.com/presentation_image_h2/160ba28345315f00cb1fe3aab75ae1af/image-12.jpg)
- Slides: 12

UML, XML and Java 1

Simple PIM Journal name: String SSN: String 1 * Journal. Record description: String A journal contains journal records. 2

A Simple Java Implementation These “use dependencies” can be changed to associations (with navigation) 3

package journal; import java. util. Linked. List; import java. util. List; import journal. IJournal; public class XMLApplication { private List<IJournal> journals = new Linked. List<IJournal>(); public void create() { journals = new Linked. List<IJournal>(); // create first journal with one journal record IJournal journal = new Journal. Impl(); journal. set. Name("Ola"); journal. set. Ssn("1"); IJournal. Record record = new Journal. Record. Impl(); record. set. Description("Record 1 in journal 1"); journal. get. Journal. Records(). add(record); journals. add(journal); // create second journal with two journal record journal = new Journal. Impl(); journal. set. Name("Kari"); journal. set. Ssn("2"); record = new Journal. Record. Impl(); record. set. Description("Record 1 in journal 2"); journal. get. Journal. Records(). add(record); record = new Journal. Record. Impl(); record. set. Description("Record 2 in journal 2"); journal. get. Journal. Records(). add(record); journals. add(journal); } The Simple Java Application public void write. To. Screen() { if (journals == null) return; for (IJournal journal: journals){ System. out. print("Name: " + journal. get. Name()); System. out. println(" SSN: " + journal. get. Ssn()); for (IJournal. Record record: journal. get. Journal. Records()){ System. out. println(" Description: " + record. get. Description()); } } public static void main(String[] args) { XMLApplication app = new XMLApplication(); app. create(); app. write. To. Screen(); } } Output: Name: Ola SSN: 1 Description: Record 1 in journal 1 Name: Kari SSN: 2 Description: Record 1 in journal 2 Description: Record 2 in journal 2 4

package journal; import java. util. Linked. List; import java. util. List; import journal. IJournal. Record; public class Journal. Impl implements IJournal { } Journal private String name; private String ssn; private List<IJournal. Record> journal. Records = new Linked. List<IJournal. Record>(); public String get. Name() { package journal; return name; } import java. util. List; public void set. Name(String name) { this. name = name; public interface IJournal { } public String get. Ssn() { public abstract String get. Name(); return ssn; public abstract void set. Name(String name); } public void set. Ssn(String ssn) { this. ssn = ssn; public abstract String get. Ssn(); } public List<IJournal. Record> get. Journal. Records() { public abstract void set. Ssn(String ssn); return journal. Records; } public List<IJournal. Record> get. Journal. Records() ; public void set. Journal. Records(List<IJournal. Record> journal. Records) { this. journal. Records = journal. Records; public void set. Journal. Records( } List< IJournal. Record> journal. Records); } 5

Journal. Record package journal; public interface IJournal. Record { public abstract String get. Description(); public abstract void set. Description(String description); } package journal; import java. lang. String; public class Journal. Record. Impl implements IJournal. Record { private String description; public String get. Description() { return description; } } public void set. Description(String description) { this. description = description; } 6

What to store on file – what should persist: 7
![ref Wikipedia Wellformed documents XML syntax As long as only wellformedness is required [ref. Wikipedia]: Well-formed documents: XML syntax • As long as only well-formedness is required,](https://slidetodoc.com/presentation_image_h2/160ba28345315f00cb1fe3aab75ae1af/image-8.jpg)
[ref. Wikipedia]: Well-formed documents: XML syntax • As long as only well-formedness is required, XML is a generic framework for storing any amount of text or any data whose structure can be represented as a tree. • The only indispensable syntactical requirement is that the document has exactly one root element. This means that the text must be enclosed between a root opening tag and a corresponding closing tag. The following is a well-formed XML document: <journal>This is a journal. . </journal> • The root element can be preceded by an optional XML declaration. This element states what version of XML is in use (normally 1. 0); it may also contain information about character encoding and external dependencies. <? xml version="1. 0" encoding="UTF-8"? > • The specification requires that processors of XML support the pan-Unicode character encodings UTF-8. . . 8
![ref Wikipedia Wellformed documents XML syntax Continues The text enclosed by the [ref. Wikipedia]: Well-formed documents: XML syntax Continues… • • The text enclosed by the](https://slidetodoc.com/presentation_image_h2/160ba28345315f00cb1fe3aab75ae1af/image-9.jpg)
[ref. Wikipedia]: Well-formed documents: XML syntax Continues… • • The text enclosed by the root tags may contain an arbitrary number of XML elements. The basic syntax for one element is: <name attribute="value">content</name> Here, » content « is some text which may again contain XML elements. So, a generic XML document contains a tree-based data structure. Here is an example of a structured XML document: <recipe name="bread" prep_time="5 mins" cook_time="3 hours"> <title>Basic bread</title> <ingredient amount="3" unit="cups">Flour</ingredient> … <instructions> <step>Mix all ingredients together. </step> … </instructions </recipe> XML requires that elements be properly nested — elements may never overlap. 9

Using XML for Storing Data Journal name: String SSN: String Journal. Record description: String : Journal. Record name=“Ola…” SSN=“ 1” description=“May be a…” : Journal. Record name=“Kari…” SSN=“ 2” description=“Lack of iron. ” : Journal. Record description=“A slight attac…” <medicalsystem> <journal name=“Ola Norman" ssn=“ 1"> <journalrecord>May be a little bit fat? </journalrecord> </journal> <journal name=“Kari Norman" ssn=“ 2"> <journalrecord>Lack of iron. </journalrecord> <journalrecord>A slight attack of shopomania? </journalrecord> </journal> </medicalsystem> 10

A Simplified Model of the Structure of XML-documents <medicalsystem> <journal name=“Ola" ssn=“ 1"> <journalrecord>record 1 in journal 1</journalrecord> </journal> <journal name=“Kari" ssn=“ 2"> <journalrecord>record 1 in journal 2</journalrecord> <journalrecord>record 2 in journal 2</journalrecord> </journal> </medicalsystem> 11
![References 1 Serial Access with the Simple API for XML SAX http java sun References [1] Serial Access with the Simple API for XML (SAX) http: //java. sun.](https://slidetodoc.com/presentation_image_h2/160ba28345315f00cb1fe3aab75ae1af/image-12.jpg)
References [1] Serial Access with the Simple API for XML (SAX) http: //java. sun. com/webservices/jaxp/dist/1. 1/docs/tutorial/sax/index. html 12