Presentation is loading. Please wait.

Presentation is loading. Please wait.

Presentation 6: Introduction to XML and related technologies.

Similar presentations


Presentation on theme: "Presentation 6: Introduction to XML and related technologies."— Presentation transcript:

1 Presentation 6: Introduction to XML and related technologies

2 Ingeniørhøjskolen i Århus Slide 2 af 31 Outline Why an XML presentation? W3C & legacy of XML – ultra short XML markup and Namespaces DTD’s XML Schemas DOM/SAX The SOAP connection

3 Ingeniørhøjskolen i Århus Slide 3 af 31 Why do we need an XML presentation? Because SOAP, WSDL & UDDI is based on XML technologies Important to understand how the API’s work –Parsing mechanisms DOM SAX Why its slow ;)

4 Ingeniørhøjskolen i Århus Slide 4 af 31 W3C & the legacy of XML World Wide Consortium –Founded 1994 to lead the WWW into the future –For standardizations on the Internet –First Chairman: Tim Berners-Lee – founder of HTML & HTTP (or World Wide Web as we know it) –Run by Chairman, Director & Staff –Boards of members submits proposals and work to formulate –Ensures standardization of WWW technologies Like: XHTML, XML, XSL, CSS, SOAP, WAP etc. Members: Microsoft, IBM, SUN, Oracle and many others –http://www.w3c.orghttp://www.w3c.org Legacy: –Standard Generalized Markup Language (SGML) http://www.w3.org/TR/REC-html40/sgml/sgmldecl.html HTML has the same legacy - in fact / tightly coupled

5 Ingeniørhøjskolen i Århus Slide 5 af 31 XML markup eXtended Markup Language XML based on SGML (subset of) Like SGML for structure not layout (as HTML) XML targets the Internet – but is also being used for application exchange formats (Open Office, XMI) – CSVs XML is an W3C Recommendation –http://www.w3.org/TR/REC-xml Structure decided by DTD or Schema (more later) Wide spread support for XML (hype agtig)

6 Ingeniørhøjskolen i Århus Slide 6 af 31 Presenting XML documents Examples fetched from DEITEL First we will look at a standalone XML document and its components (elements) –Note: XML document needs to be Wellformed

7 Ingeniørhøjskolen i Århus Slide 7 af 31 Article.xml 1 2 3 4 5 6 7 8 Simple XML 9 10 September 19, 2001 11 12 13 Tem 14 Nieto 15 16 17 XML is pretty easy. 18 19 Once you have mastered XHTML, XML is easily 20 learned. You must remember that XML is not for 21 displaying information but for managing information. 22 23 24 Optional XML declaration. Element article is the root element. Elements title, date, author, summary and content are child elements of article.

8 Ingeniørhøjskolen i Århus Slide 8 af 31 Browser displaying XML (unformatted) IE5.5 displaying article.xml.

9 Ingeniørhøjskolen i Århus Slide 9 af 31 Use of XML Namespaces XML namespaces used to avoid naming conflicts When several different elements are involved isnt always a book Keyword ”xmlns”

10 Ingeniørhøjskolen i Århus Slide 10 af 31 Namespace.xml 1 2 3 4 5 6 <text:directory xmlns:text = "urn:deitel:textInfo" 7 xmlns:image = "urn:deitel:imageInfo"> 8 9 10 A book list 11 12 13 14 A funny picture 15 16 17 18 Keyword xmlns creates two namespace prefixes, text and image. URIs ensure that a namespace is unique.

11 Ingeniørhøjskolen i Århus Slide 11 af 31 Defaultnamespace.xml 1 2 3 4 5 6 <directory xmlns = "urn:deitel:textInfo" 7 xmlns:image = "urn:deitel:imageInfo"> 8 9 10 A book list 11 12 13 14 A funny picture 15 16 17 18 Default namespace. Element file uses the default namespace. Element file uses the namespace prefix image.

12 Ingeniørhøjskolen i Århus Slide 12 af 31 DTDs Document Type Definition Extended Backus-Naur Form Defines how an XML document is structured –Required elements –Nesting of elements –Does not define types or behaviour If DTD is used – some parsers can decide if XML document is “valid” – which is more than just “wellformed”

13 Ingeniørhøjskolen i Århus Slide 13 af 31 Letter.dtd 1 2 3 4 <!ELEMENT letter ( contact+, salutation, paragraph+, 5 closing, signature )> 6 7 <!ELEMENT contact ( name, address1, address2, city, state, 8 zip, phone, flag )> 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 The ELEMENT element type declaration defines the rules for element letter. The plus sign ( + ) occurrence indicator specifies that the DTD allows one or more occurrences of an element. (2 contacs in our example) The contact element definition specifies that element contact contains child elements name, address1, address2, city, state, zip, phone and flag — in that order.

14 Ingeniørhøjskolen i Århus Slide 14 af 31 Letter.dtd 1 2 3 4 <!ELEMENT letter ( contact+, salutation, paragraph+, 5 closing, signature )> 6 7 <!ELEMENT contact ( name, address1, address2, city, state, 8 zip, phone, flag )> 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 The ATTLIST element type declaration defines an attribute (i.e., type ) for the contact element. Keyword #IMPLIED specifies that if the parser finds a contact element without a type attribute, the parser can choose an arbitrary value for the attribute or ignore the attribute and the document will be valid. Flag #PCDATA specifies that the element can contain parsed character data (i.e., text). See letter.xml next page

15 Ingeniørhøjskolen i Århus Slide 15 af 31 Letter.xml 1 2 3 4 5 6 7 8 9 10 11 John Doe 12 123 Main St. 13 14 Anytown 15 Anystate 16 12345 17 555-1234 18 19 20 21 22 Joe Schmoe 23 Box 12345 24 15 Any Ave. 25 Othertown 26 Otherstate 27 67890 28 555-4321 29 30 31

16 Ingeniørhøjskolen i Århus Slide 16 af 31 Letter.xml Program Output 32 Dear Sir: 33 34 It is our privilege to inform you about our new 35 database managed with XML. This new system allows 36 you to reduce the load of your inventory list server by 37 having the client machine perform the work of sorting 38 and filtering the data. 39 Sincerely 40 Mr. Doe 41 42

17 Ingeniørhøjskolen i Århus Slide 17 af 31 Program Output

18 Ingeniørhøjskolen i Århus Slide 18 af 31 XML Schemas DTD works OK – but –Is in Ex. Backus-Naur Form – why not use XML to describe? –Cannot declare a type to of an element – hundrede kr Could give problems XML Schemas –Use XML to describe the structure of XML documents … –Possible to give type information to XML definitions Not supported by all parsers yet Will live besides DTDs for a while

19 Ingeniørhøjskolen i Århus Slide 19 af 31 Book.xsd 1 2 3 4 5 6 <xsd:schema xmlns:xsd = "http://www.w3.org/2000/10/XMLSchema" 7 xmlns:deitel = "http://www.deitel.com/booklist" 8 targetNamespace = "http://www.deitel.com/booklist"> 9 10 11 12 13 <xsd:element name = "book" type = "deitel:BookType" 14 minOccurs = "1" maxOccurs = "unbounded"/> 15 16 17 18 19 20 21 Namespace URI.Namespace prefix. Element element defines an element to be included in the XML document structure. Attributes name and type specify the element ’s name and data type, respectively. Element complexType defines an element type that has a child element named book. Attribute minOccurs specifies that books must contain a minimum of one book element. Attribute maxOccurs, with value unbounded specifies that books may have any number of book child elements.

20 Ingeniørhøjskolen i Århus Slide 20 af 31 Book.xsd 1 2 3 4 5 6 <xsd:schema xmlns:xsd = "http://www.w3.org/2000/10/XMLSchema" 7 xmlns:deitel = "http://www.deitel.com/booklist" 8 targetNamespace = "http://www.deitel.com/booklist"> 9 10 11 12 13 <xsd:element name = "book" type = "deitel:BookType" 14 minOccurs = "1" maxOccurs = "unbounded"/> 15 16 17 18 19 20 21 A BookType has an Element named Title of Type “xsd:string” – which is defined at “http://www.w3.org/2000/10/XMLSchema”

21 Ingeniørhøjskolen i Århus Slide 21 af 31

22 Ingeniørhøjskolen i Århus Slide 22 af 31 How to use XML? Need a parser (or a parser API) to access XML (as with CSV) Two commonly used methods: –DOM (Document Object Model) W3C Recommendation Makes a tree structure representation of an XML document in memory –SAX (Simple API for XML) Supported by diff. vendors Parses document line by line and sends events to subscribers Needs to parse every time access to XML document is needed DOM is better for –Slow to load XML document (need all) –Quick access to random read or update of XML (like WWW browser - BOM) –Requires a lot of memory (need to hold entire XML in mem) SAX is better for –Applications subscribing to certain parts of XML (event subscription) –Slow for random access to XML document (must parse every time)

23 Ingeniørhøjskolen i Århus Slide 23 af 31 Hvad er DOM DOM: Document Object Model –http://www.w3.org/TR/2003/REC-DOM-Level-2-HTML- 20030109/http://www.w3.org/TR/2003/REC-DOM-Level-2-HTML- 20030109/ Hos W3C: –Standard for adgang til strukturerede dokumenter –Core DOM benyttes til XML –HTML DOM benyttes til HTML –Repræsentation af et dokument som et træ af objekter –Giver et ensartet interface på tværs af programmeringssprog –Programmerings API med interface til bl.a. JavaScript

24 Ingeniørhøjskolen i Århus Slide 24 af 31 DOM træstruktur Træstruktur i et XML dokument … eller et HTML document …table tbdoy … …… tr td tekst tekst ….

25 Ingeniørhøjskolen i Århus Slide 25 af 31 Example – using DOM on Article.xml We have looked at Article.xml We Will: –Look at the Article.xml document again –Look at the Tree Structure formed by loading it into a DOM –Use JavaScript to work on it

26 Ingeniørhøjskolen i Århus Slide 26 af 31 1 2 3 4 5 6 7 8 Simple XML 9 10 September 19, 2001 11 12 13 Tem 14 Nieto 15 16 17 XML is pretty easy. 18 19 Once you have mastered XHTML, XML is easily 20 learned. You must remember that XML is not for 21 displaying information but for managing information. 22 23 24 XML document – Article.XML

27 Ingeniørhøjskolen i Århus Slide 27 af 31 DOM Methods firstName lastName contents summary author date title article Tree structure for article.xml.

28 Ingeniørhøjskolen i Århus Slide 28 af 31 DOMExample.ht ml 1 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 4 5 6 7 8 9 10 A DOM Example 11 12 13 14 15 16 <!-- 17 var xmlDocument = new ActiveXObject( "Microsoft.XMLDOM" ); 18 19 xmlDocument.load( "article.xml" ); 20 21 // get the root element 22 var element = xmlDocument.documentElement; 23 24 document.writeln( 25 " Here is the root node of the document: " + 26 " " + element.nodeName + " " + 27 " The following are its child elements:" + 28 " " ); 29 30 // traverse all child nodes of root element 31 for ( var i = 0; i < element.childNodes.length; i++ ) { 32 var curNode = element.childNodes.item( i ); 33 Instantiate a Microsoft XML Document Object Model object and assign it to reference xmlDocument. method load loads article.xml (Fig. 20.1) into memory.Property documentElement corresponds to the root element in the document (e.g., article ). Property nodeName corresponds to the name of an element, attribute, etc. Iterates through the root node’s children using property childNodes.Method item to obtain the child node at index i.

29 Ingeniørhøjskolen i Århus Slide 29 af 31 DOMExample.html 34 // print node name of each child element 35 document.writeln( " " + curNode.nodeName 36 + " " ); 37 } 38 39 document.writeln( " " ); 40 41 // get the first child node of root element 42 var currentNode = element.firstChild; 43 44 document.writeln( " The first child of root node is: " + 45 " " + currentNode.nodeName + " " + 46 " whose next sibling is:" ); 47 48 // get the next sibling of first child 49 var nextSib = currentNode.nextSibling; 50 51 document.writeln( " " + nextSib.nodeName + 52 ". Value of " + 53 nextSib.nodeName + " element is: " ); 54 55 var value = nextSib.firstChild; 56 57 // print the text value of the sibling 58 document.writeln( " " + value.nodeValue + " " + 59 " Parent node of " + nextSib.nodeName + 60 " is: " + 61 nextSib.parentNode.nodeName + ". " ); 62 --> 63 64 65 66 Retrieve the root node’s first child node (i.e., title ) using property firstChild. Property parentNode returns a node’s parent node.

30 Ingeniørhøjskolen i Århus Slide 30 af 31 Program Output

31 Ingeniørhøjskolen i Århus Slide 31 af 31 The SOAP Connection SOAP, WSDL, UDDI uses: – XML –Namespaces –and Schemas Original idea behind Webservices –Connection through the Internet –Good sense to use XML – W3C child –Everyone loves W3C practical solutions “that work”


Download ppt "Presentation 6: Introduction to XML and related technologies."

Similar presentations


Ads by Google