XML Documents 5 May 2016 OSU CSE 1
e X tensible M arkup L anguage • A textual document format used all over the web is XML – Used to represent hierarchically organized data for “ease of use” both by humans and computers – Had its origins in SGML (Standard Generalized Markup Language) from the 1980s – Became a standard in the late 1990s 5 May 2016 OSU CSE 2
Example XML Document/File <?xml version="1.0" encoding="UTF-8"?> <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> ... </book> 5 May 2016 OSU CSE 3
Example XML Document/File <?xml version="1.0" encoding="UTF-8"?> <book printISBN="978-1-118-06331-6" webISBN="1-118063-31-7" An XML pubDate="Dec 20 2011"> declaration is the first line of well- <author>Cay Horstmann</author> formed XML file. <title> Java for Everyone: Late Objects </title> ... </book> 5 May 2016 OSU CSE 4
Example XML Document/File <?xml version="1.0" encoding="UTF-8"?> <book printISBN="978-1-118-06331-6" This is not webISBN="1-118063-31-7" part of the pubDate="Dec 20 2011"> actual <author>Cay Horstmann</author> document/file! <title> Java for Everyone: Late Objects </title> ... </book> 5 May 2016 OSU CSE 5
Example XML Document/File This is an <?xml version="1.0" encoding="UTF-8"?> author <book printISBN="978-1-118-06331-6" element . webISBN="1-118063-31-7" pubDate="Dec 20 2011"> <author>Cay Horstmann</author> <title> Java for Everyone: Late Objects </title> ... </book> 5 May 2016 OSU CSE 6
Example XML Document/File This is a <?xml version="1.0" encoding="UTF-8"?> start tag <book printISBN="978-1-118-06331-6" for an author element. webISBN="1-118063-31-7" pubDate="Dec 20 2011"> <author> Cay Horstmann</author> <title> Java for Everyone: Late Objects </title> ... </book> 5 May 2016 OSU CSE 7
Example XML Document/File This is an <?xml version="1.0" encoding="UTF-8"?> end tag <book printISBN="978-1-118-06331-6" for that author element. webISBN="1-118063-31-7" pubDate="Dec 20 2011"> <author>Cay Horstmann </author> <title> Java for Everyone: Late Objects </title> ... </book> 5 May 2016 OSU CSE 8
Example XML Document/File The content <?xml version="1.0" encoding="UTF-8"?> for this author <book printISBN="978-1-118-06331-6" element is everything webISBN="1-118063-31-7" between its start and end tags. pubDate="Dec 20 2011"> <author> Cay Horstmann </author> <title> Java for Everyone: Late Objects </title> ... </book> 5 May 2016 OSU CSE 9
Example XML Document/File If the author <?xml version="1.0" encoding="UTF-8"?> element had no <book printISBN="978-1-118-06331-6" content, then you webISBN="1-118063-31-7" might find this, or… pubDate="Dec 20 2011"> <author></author> <title> Java for Everyone: Late Objects </title> ... </book> 5 May 2016 OSU CSE 10
Example XML Document/File … or you might find <?xml version="1.0" encoding="UTF-8"?> a single <book printISBN="978-1-118-06331-6" self-closing tag webISBN="1-118063-31-7" like this. pubDate="Dec 20 2011"> <author /> <title> Java for Everyone: Late Objects </title> ... </book> 5 May 2016 OSU CSE 11
Example XML Document/File <?xml version="1.0" encoding="UTF-8"?> <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> This is a ... title </book> element. 5 May 2016 OSU CSE 12
Example XML Document/File <?xml version="1.0" encoding="UTF-8"?> <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> This is a ... book </book> element. 5 May 2016 OSU CSE 13
Example XML Document/File <?xml version="1.0" encoding="UTF-8"?> <book printISBN ="978-1-118-06331-6" webISBN="1-118063-31-7" pubDate="Dec 20 2011"> <author>Cay Horstmann</author> <title> This is the Java for Everyone: Late Objects name of an </title> attribute ... of the book element. </book> 5 May 2016 OSU CSE 14
Example XML Document/File <?xml version="1.0" encoding="UTF-8"?> <book printISBN=" 978-1-118-06331-6 " webISBN="1-118063-31-7" pubDate="Dec 20 2011"> <author>Cay Horstmann</author> <title> This is the Java for Everyone: Late Objects value </title> of the ... printISBN attribute. </book> 5 May 2016 OSU CSE 15
Recursive Structure • An XML document (without the XML declaration in the first line) is made up of: – A top-level element – A string of zero or more child elements of the top-level element, each of which is exactly like the top-level element of an XML document • Notice the similarity to a tree: the structure of an XML document is also recursive • Information it represents is hierarchical 5 May 2016 OSU CSE 16
Can You Find All The Errors? <?xml version="1.0" encoding="UTF-8"?> <pony> <unicorn mark="three lozenges"> <color>cyan</unicorn> </color> <unicorn>Twilight<color>cyan</color>Sparkle</unicorn> <unicorn mark="dolphins" version="G4" /> </pony> <dragon> <youth>Spike </dragon> 5 May 2016 OSU CSE 17
Resources • Big Java , Section 23.1 (but not the rest of Chapter 23) – https://library.ohio-state.edu/record=b8540788~S7 • Wikipedia: XML – http://en.wikipedia.org/wiki/XML 5 May 2016 OSU CSE 18
Recommend
More recommend