1 Dr Alexiei Dingli XML Technologies SAX and DOM.

50
1 Dr Alexiei Dingli XML Technologies SAX and DOM

Transcript of 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

Page 1: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

1

Dr Alexiei Dingli

XML Technologies

SAX and DOM

Page 2: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

2

• Simple API for XML

• Used to parse XML

• But does not create a default object

• It just fires events when it detects objects such as– open or close tags– PCDATA or CDATA– Comments– entities

What is SAX?

Page 3: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

3

• Imagine the following document:

<?xml version = "1.0"?>

<addressbook>

<person>

<lastname>Dingli</lastname>

<firstname>Alexiei</firstname>

<company>University of Malta</company>

<email>[email protected]</email>

</person>

</addressbook>

Example

Page 4: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

4

1. Creating a custom object model (like Person and AddressBook classes)

2. Creating a SAX parser

3. Creating a DocumentHandler (to turn your XML document into instances of your custom object model).

SAX in 3 steps

Page 5: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

5

• Create both a person and an address book object

• Create its setters, getters and to xml methods

Custom Object Model (1)

Page 6: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

6

Custom Object Model

(2)

Page 7: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

7

Create a SAX

parser

Page 8: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

8

• Actually 4 Interfaces ...

– The Document Handler

– The Entity Resolver

– The DTD Handler

– The Error Handler

Create a Document Handler (1)

Page 9: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

9

Create a Document Handler (2)

Page 10: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

10

parser.setDocumentHandler( ... )

parser.setDTDHandler( ... )

parser.setErrorHandler( ... )

Setting the parser

Page 11: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

11

• Rather than implementing all the interfaces mentioned earlier

• Make use of org.xml.sax.helpers.DefaultHandler

• Which implements all the methods

• And you simply override what you want to use

• http://java.sun.com/j2se/1.4.2/docs/api/org/xml/sax/helpers/DefaultHandler.html

Handler Class

Page 12: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

12

SAX Handler

Example Handler

Page 13: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

13

• W3C standard

• Standard way of accessing and manipulating documents

• Divided into 3 parts

– Core DOM (access any structured document)

– XML DOM

– HTML DOM

• Presents element as a tree structure

DOM

Page 14: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

14

• A standard object model for XML

• A standard programming interface for XML

• Platform- and language-independent

• A W3C standard

• The XML DOM is a standard for how to get, change, add, or delete XML elements

XML DOM

Page 15: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

15

Everything in XML is a node

– The entire document is a document node– Every XML element is an element node– The text in the XML elements are text nodes– Every attribute is an attribute node– Comments are comment nodes

XML DOM rulez

Page 16: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

16

<bookstore>

<book category="web" cover="paperback">

<title lang="en">Learning XML</title>

<year>2008</year>

</book>

</bookstore>

• Bookstore is the root node

• It contains one book node

• A book node contains a title node and a year node

• Title contains a text node “Learning XML”

• 2008 is not the value of the year node but a text node inside the year node

Example

Page 17: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

17

• Any DOM object has a node tree where– In a node tree, the top node is called the root– Every node, except the root, has exactly one

parent node– A node can have any number of children– A leaf is a node with no children– Siblings are nodes with the same parent

The node tree

Page 18: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

18

text="<bookstore>"

text=text+"<book>";

text=text+"<title>Everyday Italian</title>";

text=text+"<author>John Smith</author>";

text=text+"<year>2008</year>";

text=text+"</book>";

text=text+"</bookstore>";

Creating the XML

Page 19: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

19

try //Internet Explorer

{

xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async="false";

xmlDoc.loadXML(text);

} catch(e) {

try //Firefox, Mozilla, Opera, etc.

{

parser=new DOMParser(); xmlDoc=parser.parseFromString(text,"text/xml");

} catch(e) {

alert(e.message)

}

}

document.write("xmlDoc is loaded, ready for use");

Parsing the XML

Page 20: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

20

• x.getElementsByTagName(name) - get all elements with a specified tag name

• x.appendChild(node) - insert a child node to x

• x.removeChild(node) - remove a child node from x

XML DOM Methods

Page 21: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

21

• x.nodeName - the name of x

• x.nodeValue - the value of x

• x.parentNode - the parent node of x

• x.childNodes - the child nodes of x

• x.attributes - the attributes nodes of x

XML DOM properties

Page 22: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

22

document.write(xmlDoc.getElementsByTagName("title") [0].childNodes[0].nodeValue);

document.write("<br />");

document.write(xmlDoc.getElementsByTagName("author") [0].childNodes[0].nodeValue);

document.write("<br />");

document.write(xmlDoc.getElementsByTagName("year") [0].childNodes[0].nodeValue);

Examples

Page 23: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

23

1. By using the getElementsByTagName() method

2. By looping through (traversing) the nodes tree

3. By navigating the node tree, using the node relationships

Accessing nodes

Page 24: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

24

xmlDoc.getElementsByTagName("title") [0].childNodes[0].nodeValue;

Example 1

Page 25: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

25

x=xmlDoc.getElementsByTagName("title");

for ( i=0; i<x.length; i++) { document.write(x[i].childNodes[0].nodeValue); document.write("<br />");

}

Example 2

Page 26: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

26

x=xmlDoc.getElementsByTagName("book")[0].childNodes;

y=xmlDoc.getElementsByTagName("book")[0].firstChild;

for (i=0;i<x.length;i++) {

if (y.nodeType==1) {//Process only element_nodes (type 1)

document.write(y.nodeName + "<br />");

}

y=y.nextSibling;

}

Example 3

Page 27: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

27

• nodeName

• nodeValue

• nodeType

Node properties

Page 28: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

28

• nodeName is read-only

• nodeName of an element node is the same as the tag name

• nodeName of an attribute node is the attribute name

• nodeName of a text node is always #text

• nodeName of the document node is always #document

nodeName property

Page 29: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

29

• nodeValue for element nodes is undefined

• nodeValue for text nodes is the text itself

• nodeValue for attribute nodes is the attribute value

nodeValue property

Page 30: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

30

Node type NodeType

Element 1

Attribute 2

Text 3

Comment 8

Document 9

nodeType property

Page 31: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

31

x=xmlDoc.getElementsByTagName("book")[0].attributes;

document.write(x.getNamedItem("category").nodeValue);

Acessing node attributes

Page 32: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

32

// documentElement always represents the root node

x=xmlDoc.documentElement.childNodes;

for (i=0;i<x.length;i++) {

document.write(x[i].nodeName); document.write(": "); document.write(x[i].childNodes[0].nodeValue); document.write("<br />");

}

Traversing Example

Page 33: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

33

• parentNode

• childNodes

• firstChild

• lastChild

• nextSibling

• previousSibling

Navigating Nodes (1)

Page 34: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

34

Navigating Nodes (2)

Page 35: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

35

x=xmlDoc.getElementsByTagName("title")[0];

y=x.childNodes[0];

txt=y.nodeValue;

Result = the name of the book

Title node > Text node

Getting the node value

Page 36: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

36

x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];

x.nodeValue="Easy Cooking";

Setting the node value

Page 37: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

37

y=xmlDoc.getElementsByTagName("book")[0];

xmlDoc.documentElement.removeChild(y);

Or

y.parentNode.removeChild(y);

Removing Nodes

Page 38: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

38

newel=xmlDoc.createElement("edition");

x=xmlDoc.getElementsByTagName("book")[0];

x.appendChild(newel);

Creating nodes

Page 39: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

39

newel=xmlDoc.createElement("edition");

newtext=xmlDoc.createTextNode("first");

newel.appendChild(newtext);

x=xmlDoc.getElementsByTagName("book")[0];

x.appendChild(newel);

Creating text nodes

Page 40: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

40

newCDATA=xmlDoc.createCDATASection("Special Offer & Book Sale");

x=xmlDoc.getElementsByTagName("book")[0];

x.appendChild(newCDATA);

Create CDATA nodes

Page 41: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

41

newComment=xmlDoc.createComment("Revised March 2008");

x=xmlDoc.getElementsByTagName("book")[0];

x.appendChild(newComment);

Create Comment Node

Page 42: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

42

x.appendChild(newNode)

x.insertBefore(newNode,y)

x.cloneNode(true) // add all attributes and children if true

x.insertData(offset,"Easy "); // add text

More additional methods

Page 43: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

43

x=xmlDoc.getElementsByTagName("title")[0].getAttributeNode("lang");

txt=x.nodeValue;

Getting attribute value

Page 44: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

44

newatt=xmlDoc.createAttribute("edition");

newatt.nodeValue="first";

x=xmlDoc.getElementsByTagName("title");

x[0].setAttributeNode(newatt);

Creating attributes

Page 45: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

45

x=xmlDoc.getElementsByTagName('book');

x[0].setAttribute("category","food");

Or

x=xmlDoc.getElementsByTagName("book")[0]

y=x.getAttributeNode("category");

y.nodeValue="food";

Setting the attribute value

Page 46: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

46

x=xmlDoc.getElementsByTagName("book");

x[0].removeAttribute("category");

Removing attributes

Page 47: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

47

• Given the following XML file

• How shall we display – Two buttons

• “Get CD info” and display the Titles and the Composer

• “Get CD info abridged” and display the Titles only

Exercise

Page 48: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

48

• The code

• What’s the result?

Answer (1)

Page 49: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

49

Answer (2)

Page 50: 1 Dr Alexiei Dingli XML Technologies SAX and DOM.

50

Questions?