Adminisztratív November 24.-én az előadás és a gyakorlat elmarad A nagy gyakorlati feladatok beszedése a december 1.-én tartandó gyakorlatra tolódik A.

Slides:



Advertisements
Similar presentations
XML IV. The Document Object Model The Document Object model is a hierarchical structure of an XML document. It provides a means for accessing, and manipulating.
Advertisements

SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part II – Java API) /Spring, Bahçeşehir University, Istanbul.
SDPL : Document Object Model1 n How to provide uniform access to structured documents in parsers, browsers, editors, databases,...? - DOM, and how.
SDPL 2002Notes 3: XML Processor Interfaces1 3.3 JAXP: Java API for XML Processing n How can applications use XML processors? –A Java-based answer: through.
1 Chapter 12 Working With Access 2000 on the Internet.
1 The Information School of the University of Washington Nov 1fit dom © 2006 University of Washington Document Object Model (DOM) INFO/CSE 100, Fall.
14-Jun-15 DOM. SAX and DOM SAX and DOM are standards for XML parsers-- program APIs to read and interpret XML files DOM is a W3C standard SAX is an ad-hoc.
1 The Information School of the University of Washington Nov 8fit review © 2006 University of Washington Midterm 2 Review INFO/CSE 100, Fall 2006.
XML DOM and SAX Parsers By Omar RABI. Introduction to parsers  The word parser comes from compilers  In a compiler, a parser is the module that reads.
Apache DOM Parser©zwzOctober 24, 2002 Wenzhong Zhao Department of Computer Science The University of Kentucky.
SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part I) /Spring, Bahçeşehir University, Istanbul.
Xpath Sources:
By: Shawn Li. OUTLINE XML Definition HTML vs. XML Advantage of XML Facts Utilization SAX Definition DOM Definition History Comparison between SAX and.
The Document Object Model (DOM) & Asynchronous Javascript And XML (AJAX) : an introduction UFCEKG-20-2 : Data, Schemas and Applications.
DHTML. What is DHTML?  DHTML is the combination of several built-in browser features in fourth generation browsers that enable a web page to be more.
4.1 JavaScript Introduction
1 XML Data Management 4. Domain Object Model Werner Nutt.
HTML DOM.  The HTML DOM defines a standard way for accessing and manipulating HTML documents.  The DOM presents an HTML document as a tree- structure.
SDPL 2003Notes 3: XML Processor Interfaces1 3. XML Processor APIs n How can applications manipulate structured documents? –An overview of document parser.
1 Document Object Model (DOM) MV4920 – XML 24 September 2001 Simon R. Goerger MAJ, US Army
Structured-Document Processing Languages Spring 2011 Course Review Repetitio mater studiorum est!
XML Data Processing and Transformation ดร. มารุต บูรณรัช : หัวข้อพิเศษด้านเทคโนโลยีสารสนเทศขั้นสูง.
SDPL 2002Notes 3.2: Document Object Model1 XML 접근과 관련된 자료.
XP New Perspectives on XML, 2 nd Edition Tutorial 10 1 WORKING WITH THE DOCUMENT OBJECT MODEL TUTORIAL 10.
Lecture 2 : Understanding the Document Object Model (DOM) UFCFR Advanced Topics in Web Development II 2014/15 SHAPE Hong Kong.
Structured-Document Processing Languages Spring 2005 Course Review Repetitio mater studiorum est!
3/29/2001 O'Reilly Java Java API for XML Processing 1.1 What’s New Edwin Goei Engineer, Sun Microsystems.
SDPL 2002Notes 3: XML Processor Interfaces1 3. XML Processor APIs n How can applications manipulate structured documents? –An overview of document parser.
SDPLNotes 3.3: DOM Examples1 3.3 DOM Examples 1. DOM processing in a Web browser –a toy "XML database browser" –with JavaScript and MS Internet Explorer.
Electronic Commerce COMP3210 Session 4: Designing, Building and Evaluating e-Commerce Initiatives – Part II Dr. Paul Walcott Department of Computer Science,
Working with the XML Document Object Model ©NIITeXtensible Markup Language/Lesson 7/Slide 1 of 44 Objectives In this lesson, you will learn to: *Identify.
The XML Document Object Model (DOM) Aug’10 – Dec ’10.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
XML Processing in Java. Required tools Sun JDK 1.4, e.g.: JAXP (part of Java Web Services Developer Pack, already in Sun.
Java API for XML Processing (JAXP) Dr. Rebhi S. Baraka Advanced Topics in Information Technology (SICT 4310) Department of Computer.
SDPL 2002Notes 3.2: Document Object Model1 3.2 Document Object Model (DOM) n How to provide uniform access to structured documents in diverse applications.
McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Scripting with the DOM Ellen Pearlman Eileen Mullin Programming the Web.
Sheet 1XML Technology in E-Commerce 2001Lecture 3 XML Technology in E-Commerce Lecture 3 DOM and SAX.
Introduction to XML This presentation covers introductory features of XML. What XML is and what it is not? What does it do? Put different related technologies.
1 Dr Alexiei Dingli XML Technologies SAX and DOM.
XML Study-Session: Part III
Document Object Model DOM. Agenda l Introduction to DOM l Java API for XML Parsing (JAXP) l Installation and setup l Steps for DOM parsing l Example –Representing.
SDPLNotes 3.2: DOM1 3.2 Document Object Model (DOM) n How to provide uniform access to structured documents in diverse applications (parsers, browsers,
Apache DOM Parser©zwzOctober 24, 2002 Wenzhong Zhao Department of Computer Science The University of Kentucky.
Schema Data Processing
Structured-Document Processing Languages Spring 2007 Course Review Repetitio mater studiorum est!
1 The Information School of the University of Washington Nov 1fit dom © 2006 University of Washington Document Object Model (DOM)
1 Introduction JAXP. Objectives  XML Parser  Parsing and Parsers  JAXP interfaces  Workshops 2.
Structured-Document Processing Languages Spring 2004 Course Review Repetitio mater studiorum est!
Web services. DOM parsing and SOAP.. Summary. ● Exercise: SAX-Based checkInvoice(), ● push parsing, ● event-based parsing, ● traversal order is depth-first.
SDPL 2002Notes 3.2: Document Object Model1 The Misconceived Web n The original vision of the WWW was as a hyperlinked document-retrieval system. n It did.
Document Object Model (DOM). Outline  Introduction of DOM  Overview of DOM  DOM Relationships  Standard DOM.
SDPL 20063: XML Processor Interfaces1 3. XML Processor APIs n How can (Java) applications manipulate structured (XML) documents? –An overview of XML processor.
Document Object Model.  The XML DOM (Document Object Model) defines a standard way for accessing and manipulating XML documents.  The DOM presents an.
13-Mar-16 DOM. 2 Difference between SAX and DOM DOM reads the entire XML document into memory and stores it as a tree data structure SAX reads the XML.
USING ANDROID WITH THE DOM. Slide 2 Lecture Summary DOM concepts SAX vs DOM parsers Parsing HTTP results The Android DOM implementation.
XML DOM Week 11 Web site:
DHTML.
XML Parsers Overview Types of parsers Using XML parsers SAX DOM
Document Object Model (DOM)
Document Object Model (DOM)
Java/XML.
{ XML Technologies } BY: DR. M’HAMED MATAOUI
Document Object Model (DOM)
Week 11 Web site: XML DOM Week 11 Web site:
Document Object Model (DOM)
XML Parsers Overview Types of parsers Using XML parsers SAX DOM
Document Object Model (DOM)
DOM 24-Feb-19.
WaysInJavaToParseXML
Presentation transcript:

Adminisztratív November 24.-én az előadás és a gyakorlat elmarad A nagy gyakorlati feladatok beszedése a december 1.-én tartandó gyakorlatra tolódik A november 24.-én elmaradt előadást december 1.-én csütörtökön délután ig pótoljuk az A/1 228-as teremben.

Document object model

Aims How to provide uniform access to structured documents in diverse applications (parsers, browsers, editors, databases)? Overview of W3C DOM Specification –second one in the “XML-family” of recommendations Level 1, W3C Rec, Oct Level 2, W3C Rec, Nov Level 3, W3C Working Draft (January 2002) What does DOM specify, and how to use it?

DOM: What is it? An object-based, language-neutral API for XML and HTML documents –allows programs and scripts to build documents, navigate their structure, add, modify or delete elements and content –Provides a foundation for developing querying, filtering, transformation, rendering etc. applications on top of DOM implementations In contrast to “Serial Access XML” could think as “Directly Obtainable in Memory”

What the is the DOM? Document Object Model – Your web browser builds a model of the web page (the document) that includes all the objects in the page (tags, text, etc) – All of the properties, methods, and events available to the web developer for manipulating and creating web pages are organized into objects – Those objects are accessible via scripting languages in modern web browsers

DOM structure model Based on O-O concepts: –methods (to access or change object’s state) –interfaces (declaration of a set of methods) –objects (encapsulation of data and methods) Structure –Tree-like structure implied by the abstract relationships defined by the programming interfaces; Does not necessarily reflect data structures used by an implementation (but probably does)

DOM tree DOM Tree High Level XML Update Aimed at Object Orientated Programmers Stores XML in Tree Structure

Why is this useful? Because we can access the model too! – the model is made available to scripts running in the browser, not just the browser itself A script can find things out about the state of the page A script can change things in response to events, including user requests

Minimal DOM DOM Core Level 3 DOM Core Level 2 DOM Core Level 1 Application Based Consider Structural Behaviour Only

Structure of DOM Level 1 I: DOM Core Interfaces –Fundamental interfaces basic interfaces to structured documents –Extended interfaces XML specific: CDATASection, DocumentType, Notation, Entity, EntityReference, ProcessingInstruction II: DOM HTML Interfaces –more convenient to access HTML documents

DOM Level 2 –Level 1: basic representation and manipulation of document structure and content (No access to the contents of a DTD) DOM Level 2 adds –support for namespaces –accessing elements by ID attribute values –optional features interfaces to document views and style sheets an event model (for, say, user actions on elements) methods for traversing the document tree and manipulating regions of document (e.g., selected by the user of an editor) –Loading and writing of docs not specified (-> Level 3)

SDPL 2002Notes 3.2: Document Object Model12 invoice invoicepage name addressee addressdata address form="00"type="estimatedbill" Leila Laskuprintti streetaddresspostoffice KUOPIO Pyynpolku 1 <invoice> <invoicepage form="00" <invoicepage form="00" type="estimatedbill"> type="estimatedbill"> Leila Laskuprintti Leila Laskuprintti Pyynpolku 1 Pyynpolku KUOPIO KUOPIO......Document Element NamedNodeMap Text DOM structure model

Sample DOM Document An HTML Document This is a simple document. This is what the browser reads This is what the browser displays on screen.

Document "Sample DOM Document" "An HTML Document" "This is a" "simple" "document" This is a drawing of the model that the browser is working with for the page.

DOM Language Bindings Language-independence: –DOM interfaces are defined using OMG Interface Definition Language (IDL; Defined in Corba Specification) Language bindings (implementations of DOM interfaces) defined in the Recommendation for –Java and –ECMAScript (standardised JavaScript)

Core Interfaces: Node & its variants Node Comment DocumentFragmentAttr Text Element CDATASection ProcessingInstruction CharacterData EntityDocumentTypeNotation EntityReference “Extended interfaces” Document

SDPL 2002Notes 3.2: Document Object Model17 DOM interfaces: Node invoice invoicepage name addressee addressdata address form="00" type="estimatedbill" Leila Laskuprintti streetaddresspostoffice KUOPIOPyynpolku 1NodegetNodeTypegetNodeValuegetOwnerDocumentgetParentNode hasChildNodesgetChildNodes getFirstChildgetLastChildgetPreviousSiblinggetNextSibling hasAttributesgetAttributes appendChild(newChild)insertBefore(newChild,refChild)replaceChild(newChild,oldChild)removeChild(oldChild)Document Element NamedNodeMap Text

Object Creation in DOM Each DOM object X lives in the context of a Document: X.getOwnerDocument() Objects implementing interface X are created by factory methods D.create X (…), where D is a Document object. E.g: – createElement("A"), createAttribute("href"), createTextNode("Hello!") Creation and persistent saving of Document s left to be specified by implementations

SDPL 2002Notes 3.2: Document Object Model19 invoice invoicepage name addressee addressdata address form="00"type="estimatedbill" Leila Laskuprintti streetaddresspostoffice KUOPIO Pyynpolku 1 DocumentgetDocumentElementcreateAttribute(name)createElement(tagName)createTextNode(data)getDocType()getElementById(IdVal) Node Document Element NamedNodeMap Text DOM interfaces: Document

document.getElementById("radioLC").checked Reference to several nodes in the model of the page that the browser constructed document – The root of the tree is an object of type HTMLDocument – Using the global variable document, we can access all the nodes in the tree, as well as useful functions and other global information title, referrer, domain, URL, body, images, links, forms,... open, write, close, getElementById,...

Some information from a document DOM Sample 1 Information about this document. document.write(" Title: ",document.title); document.write(" Referrer: ",document.referrer); document.write(" Domain: ",document.domain); document.write(" URL: ",document.URL);

Nov 1 fit dom © 2006 University of Washington document.getElementById("radioLC").checked getElementById("radioLC") – This is a predefined function that makes use of the id that can be defined for any element in the page – An id must be unique in the page, so only one element is ever returned by this function – The argument to getElementById specifies which element is being requested

Nov 1 fit dom © 2006 University of Washington document.getElementById("radioLC").checked checked – This is a particular property of the node we are looking at, in this case, a radio button – Each type of node has its own set of properties for radio button: checked, name,... refer to the HTML DOM for specifics for each element type – Some properties can be both read and set

Nov 1 fit dom © 2006 University of Washington Getting vs. Setting var oldvalue = document.getElementById("resultField").value; document.getElementById("resultField").value = "new value";

SDPL 2002Notes 3.2: Document Object Model25 DOM interfaces: Element invoice invoicepage name addressee addressdata address form="00"type="estimatedbill" Leila Laskuprintti streetaddresspostoffice KUOPIO Pyynpolku 1 ElementgetTagNamegetAttributeNode(name)setAttributeNode(attr)removeAttribute(name)getElementsByTagName(name)hasAttribute(name) Node Document Element NamedNodeMap Text

Accessing properties of a Node – Node.getNodeName () for an Element = getTagName() for an Attr: the name of the attribute for Text = "#text" etc – Node.getNodeValue() content of a text node, value of attribute, …; null for an Element (!!) (in XSLT/Xpath: the full textual content) – Node.getNodeType() : numeric constants (1, 2, 3, …, 12) for ELEMENT_NODE, ATTRIBUTE_NODE,TEXT_NODE, …, NOTATION_NODE

Content and element manipulation Manipulating CharacterData D : –D.substringData( offset, count ) –D.appendData( string ) –D.insertData( offset, string ) –D.deleteData( offset, count ) –D.replaceData( offset, count, string ) (= delete + insert) Accessing attributes of an Element object E : Accessing attributes of an Element object E : –E.getAttribute( name ) –E.setAttribute( name, value ) –E.removeAttribute( name )

Additional Core Interfaces (1) NodeList for ordered lists of nodes –e.g. from Node.getChildNodes() or Element.getElementsByTagName("name") all descendant elements of type "name" in document order (wild-card "*" matches any element type) Accessing a specific node, or iterating over all nodes of a NodeList : Accessing a specific node, or iterating over all nodes of a NodeList : –E.g. Java code to process all children: for (i=0; i<node.getChildNodes().getLength(); i++) process(node.getChildNodes().item(i));

Additional Core Interfaces (2) NamedNodeMap for unordered sets of nodes accessed by their name: –e.g. from Node.getAttributes() NodeList s and NamedNodeMap s are "live": –changes to the document structure reflected to their contents

DOM: Implementations Java-based parsers e.g. IBM XML4J, Apache Xerces, Apache Crimson MS IE5 browser: COM programming interfaces for C/C++ and MS Visual Basic, ActiveX object programming interfaces for script languages XML::DOM (Perl implementation of DOM Level 1)

A Java-DOM Example A stand-alone toy application BuildXml –either creates a new db document with two person elements, or adds them to an existing db document –based on the example in Sect. 8.6 of Deitel et al: XML - How to program Technical basis –DOM support in Sun JAXP –native XML document initialisation and storage methods of the JAXP 1.1 default parser (Apache Crimson)

Code of BuildXml (1) Begin by importing necessary packages: import java.io.*; import org.w3c.dom.*; import org.xml.sax.*; import javax.xml.parsers.*; // Native (parse and write) methods of the // JAXP 1.1 default parser (Apache Crimson): import org.apache.crimson.tree.XmlDocument;

Code of BuildXml (2) Class for modifying the document in file fileName : public class BuildXml { private Document document; public BuildXml(String fileName) { File docFile = new File(fileName); Element root = null; // doc root elemen // Obtain a SAX-based parser: DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

Code of BuildXml (3) try { // to get a new DocumentBuilder: documentBuilder builder = factory.newInstance(); if (!docFile.exists()) { //create new doc document = builder.newDocument(); // add a comment: Comment comment = document.createComment( "A simple personnel list"); document.appendChild(comment); // Create the root element: root = document.createElement("db"); document.appendChild(root);

Code of BuildXml (4) … or if docFile already exists: } else { // access an existing doc try { // to parse docFile document = builder.parse(docFile); root = document.getDocumentElement(); } catch (SAXException se) { System.err.println("Error: " + se.getMessage() ); System.exit(1); } /* A similar catch for a possible IOException */

Code of BuildXml (5) Create and add two child elements to root : Node personNode = createPersonNode(document, "1234", "Pekka", "Kilpeläinen"); root.appendChild(personNode); personNode = createPersonNode(document, "5678", "Irma", "Könönen"); root.appendChild(personNode);

Code of BuildXml (6) Finally, store the result document: try { // to write the // XML document to file fileName ((XmlDocument) document).write( new FileOutputStream(fileName)); } catch ( IOException ioe ) { ioe.printStackTrace(); }

Subroutine to create person elements public Node createPersonNode(Document document, String idNum, String fName, String lName) { Element person = document.createElement("person"); person.setAttribute("idnum", idNum); Element firstName = document. createElement("first"); person.appendChild(firstName); firstName. appendChild( document. createTextNode(fName) ); /* … similarly for a lastName */ return person; }

The main routine for BuildXml public static void main(String args[]){ if (args.length > 0) { String fileName = args[0]; BuildXml buildXml = new BuildXml(fileName); } else { System.err.println( "Give filename as argument"); }; } // main

Summary of XML APIs XML processors make the structure and contents of XML documents available to applications through APIs Event-based APIs –notify application through parsing events –e.g., the SAX call-back interfaces Object-model (or tree) based APIs –provide a full parse tree –e.g, DOM, W3C Recommendation –more convenient, but may require too much resources with the largest documents Major parsers support both SAX and DOM

DOM Sample 3 var switchCount = 0; var adjectives = ["simple","complex","fascinating","unique"]; function switcher() { if (switchCount == (adjectives.length - 1)) switchCount = 0; else switchCount++; var italicNode = document.getElementById("adjPhrase"); italicNode.firstChild.nodeValue = adjectives[switchCount]; } An HTML Document This is a simple document. switch This is what the browser reads (dom3.html).

This is what the browser displays on screen.

Nov 1 fit dom © 2006 University of Washington References – JavaScript, The Definitive Guide by David Flanagan. Publisher O'Reilly – W3C Document Object Model – Document Object Model in Mozilla