XML and XMLTree
CSE 2221
Trees
Trees are a very common concept seen in computer science.
Composition of Trees
A tree is made up of nodes. The first of these nodes is called a root node. The root node will always be seen at the top of the tree with its children connected unterneath. The thing that connects these nodes are called branches.
- The opposite of root nodes, nodes with no branches connected to them are named leaf nodes.
Properties of a Tree
- Size
- the size of a tree is the total number of nodes it contains.
- Path
- a path is the connection between two nodes, all paths have a length.
- Height
- is the length of its longest path from the root.
- Subtree
- a subtree is a section of a tree that can be isolated into its own tree.
XML
XML stands for Extensible Markup Language. It is created to make a heirachical structure of information that is easier for the computer to process.
Creating an XML file
All XML files have to contain a declaration. This is the first lind of every XML file and tells the computer what type of file it is.
Tags
XML files are made up of tags. Tags have several parts to them:
- Starting Tag
- a word contained in
<>
i.e.<author>
. - Contents
- Plain text in between a starting and closing tag.
- Closing Tag
- the same word as the starting tag but with a
/
in front. I.e.</author>
- Attributes
- These are values assigned to a tag. They are included inside the
<>
of the starting tag and are space separated. Attributes also have values assigned to them in quotes. I.e.<author attribute="value">
.
XML Tree Structure
The XMLTree
component family is included in the Ohio State Library and provides several methods for handling XML documents. This slide
contains a good visual for how an XML file can be represented as a tree.
XMLTree
Methods
All XMLTree
methods are instance methods, meaning they must be called from an initialized variable. This variable must be of type XMLTree
. This is the same model that SimpleReader
and SimpleWritter
use.
Constructors
There are two constructors for XMLTree
, they only differ in the parameters they require. To create an XMLTree
, you need either a valid XML file or link. Then construct an XMLTree
with the name of this file.
XMLTree t = new XMLTree1("file.xml");
Methods
A list of all XMLTree
methods can be found here.
String label()
- returns the label of the root of this.
boolean isTag()
- returns whether the label of the root of this is a tag or text
boolean hasAttribute(String name)
- Returns true if this has an attribute of
name
String attributeValue(String name)
- returns the value associated with the attribute with name of
name
of the root node. int numberOfChildren()
- returns the number of children of the root node.
XMLTree child(int k)
- returns the subtree of the kth child of the rood node this.
Really Simple Syndication
RSS is used for web based “feeds” of information. of information. There are a few version of RSS but in this class we will focus on version 2.0
. RSS is just a strict format of XML.
<?xml version="1.0" encoding="utf-8"?> <rss version="2.0"> <channel> <title>Yahoo! News</title> <link>http://news.yahoo.com</link> <description>The latest news and headlines from Yahoo! News.</description> <item> <title>Apple seeks to stop...</title> <link>http://news.yahoo.com/...</link> <description>Apple Inc will seek a...</description> <pubDate>Mon, 27 Aug 2012 14:40:49</pubDate> <source url="http://www.reuters.com">Reuters</source> </item> ... </channel> </rss>
Important things to note about the syntax of an RSS feed is that the root node is always <rss version="X.X">
and that the order of children for every node is not garunteed.