Tài liệu Module 9: Using XML to Exchange Data pptx

80 489 0
Tài liệu Module 9: Using XML to Exchange Data pptx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Contents Overview 1 Introduction to XML 2 Validating XML Documents 20 Using the Document Object Model 31 Applying XML in N-Tier Applications 53 Lab 9: Exchanging Data Using XML 60 Best Practices 67 Review 69 Module 9: Using XML to Exchange Data Information in this document is subject to change without notice. The names of companies, products, people, characters, and/or data mentioned herein are fictitious and are in no way intended to represent any real individual, company, product, or event, unless otherwise noted. Complying with all applicable copyright laws is the responsibility of the user. No part of this document may be reproduced or transmitted in any form or by any means, electronic or mechanical, for any purpose, without the express written permission of Microsoft Corporation. If, however, your only means of access is electronic, permission to print one copy is hereby granted. Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this document. Except as expressly provided in any written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual property.  2000 Microsoft Corporation. All rights reserved. Microsoft, BackOffice, MS-DOS, Windows, Windows NT, ActiveX, MSDN, PowerPoint, and Visual Basic are either registered trademarks or trademarks of Microsoft Corporation in the U.S.A. and/or other countries. The names of companies, products, people, characters, and/or data mentioned herein are fictitious and are in no way intended to represent any real individual, company, product, or event, unless otherwise noted. Other product and company names mentioned herein may be the trademarks of their respective owners. Module 9: Using XML to Exchange Data iii Instructor Notes This module introduces the Extensible Markup Language (XML). Students will learn how data is represented by using XML and how to use Document Type Definitions (DTDs) and schemas to validate document structure. Students will also learn how to parse XML by using the Document Object Model (DOM). After completing this module, students will be able to: ! Describe the purpose and benefits of XML. ! Describe the structure of a well-formed XML document. ! Describe the purpose of XML Schemas and DTDs. ! Manipulate XML by using DOM. In the lab, students will examine code to see how the XML DOM can be used to create XML documents. They will then use the DOM to read an XML purchase order that was generated by the Purchase Order Online application used in the labs for this course. Materials and Preparation This section provides you with the required materials and preparation tasks that are needed to teach this module. Required Materials To teach this module, you need the following materials: ! Microsoft PowerPoint ® file 1907A_09.ppt ! Module 9: Using XML to Exchange Data ! Lab 9: Exchanging Data Using XML Preparation Tasks To prepare for this module, you should: ! Read all of the materials for this module. ! Complete the lab. ! Read the instructor notes and the margin notes for this module. Presentation: 75 Minutes Lab: 60 Minutes iv Module 9: Using XML to Exchange Data Demonstration This section provides demonstration procedures that will not fit in the margin notes or are not appropriate for the student notes. Using the Document Object Model The purpose of this demonstration is to show how the DOM can be used to create, load, browse, and search an XML document. This demonstration uses an XML document containing a booklist. It has an associated schema at http://localhost/books/booklistschema.xml . This demonstration is divided into four parts. You can demonstrate all parts or selected individual parts. Note, however, that to demonstrate parts C or D, the demonstration program first requires that an XML document be loaded. This initial step is achieved by clicking the Load XML Document button (described in part B). You must also follow the demonstration preparation instructions that follow prior to performing a demonstration. ! Part A: Create and Save an XML Document ! Part B: Load and Validate an XML Document ! Part C: Walk Through an XML Document ! Part D: Search an XML Document ! Prepare for the demonstration 1. Use Windows Explorer to navigate to the <install folder>\Democode\Mod09\XML folder. 2. Right-click the Books folder and choose Properties 3. On the Web Sharing tab, click the Share this folder option button. 4. Leave the default alias (Books) and ensure that all Access Permissions check boxes are checked. Then click OK. 5. Click Yes to accept the warning message and then click OK on the Properties dialog box. 6. Open the project XMLDemo.vbp located in <install folder>\Democode\Mod09\XML. 7. Display the cmdCreateXMLDoc_Click procedure and place a breakpoint on the line Set xmlDoc = New MSXML.DOMDocument. 8. Display the cmdLoadXMLDocument_Click procedure and place a breakpoint on the line Set xmlDoc = New MSXML.DOMDocument. 9. Display the cmdWalkXMLDocument_Click procedure and place a breakpoint on the line Debug.Assert Not (xmlDoc Is Nothing). 10. Display the cmdSearchXMLDocument_Click procedure and place a breakpoint on the line Debug.Assert Not (xmlDoc Is Nothing). Module 9: Using XML to Exchange Data v ! Part A: Create and Save an XML Document 1. Run the project. 2. Click the Create XML Document button. Execution will halt at the breakpoint in cmdCreateXMLDoc_Click. 3. Explain that the line with the breakpoint instantiates an MSXML.DOMDocument object, which represents the top node of the XML DOM tree. Press F8 to step over the line with the breakpoint. 4. Step over the next three lines of code, making the following observations: • A processing instruction containing an XML declaration is created. • Processing instructions (like all new elements and attributes) must be appended to the appropriate place in the DOM tree. This step is performed by using the appendChild method of the IXMLDOMDocument interface. In this case, the processing instruction is appended directly to the DOMDocument, placing it at the top level of the document. 5. Step over the next three lines of code, making the following observations: • The DOMDocument interface’s createElement method is used to create the booklist element. • An xmlns attribute is created by using setAttribute. This attribute is used to associate a schema with an XML document. • The booklist element is appended to the tree at the top level, making booklist the root element. 6. Explain that the private subroutine AddBook is used to create a book element, together with its associated attributes and child elements (title, author, and price). 7. Press F8 to enter the AddBook subroutine. 8. Step over the code in the AddBook subroutine, making the following observations: • New elements are created by using the createElement method of the IXMLDOMDocument interface. This interface is obtained via the ownerDocument property of IXMLDOMNode. This code returns the root of the document containing the node. • Attributes are associated with elements by using the setAttribute method of the element’s IXMLDOMElement interface. • Each element is appended to the supplied parent node (which in this case is the booklist element) by using the appendChild method. 9. Having returned to the cmdCreateXMLDoc_Click procedure, step over the remaining calls to AddBook. vi Module 9: Using XML to Exchange Data 10. Step into the SaveXMLDocument private subroutine. Step through this routine, making the following observations: • ADO Record and Stream objects are used to create the output XML document. Using these objects allows the document to be output to a Web folder by using a URL. • A Stream object is opened from the Record object to represent the contents of the file. • The WriteText method of the Stream object is called with a string representation of the XML document passed as a parameter (xmlDoc.xml). 11. Press the Continue toolbar button to resume execution of the program. 12. A message box will be displayed confirming that the document has been successfully created. Press OK to dismiss this message box. ! Part B: Load and Validate an XML Document 1. Run the project if it is not already running. Click the Load XML Document button. Execution will halt at the breakpoint within cmdLoadXMLDocument_Click. 2. Step over the line of code that instantiates a new DOMDocument. 3. Explain that by default the Load method of DOMDocument will load a document asynchronously. In this instance, the code sets the async property to False to perform a synchronous load. Point out the pitfalls of starting to use the DOM with a partially loaded tree. Also point out that if an asynchronous load is chosen, the ondataavailable event is fired when the XML document data is available. 4. Press F8 to step over the next line of code. 5. Point out that the validateOnParse property of DOMDocument indicates whether validation should be performed while loading the document. In this case, validation occurs against the schema referenced by the document (by using the xmlns attribute of the booklist root element). 6. Press F8 to step over the next line of code. 7. Step over the call to the DOMDocument’s Load method and point out that a URL is used to locate the XML document. 8. Press the Continue toolbar button to resume program execution. 9. A message box will be displayed confirming that the document has been successfully loaded. Press OK to dismiss this message box. 10. The DOM tree is now fully populated. ! Part C: Walk Through An XML Document 1. Click the Walk XML Document button. If this button is disabled, you must first load an XML document by using the Load XML Document button. 2. Execution will halt at the breakpoint in cmdWalkXMLDocument_Click. 3. Step over the remaining lines of code in the subroutine, making the following observations: 4. The root IXMLDOMNode variable is set to the document’s root element by using the DOMDocument’s documentElement property. Module 9: Using XML to Exchange Data vii 5. The hasChildNodes method of IXMLDOMNode is used to test whether the root element has any child elements. 6. The length property of the child nodes IXMLDOMNodeList interface is used to ascertain how many direct children elements the booklist element possesses. This number represents the number of books in the book list. 7. A For loop is established to process each book element. 8. The bookNode IXMLDOMNode variable is set to each successive child node. 9. The node type is checked by using the nodeTypeString property. In this case, the node type will always be “element” because only book elements are direct children of the booklist element. 10. For each element, the ProcessBookElement private subroutine is called, which outputs the element tag name and text together with the values of the isbn and type attributes. Make sure the Immediate window is visible as output is sent to this window. Notice that the text property associated with the book element is a concatenation of all the text nodes for all child elements of book. ! Part D: Search an XML Document A set of XSL patterns has been provided in the XSL Pattern combo box. You can repeat these steps for each pattern. The patterns are: //author Returns all author elements in the document //book[@isbn=’1-444444-11-0’] Returns the book element with the specified isbn attribute value. //book[@type=’psychology’] Returns all book elements with the specified type attribute value. ! Part E: . 1. Click the Search XML Document button. If this button is disabled, you must first load an XML document by using the Load XML Document button. 2. Execution will halt at the breakpoint in cmdSearchXMLDocument_Click. 3. Step over the remaining lines of code in this subroutine, making the following observations: 4. The XSL pattern is passed as a parameter to the selectNodes method of the IXMLDOMDocument interface. 5. selectNodes returns an IXMLDOMNodeList collection that contains matching nodes. 6. A For Each construct is established to process each node in the collection. 7. The element tag name and text values are output to the Immediate window. Notice that for searches that return book elements, the text property of the book element is a concatenation of all the text nodes for all child elements of book. viii Module 9: Using XML to Exchange Data Module Strategy Use the following strategy to present this module: ! Introduction to XML Provide an overview of XML. XML defines a generic mechanism for adding tagged information to character data. This extra information can help to convey additional context, or metadata, or it can define the structure of the data contained within the tags (for example, by defining the fields of a purchase order). Although it may initially seem fairly simplistic, the simplicity and flexibility of XML are two of the key features that have helped make it the de facto information exchange mechanism for e- commerce. Discuss the syntax of XML and how it encompasses other data. Look at how XML can be applied and the types of data it can help to represent. Emphasize the suitability of XML for document interchange (e-business) and explain that document exchange is how XML is used in the lab scenario. The purchase order system produces XML order documents for vendor trading partners. Mention other members of the XML family and which parts of the XML family are implemented in some common Microsoft products. There is a practice of using Microsoft Internet Explorer 5 to view an XML document. The practice initially uses Internet Explorer 5 to display the XML data in its raw format and then asks students to associate an XSLT style sheet with the XML document. Internet Explorer 5 is used to view the document again. This time, as Internet Explorer 5 processes the style sheet, the data is displayed in an HTML table. ! Validating XML Documents Describe the concept of XML validation and that it is frequently useful to know whether an XML document conforms to a specific XML grammar. The process of checking that an XML document conforms to a specific XML grammar is called validation. Applications can accept or reject documents based on their validity. The two common mechanisms for defining the XML grammars used when validating XML documents are DTDs and XML Schemas. Explain why it is useful to validate XML documents. Show students how DTDs and XML Schemas work, and stress the advantages that schemas have over DTDs. Point out that the prime advantage of the DTD is that it is part of the XML 1.0 specification. Discuss schema syntax. ! Using the Document Object Model Discuss that one of the great advantages of using XML rather than a proprietary data format is that there are ready-made parsers, such as the Microsoft XML engine MSXML, to perform much of the difficult work automatically. However, after the parser has processed the XML data, you need some mechanism of accessing it programmatically. The DOM standard defines such a mechanism. Explain the basics of how to access and manipulate XML data by using the DOM. Discuss searching for name tags and nodes based on particular criteria. Discuss XPath syntax. Finally, describe how to create, trim, and persist XML trees. Module 9: Using XML to Exchange Data ix ! Applying XML in N-Tier Applications Discuss the different mechanisms for creating and manipulating XML. Refer students to the lab scenario, and discuss some of the ways in which XML might be used in distributed applications like those found in the lab for this module. Examine some of the potential sources of XML, and explain how XML can be easily sent to a URL for further processing. Look at how XSL Transformations (XSLT) can be used as a powerful tool for converting between XML grammars. ! Best Practices Summarize the best practices that should be observed when using XML to exchange data. THIS PAGE INTENTIONALLY LEFT BLANK [...].. .Module 9: Using XML to Exchange Data # Overview ! Introduction to XML ! Validating XML Documents ! Using the Document Object Model ! Applying XML in N-Tier Applications ! Lab 9: Exchanging Data Using XML ! Best Practices ! Review The Extensible Markup Language (XML) defines a flexible data representation that is ideal for data exchange in loosely coupled systems Because of this advantage, XML is... useful to validate XML documents You will then learn how DTDs and XML Schemas work and the pros and cons of each mechanism You will also learn how to apply a DTD or XML Schema to an XML document This section includes the following topics: ! Conforming to an XML Grammar ! Validating XML Documents with DTDs ! Validating XML Documents with Schemas Module 9: Using XML to Exchange Data 21 Conforming to an XML. .. this module, you will be able to: ! Describe the purpose and benefits of XML ! Describe the structure of a well-formed XML document ! Describe the purpose of XML Schemas and DTDs ! Manipulate XML by using the Document Object Model ! Describe how XML can be applied in an n-tier Windows DNA solution 1 2 Module 9: Using XML to Exchange Data # Introduction to XML ! What is XML? ! Benefits of XML ! XML Syntax... following topics: ! What Is XML? ! Benefits of XML ! XML Syntax ! XML Family of Standards ! XML Support in Microsoft Products ! Practice: Viewing an XML Document with Internet Explorer 5 Module 9: Using XML to Exchange Data 3 What Is XML? ! Standard for defining data in tagged form $ $ Imposes a structure on the underlying raw data Provides extra information, or metadata, about part of the underlying raw data. .. standards, see XML Family of Standards in this module 5 6 Module 9: Using XML to Exchange Data Benefits of XML ! Data exchange $ ! A cheap alternative to EDI Standardization of documents $ $ ! XML grammars being standardized for vertical markets Microsoft’s BizTalk initiative Metadata $ ! Tools such as Rational Rose can export OO design model in a format known as XML Metadata Interchange (XMI) Structure and... transformed the raw XML data into HTML, resulting in the tabulated display of the book list This list illustrates that Internet Explorer 5 processes any style sheet attached to an XML document before displaying it 12 Close Internet Explorer 20 Module 9: Using XML to Exchange Data # Validating XML Documents ! Conforming to an XML Grammar ! Validating XML Documents with DTDs ! Validating XML Documents with... in this module, you will see how much easier it is to write a program to process an XML document than it would be to process an HTML document Module 9: Using XML to Exchange Data XML itself is only one of a set of related standards The World Wide Web Consortium defines the standard for XML and the other technologies in the XML family For more information about the XML family of standards, see XML Family... the response as an XML document ! Simple Object Access Protocol (SOAP) SOAP defines a mechanism for using XML and HTTP as a firewall-friendly alternative to COM For more information about SOAP, go to http://msdn.microsoft.com /xml/ general/soapspec-v1.asp Module 9: Using XML to Exchange Data ! 17 ActiveX® Data Objects (ADO version 2.0 and above) ADO recordsets can be persisted in an XML format This advantage... convey meaning, as shown in the following example: 9 10 Module 9: Using XML to Exchange Data The element is enough to signify that the book is currently in stock This type of XML element is called an empty element XML defines a shorthand notation for empty elements by collapsing the two tags into one This single tag looks like a start tag, but with a trailing... element 8 Close Internet Explorer Module 9: Using XML to Exchange Data 19 ! Apply an XSL style sheet You will now apply an XSLT style sheet to the XML document to transform the XML into HTML 1 Start a copy of Notepad You will use this copy to edit the XML document a From the Start menu, select Run b In the Open field, type notepad c Click OK 2 In Notepad, point to the File menu and select Open 3 In . viii Module 9: Using XML to Exchange Data Module Strategy Use the following strategy to present this module: ! Introduction to XML Provide an overview of XML. . Module 9: Using XML to Exchange Data 1 # ## # Overview ! Introduction to XML ! Validating XML Documents ! Using the Document Object Model ! Applying XML

Ngày đăng: 21/12/2013, 19:15

Tài liệu cùng người dùng

Tài liệu liên quan