XMLTree Model

XMLTree Model
5 May 2016
OSU CSE
1
XMLTree
• The XMLTree component family allows
you to create, and navigate through, a tree
whose structure mirrors that of an XML file
– The file from which the tree is created may
come from your computer or from the web
– You need not worry about parsing the file
(recognizing tags, matching start-end tags,
identifying attributes, etc.), as this is done for
you by XMLTree
5 May 2016
OSU CSE
2
Example XML File
<book printISBN="978-1-118-06331-6" webISBN="1-118063-31-7"
pubDate="Dec 20 2011">
<author>Cay Horstmann</author>
<title>Java for Everyone: Late Objects</title>
<contents>
<preface></preface>
<chapter number="1" title="Introduction">
<section title="Chapter Goals"></section>
<section title="Chapter Contents"></section>
<section title="Computer Programs" number="1.1"></section>
<section title="The Anatomy of a Computer" number="1.2"></section>
...
</chapter>
<chapter number="2" title="Fundamental Data Types">
</chapter>
...
</contents>
</book>
5 May 2016
OSU CSE
3
Example XMLTree Created
<book>
<book>
printISBN → 978-1-118-06331-6
webISBN → 1-118063-31-7
pubDate → Dec 20 2011
<author>
<author>
<contents>
<contents>
<title>
<title>
Java for Everyone:
Late Objects
Cay Horstmann
<preface>
<preface>
<chapter>
<chapter>
number → 1
title → Introduction
<section>
<section>
title → Chapter Goals
31 January 2017
<section>
<section>
title → Chapter Contents
<section>
<section>
number → 1.1
title → Computer Programs
OSU CSE
<chapter>
<chapter>
number → 2
title → Fundamental Data Types
<section>
<section>
number → 1.2
title → The Anatomy of a Computer
...
...
4
Top-Level Element = Root of Tree
<book printISBN="978-1-118-06331-6"
webISBN="1-118063-31-7"
pubDate="Dec 20 2011">
...
</book>
31 January 2017
OSU CSE
5
Top-Level Element = Root of Tree
<book printISBN="978-1-118-06331-6"
webISBN="1-118063-31-7"
pubDate="Dec 20 2011">
...
</book>
5 May 2016
OSU CSE
6
Top-Level Element = Root of Tree
<book printISBN="978-1-118-06331-6"
webISBN="1-118063-31-7"
pubDate="Dec 20 2011">
...
</book>
5 May 2016
OSU CSE
7
Top-Level Element = Root of Tree
The attribute name-value
<book printISBN="978-1-118-06331-6"
pairs of an element/tag are
webISBN="1-118063-31-7"
in the tree node of that
pubDate="Dec 20 2011">
element/tag.
...
</book>
5 May 2016
OSU CSE
8
Top-Level Element = Root of Tree
<book printISBN="978-1-118-06331-6"
webISBN="1-118063-31-7"
pubDate="Dec 20 2011">
...
</book>
5 May 2016
OSU CSE
9
Top-Level Element = Root of Tree
The children of the
<book printISBN="978-1-118-06331-6"
top-level element/tag
webISBN="1-118063-31-7"
are the children of
pubDate="Dec 20 2011">
the root of the tree.
...
</book>
5 May 2016
OSU CSE
10
Top-Level Element = Root of Tree
The first child node of
<book printISBN="978-1-118-06331-6"
the root is known as
webISBN="1-118063-31-7"
“child 0” because
pubDate="Dec 20 2011">
children are
...
numbered from 0.
</book>
5 May 2016
OSU CSE
11
Top-Level Element = Root of Tree
The second child node
<book printISBN="978-1-118-06331-6"
of the root is known as
webISBN="1-118063-31-7"
“child 1”.
pubDate="Dec 20 2011">
...
</book>
5 May 2016
OSU CSE
12
Top-Level Element = Root of Tree
The third child node of
<book printISBN="978-1-118-06331-6"
the root is known as
webISBN="1-118063-31-7"
“child 2”.
pubDate="Dec 20 2011">
...
</book>
5 May 2016
OSU CSE
13
First Child Element = First Child Node
<book ...>
<author>Cay Horstmann</author>
...
</book>
31 January 2017
OSU CSE
14
First Child Element = First Child Node
<book ...>
<author>Cay Horstmann</author>
...
</book>
5 May 2016
OSU CSE
15
First Child Element = First Child Node
<book ...>
<author>Cay Horstmann</author>
...
</book>
5 May 2016
OSU CSE
16
First Child Element = First Child Node
<book ...>
<author>Cay Horstmann</author>
...
</book>
The content of an
element/tag is in a
child node of the node
for that element/tag;
the content node itself
has no tag.
5 May 2016
OSU CSE
17
Second Child Element = Second Child Node
<book ...>
...
<title>Java for Everyone: Late Objects</title>
...
</book>
5 May 2016
OSU CSE
18
Second Child Element = Second Child Node
<book ...>
...
<title>Java for Everyone: Late Objects</title>
...
</book>
5 May 2016
OSU CSE
19
Second Child Element = Second Child Node
<book ...>
...
<title>Java for Everyone: Late Objects</title>
...
</book>
5 May 2016
OSU CSE
20
… And So On!
• The same rules apply at all levels of the
XML file and of the XMLTree object that is
created from it
5 May 2016
OSU CSE
21
Key Properties/Invariants
• Every node in the tree represents either a tag
(e.g., <author>) or the text content (e.g.,
"Cay Horstman") of an element
• Every node in the tree has a label String
that is either the tag name (e.g., "author")
or the text content itself (e.g., "Cay
Horstman")
• Only tag nodes can have zero or more
attribute name-value pairs, each of which is a
String (e.g., "pubDate" → "Dec 20
2011")
5 May 2016
OSU CSE
22
Tag Nodes and Text Nodes
Tag node
<book>
<book>
label = “book”
attributes =
“printISBN” → “978-1-118-06331-7”
“webISBN” → “1-118063-31-7”
“pubDate” → “Dec 20 2011”
printISBN → 978-1-118-06331-6
webISBN → 1-118063-31-7
pubDate → Dec 20 2011
<author>
<author>
Cay Horstmann
<contents>
<contents>
<title>
<title>
Java for Everyone:
Late Objects
<preface>
<preface>
<chapter>
<chapter>
<chapter>
<chapter>
number → 1
title → Introduction
Tag node
number → 2
title → Fundamental Data Types
...
label = “preface”
No attributes
Text node
<section>
<section>
<section>
<section>
title → Chapter
Goals
title → Chapter Contents
label
= “Cay
Horstmann”
5 May 2016
<section>
<section>
number → 1.1
title → Computer Programs
OSU CSE
<section>
<section>
number → 1.2
title → The Anatomy of a Computer
...
23
Reality Check
• Carefully examine the example XML file
and the example XMLTree diagram shown
earlier to check your understanding of how
the file content is modeled by the tree
5 May 2016
OSU CSE
24
Can You Draw The Tree?
<?xml version="1.0"?>
<buckeyes>
<location stadium="home" />
Go Bucks!
<game opp="UAB" date="22 Sep 2012">
<forecast>Sunny</forecast>
</game>
</buckeyes>
5 May 2016
OSU CSE
25
Resources
• OSU CSE Components API: XMLTree
– http://cse.osu.edu/software/common/doc/
5 May 2016
OSU CSE
26