Computer Science E-259 XML with Java, Java Servlet, and JSP

Lecture 3: DOM Level 3 1 October 2007 David J. Malan [email protected]

1 Copyright © 2007, David J. Malan . All Rights Reserved.

Computer Science E-259 Last Time ƒ ƒ ƒ ƒ ƒ

XML 1.1 SAX 2.0.2 JAXP 1.3 and Xerces 2.7.1 Parsing My First XML Parser

2 Copyright © 2007, David J. Malan . All Rights Reserved.

Last Time A Representative Document

3

Jim Bob graduate Computer Science & Music Jim Bob! Hi my name is jim. I look like ]]> ... Copyright © 2007, David J. Malan . All Rights Reserved.

Last Time SAX 2.0.2 startDocument(); endDocument(); startElement(·,·); endElement(·); characters(·); ...

4 Copyright © 2007, David J. Malan . All Rights Reserved.

Computer Science E-259 This Time ƒ ƒ ƒ

DOM Level 3 JAXP 1.3 and Xerces 2.7.1 My First XML Parser

5 Copyright © 2007, David J. Malan . All Rights Reserved.

DOM Level 3 Why? ƒ

ƒ

ƒ

The SAX API has a number of important advantages… ƒ You can write very fast SAX parsers ƒ No memory to allocate, data structures to link ƒ Fire and forget ƒ It is useful for large documents ƒ Loading the whole document into memory is prohibitive ƒ It is easy to use …but it doesn't solve every problem ƒ Need to have an internal data structure for some applications ƒ To follow links in information (especially backwards ones) ƒ To perform operations that require having multiple pieces of the document at the same time Enter the DOM…

6 Copyright © 2007, David J. Malan . All Rights Reserved.

DOM Level 3 By Example Document

Comment Jim Bob graduate Text: \n\t

Text: \n\t\t

Element: name

Text: Jim Bob

Element: students

Element: student

Text: \n\t\t

Attr: id

Element: status

Text: \n

Text: \n\t

Text: graduate

7 Copyright © 2007, David J. Malan . All Rights Reserved.

DOM Level 3 By Definition ƒ ƒ ƒ

The result of parsing a document with a DOM parser is a DOM tree that matches the structure of that document After parsing is complete, the tree data can be used by application A DOM tree may be different than trees you have seen in the past ƒ There are different types of nodes in the tree ƒ Only some nodes can have children ƒ For nodes that are allowed children, there is no limit on the number of child nodes ƒ Attributes can grow the tree "horizontally" as well as vertically ƒ Can think of a DOM tree as a hybrid of list and tree concepts

8 Copyright © 2007, David J. Malan . All Rights Reserved.

DOM Level 3 By Definition ƒ

ƒ

Presents a language-neutral interface for manipulating hierarchical documents ƒ Used for both (X)HTML and XML Object hierarchy: every object type represents a component of the XML information model

Node

Element

Attr

Text

Document

Comment

...

9 Copyright © 2007, David J. Malan . All Rights Reserved.

DOM Level 3 Relationship with SAX ƒ ƒ

ƒ

Although the result of using a DOM parser and a SAX parser may seem very different… …both DOM and SAX are methods for encoding the structure and content of an XML document ƒ SAX does this by the type and order of events that are invoked ƒ DOM does this by using objects in a tree data-structure In fact, it is possible to create a DOM tree from a series of SAX events ƒ One of the things you have to do in Project 1!

10 Copyright © 2007, David J. Malan . All Rights Reserved.

DOM Level 3 A Sample Document



11 Copyright © 2007, David J. Malan . All Rights Reserved.

DOM Level 3 Relationship with SAX Document

DOM Document

Element: students

Handler

Text: \n\t

Element: student

Attr: id

Text: \n

startDocument(); startElement("students", {}); characters("\n\t"); startElement("student", {("id", "0001")}); endElement("student"); characters("\n"); endElement("students"); endDocument(); 12 Copyright © 2007, David J. Malan . All Rights Reserved.

DOM Level 3 Nodes

Node

Element

Attr

Text

Document

Comment

...

13 Copyright © 2007, David J. Malan . All Rights Reserved.

DOM Level 3 Nodes ƒ ƒ

ƒ ƒ

14

All objects in the DOM tree implement a Node interface The Node interface contains methods to get ƒ a name (used to store the name of the node) ƒ a value (used to store the value of the node, if any) ƒ a child list (a list of nodes that are children of the current node) ƒ a list of attributes ƒ the parent of the node Not every node subtype has meaningful data to return from these methods (e.g., only Element has attributes) Provides most of the functionality you ever want on a node ƒ Get the children of an element ƒ Get the value of a text node ƒ Modify the DOM tree by adding or removing elements ƒ … Copyright © 2007, David J. Malan . All Rights Reserved.

DOM Level 3 Interfaces ƒ

ƒ

ƒ

The W3C defined the DOM interfaces for a languageneutral data structure ƒ In Java, these interfaces are in the org.w3c.dom package In any one language, applications can use the interfaces without ever "seeing" the actual implementation ƒ In Java, you program against org.w3c.dom.Node and not, e.g., org.apache.xerces.dom.NodeImpl In My First XML Parser, we ƒ don't use the org.w3c.dom interfaces ƒ simplify by using a Node base class and subclasses instead of separating an interface from an implementation

15 Copyright © 2007, David J. Malan . All Rights Reserved.

DOM Level 3 Document Document

ƒ ƒ

ƒ ƒ

At the root of the XML DOM is a Document object ƒ This is not the same as the root element! Can have content that is valid at the top level of an XML document ƒ Processing instructions, comments Also contains the (one and only one) document element Contains functions for creating other types of DOM Nodes ƒ Remember, the DOM specifies an interface, not an implementation! ƒ This design pattern is known as a factory

16 Copyright © 2007, David J. Malan . All Rights Reserved.

DOM Level 3 Element Element

ƒ ƒ

The most "interesting" object in the DOM tree, as it makes up most of the structure Adds a few additional utility functions on top of the Node interface for manipulating attributes

17 Copyright © 2007, David J. Malan . All Rights Reserved.

DOM Level 3 Attr Attr

ƒ ƒ

Somewhat special in the DOM hierarchy in that it is not part of the DOM tree proper Elements have a list of attributes attached

18 Copyright © 2007, David J. Malan . All Rights Reserved.

DOM Level 3 ... ...

ƒ

ƒ

Most of the other DOM types are relatively simple, and use the name and value fields defined by the base Node interface CDATASection, Comment, ProcessingInstruction, and Text, for instance, all fall into this category

19 Copyright © 2007, David J. Malan . All Rights Reserved.

JAXP 1.3 and Xerces 2.7.1 DocumentBuilderDemo javax.xml.parsers.DocumentBuilderFactory javax.xml.parsers.DocumentBuilder org.w3c.dom.* ...

20 Copyright © 2007, David J. Malan . All Rights Reserved.

JAXP 1.3 and Xerces 2.7.1 Namespaces ƒ ƒ

ƒ

Many of JAXP’s APIs mention XML namespaces Namespaces are a way to specify groupings of tag and attribute names so that two names with different meanings don't “collide” ƒ For example, the element “name” may refer to a person in a student markup language, but may refer to a book in a library markup language Allow you to specify a namespace, local name, and fully qualified name ƒ studentml:name Namespace URI

Local Name

QName

ƒ

More to come...

21 Copyright © 2007, David J. Malan . All Rights Reserved.

My First XML Parser DOMBuilderDemo cscie259.project1.mf.*

22 Copyright © 2007, David J. Malan . All Rights Reserved.

Computer Science E-259 Next Time ƒ ƒ ƒ ƒ ƒ

CSS Level 2 XPath 1.0 XSLT 1.0 TrAX Project 2

23 Copyright © 2007, David J. Malan . All Rights Reserved.

Computer Science E-259 XML with Java, Java Servlet, and JSP

Lecture 3: DOM Level 3 1 October 2007 David J. Malan [email protected]

24 Copyright © 2007, David J. Malan . All Rights Reserved.

Computer Science E-259

Oct 1, 2007 - By Definition. ▫ The result of parsing a document with a DOM parser is a. DOM tree that matches the structure of that document. ▫ After parsing is ...

58KB Sizes 2 Downloads 342 Views

Recommend Documents

The Future of Computer Science - Cornell Computer Science
(Cornell University, Ithaca NY 14853, USA). Abstract ... Where should I go to college? ... search engine will provide a list of automobiles ranked according to the preferences, .... Rather, members of a community, such as a computer science.

Computer Science E-259 Lectures - Computer Science E-259: XML ...
Sep 17, 2007 - most important new technology development of the last two years." Michael Vizard ... applications: what are the tools and technologies necessary to put ... XML. When. ▫ The World Wide Web Consortium (W3C) formed an XML.

Computer Science E-259
Jan 7, 2008 - Yahoo! UI Library http://developer.yahoo.com/yui/ ..... how to program in JavaScript and PHP, how to configure. Apache and MySQL, how to ...

Computer Science E-259
Nov 19, 2007 - labeling the information content of diverse data sources .... .... ELEMENT article (url, headline_text, source, media_type, cluster,.

TEXTS IN COMPUTER SCIENCE
Java — Designed as a language to support mobile programs, Java has special .... We offer a few low-level coding hints that are helpful in building quality programs. ...... cheap in selecting your table size or else you will pay the price later.

Computer Science E-259
Oct 1, 2007 - DOCTYPE students SYSTEM "student.dtd">.

Computer Science E-259
Nov 29, 2007 - these foundations, the course will explore in detail a number of case studies that utilize XML in e-business: e-commerce, web personalization, ...

COMPUTER SCIENCE - Pune University
Poona College of Arts, Science and Commerce, Pune 411 001. 7. 001. 070 ... Sinhagad Technical Education Society's B.C.S. College, Pune 411 041.( 878-.

Computer Science E-259
Dec 3, 2007 - Redefines simple and complex types, groups, and attribute groups from an external schema redefine. Describes the format of non-XML data ...

BS Computer Science - GCUF
Nov 1, 2015 - GOVERNMENT COLLEGE UNIVERSITY, FAISALABAD. 2nd MERIT LIST OF BS Computer Science (EVENING). FOR FALL, 2015-2016.

Computer Science E-259
Nov 19, 2007 - ELEMENT article (url, headline_text, source, media_type, cluster, tagline, document_url ... http://www.oasis-open.org/specs/index.php#dbv4.1.

Computer Science E-259
Oct 22, 2007 - Computer Science E-259. XML with Java. Lecture 5: ... XPath 1.0. ▫ Location Paths. ▫ Data Types ... Data Types. ▫ boolean. ▫ number. ▫ string.

Computer Science E-259
Nov 29, 2007 - students with previous Java programming and web development experience, this course introduces XML as a key enabling technology in today's e-business applications. Students will learn the fundamentals of XML: schemas, XSL stylesheets,

Computer Science E-259
Oct 22, 2007 - 6. Copyright © 2007, David J. Malan . All Rights Reserved. XSLT 1.0, Continued. Data Types. ▫ boolean. ▫ number. ▫ string. ▫ node-set. ▫ external object. ▫ result tree fragment ...

Computer Science E-259
Jan 7, 2008 - . 4019 2445 .... with SQL, and how to use Ajax with both XML and JSON. The course ...

Computer Science E-259
Oct 1, 2007 - structure and content of an XML document. ▫ SAX does this by the type and order of events that are invoked. ▫ DOM does this by using objects in ...

BS Computer Science - GCUF
Nov 1, 2015 - GOVERNMENT COLLEGE UNIVERSITY, FAISALABAD. 2nd MERIT LIST OF BS Computer Science (EVENING). FOR FALL, 2015-2016.

TEXTS IN COMPUTER SCIENCE
thousand bright students, so look there for errata and revised solutions. ..... content, just like the house numbers on a street permit access by address, not ...

pdf-1466\communication-networks-computer-science-computer ...
... of the apps below to open or edit this item. pdf-1466\communication-networks-computer-science-computer-networking-by-cram101-textbook-reviews.pdf.

computer / information technology / computer science & engineering
GUJARAT TECHNOLOGICAL UNIVERSITY. B.E Semester: 4. Computer Engineering/ Computer Science & Engineering/. Information Technology. Subject Name ...