Presentation is loading. Please wait.

Presentation is loading. Please wait.

5 Processing XML 5 - 2 Parsing XML documents  Document Object Model (DOM)  Simple API for XML (SAX) Class generation Overview.

Similar presentations


Presentation on theme: "5 Processing XML 5 - 2 Parsing XML documents  Document Object Model (DOM)  Simple API for XML (SAX) Class generation Overview."— Presentation transcript:

1

2 5 Processing XML

3 5 - 2 Parsing XML documents  Document Object Model (DOM)  Simple API for XML (SAX) Class generation Overview

4 5 - 3 What's the Problem? The XML Handbook Goldfarb Prescod Prentice Hall 655 0130811521 44.95 XML Design Spencer Wrox Press... ? Book ?

5 5 - 4 Parsing XML Documents Document Tree Parser Docu- ment DTD / Schema Application implements DocumentHandler endDocument startDocument endElement startElement DOM SAX

6 5 - 5 Parser Project X (Sun Microsystems) Ælfred (Microstar Software) XML4J (IBM) Lark (Tim Bray) MSXML (Microsoft) XJ (Data Channel) Xerces (Apache)...

7 5 - 6 Prescod book Prentice Hall The XML Handbook Goldfarb Prescod Prentice Hall 655 0130811521 44.95 XML Design Spencer Wrox Press... The Document Object Model XML Document Structure The XML Handbook Goldfarb655 books book publisherpagesisbnauthortitle...

8 5 - 7 The Document Object Model Provides a standard interface for access to and manipulation of XML structures. Represents documents in the form of a hierarchy of nodes. Is platform- and programming-language-neutral Is a recommendation of the W3C (October 1, 1998) Is implemented by many parsers

9 5 - 8 DOM - Structure Model Document Node NodeList Element Prescod book Prentice Hall The XML Handbook Goldfarb655 books book publisherpagesisbnauthortitle...

10 5 - 9 The Document Interface Method Result docType implementation documentElement getElementsByTagName(String) createTextNode(String) createComment(String) createElement(String) create CDATASection(String) DocumentType DOMImplementation Element NodeList String Comment Element CDATASection

11 5 - 10 The Node Interface Method Result nodeName nodeValue nodeType parentNode childNodes firstChild lastChild previousSibling nextSibling attributes insertBefore(Node new,Node ref) replaceChild(Node new,Node old) removeChild(Node) hasChildNode String short Node NodeList Node NodeNamedMap Node Boolean

12 5 - 11 Node Types / Node Names Result: NodeType /NodeName Node Node Node Fields Type Name ELEMENT_NODE 1 tagName ATTRIBUTE_NODE 2 name of attribute TEXT_NODE 3 "#text" CDATA_SECTION_NODE 4 "#cdata-section" ENTITY_REFERENCE_NODE 5 name of entity referenced ENTITY_NODE 6 entity name PROCESSING_INSTRUCTION_NODE 7 target COMMENT_NODE 8 "#comment" DOCUMENT_NODE 9 "#document" DOCUMENT_TYPE_NODE10 document type name DOCUMENT_FRAGMENT_NODE11 "#document-fragment" NOTATION_NODE12 notation name

13 5 - 12 The NodeList Interface Method Result length item(int) Int Node

14 5 - 13 The Element Interface Method Result tagName getAttribute(String) setAttribute(String name, String value) removeAttribute(String) getAttributeNode(String) setAttributeNode(Attr) removeAttributeNode(String) getElementsByTagName String Attr NodeList

15 5 - 14 DOM Methods for Navigation firstChildlastChild nextSiblingpreviousSibling parentNode getElementsByTagName childNodes (length, item())

16 5 - 15 DOM Methods for Manipulation appendChild insertBefore replaceChild removeChild createElement createAttribute createTextNode

17 5 - 16 Example Goldfarb Spencer books book author Prescod doc.documentElement.childNodes.item(0).getElementsByTagName("author"). item(1).childNodes.item(0).data doc.documentElement.childNodes.item(0).getElementsByTagName("author"). item(1).childNodes.item(0).data Root Node DOM Object TextBooks second Author Text Subnodes first thereof first Book Authors

18 5 - 17 Script DOM Example DOM Example var doc, root, book1, authors, author2; doc = new ActiveXObject("Microsoft.XMLDOM"); doc.async = false; doc.load("books.xml"); if (doc.parseError != 0) alert(doc.parseError.reason); else { root = doc.documentElement; document.write("Name of Root node: " + root.nodeName + " "); document.write("Type of Root node: " + root.nodeType + " "); book1 = root.childNodes.item(0); authors = book1.getElementsByTagName("author"); document.write("Number of authors: " + authors.length + " "); author2 = authors.item(1); document.write("Name of second author: " + author2.childNodes.item(0).data);} DOM Example DOM Example var doc, root, book1, authors, author2; doc = new ActiveXObject("Microsoft.XMLDOM"); doc.async = false; doc.load("books.xml"); if (doc.parseError != 0) alert(doc.parseError.reason); else { root = doc.documentElement; document.write("Name of Root node: " + root.nodeName + " "); document.write("Type of Root node: " + root.nodeType + " "); book1 = root.childNodes.item(0); authors = book1.getElementsByTagName("author"); document.write("Number of authors: " + authors.length + " "); author2 = authors.item(1); document.write("Name of second author: " + author2.childNodes.item(0).data);}

19 5 - 18 SAX - Simple API for XML Docu- ment DTD Application endDocument startDocument endElement startElement Parser

20 5 - 19 SAX - Simple API for XML Event-driven parsing model "Don't call the DOM, the parser calls you." Developed by the members of the XML-DEV Mailing List Released on May 11, 1998 Supported by many parsers...... but Ælfred is the saxon king.

21 5 - 20 Procedure DOM  Creating a parser instance  Parsing the whole document  Processing the DOM tree SAX  Creating a parser instance  Registrating event handlers with the parser  Parser calls the event handler during parsing

22 5 - 21 Namespace Support <orderxmlns="http://www.net-standard.com/namespaces/order" xmlns:bk="http://www.net-standard.com/namespaces/books" xmlns:cust="http://www.net-standard.com/namespaces/customer" >... XML Handbook 0130811521....

23 5 - 22 Access to Qualified Elements Node "book" bk:book http://www.net-standard.com/namespaces/books bk book Interface "Node" DOM Level 2 Method nodeName namespaceURI prefix localName qName uri localName SAX 2.0 startElement

24 5 - 23 Generation of Data Structures DTD / Schema 'yacht' Generation 01yacht 05name 05details 10type Class Processing Mona Lisa Any text describing this yacht 147 GULFSTAR 55 ength>1700 480 170 112 84 202 8 01yacht 05VENTANA 05details 10GULFSTAR 55 Object

25 5 - 24 Summary To avoid expensive text processing, applications use an XML parser that creates a DOM tree of a document. The DOM provides a standardized API to access the content of documents and to manipulate them. Alternatively or additionally, applications can work event-based using the SAX interface, which is provided by many parsers.


Download ppt "5 Processing XML 5 - 2 Parsing XML documents  Document Object Model (DOM)  Simple API for XML (SAX) Class generation Overview."

Similar presentations


Ads by Google