Tài liệu Module 5: Creating and Manipulating Trees Using DOM ppt

32 529 0
Tài liệu Module 5: Creating and Manipulating Trees Using DOM ppt

Đ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 Lesson: How Does DOM Work? 2 Lesson: Using DOM and the .NET Framework XML Classes 8 Lab 5: Changing a DOM Tree 16 Review 26 Module 5: Creating and Manipulating Trees Using DOM Information in this document, including URL and other Internet Web site references, is subject to change without notice. Unless otherwise noted, the example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events depicted herein are fictitious, and no association with any real company, organization, product, domain name, e-mail address, logo, person, places or events is intended or should be inferred. Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of Microsoft Corporation. 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.  2001 Microsoft Corporation. All rights reserved. Microsoft, MS-DOS, Windows, Windows NT, ActiveX, BackOffice, bCentral, BizTalk, FrontPage, MSDN, MSN, Netshow, PowerPoint, SharePoint, Visio, Visual Basic, Visual C++, Visual C#, Visual InterDev, Visual Studio, Windows Media, and Xbox are either registered trademarks or trademarks of Microsoft Corporation in the U.S.A. and/or other countries. The names of actual companies and products mentioned herein may be the trademarks of their respective owners. Module 5: Creating and Manipulating Trees Using DOM iii Instructor Notes This module covers the basic details of how to use the Document Object Model (DOM) within the Microsoft ® .NET environment. The aim is to give participants an idea of what DOM is about without overwhelming them with its complexity and the many ways that you can use it. Succinctly explain the concepts and the role of DOM in the work of the World Wide Web Consortium (W3C). Be sure to make it clear why DOM is needed. After completing this module, participants will be able to: ! Describe the basic nature of the Document Object Model (DOM). ! Load an XML document into memory. ! Navigate a tree to find data. ! Retrieve and set node properties. ! Save a DOM tree to an XML file. Materials and Preparation This section provides the materials and preparation tasks that you need to teach this module. To teach this module, you need the following materials: ! Microsoft PowerPoint ® file 2500A_05.ppt ! XML Tree Viewer To prepare for this module: ! Read all of the materials for this module and complete the labs. ! Read about DOM in the .NET Framework software development kit (SDK). ! Read the W3C materials about DOM so that you can comfortably show them to the class and comment on them briefly. ! Develop as thorough an understanding as you can about the way DOM interacts with other XML technologies, because some participants may have questions about this. Although Course 1905B, Building XML-Based Web Applications, contains a chapter on DOM, you can only refer to it for comparison purposes, because it is based on Microsoft Visual Basic ® version 6.0 techniques and code. Presentation: 45 minutes Lab: 30 minutes Required Materials Preparation Tasks Note iv Module 5: Creating and Manipulating Trees Using DOM Module Strategy This module describes how to use Microsoft Visual Studio ® .NET and the .NET XML classes to implement basic DOM functionality. Use the following strategy to present this module: ! How Does DOM Work? This lesson introduces how DOM applications work. Explain how the .NET Framework XML classes enable developers to use and extend the W3C XML DOM by using base and extended classes. Direct participants to the shortcut that Visual Studio .NET installs on the Desktop that opens the Microsoft .NET Framework SDK. Emphasize the importance of using the SDK when developing with the .NET Framework XML classes. ! Using DOM and the .NET Framework XML Classes This lesson builds a basic DOM solution by using the .NET Framework XML classes. This lesson first describes how to load an XML document or string. Then, it describes how to search and manipulate the data. Finally, it describes how to save the result as a new XML file. Module 5: Creating and Manipulating Trees Using DOM 1 Overview ! How Does DOM Work? ! Using DOM and the .NET Framework XML Classes ***************************** ILLEGAL FOR NON - TRAINER USE ****************************** The Document Object Model (DOM) is an important technology to know when working with XML documents. This module introduces DOM and how it is enabled in the Microsoft ® .NET Framework environment. For more information on DOM, see the DOM page on the W3C site at http://www.w3.org/dom. After completing this module, you will be able to: ! Describe the basic nature of the Document Object Model (DOM). ! Load an XML document into memory. ! Navigate a tree to find data. ! Retrieve and set node properties. ! Save a DOM tree to an XML file. Introduction Note Objectives 2 Module 5: Creating and Manipulating Trees Using DOM Lesson: How Does DOM Work? ! How Does a DOM Application Work? ! What Are the .NET Framework XML Classes? ***************************** ILLEGAL FOR NON - TRAINER USE ****************************** After completing this lesson, you will be able to: ! Describe how a DOM application works. ! Describe how the .NET Framework XML Classes enable you to create a DOM application. Lesson objectives Module 5: Creating and Manipulating Trees Using DOM 3 How Does a DOM Application Work? Application Application DOM "Load XML into a DOM object" "Load XML into a DOM object" XML Processor XML Result "Search the DOM memory structure" "Search the DOM memory structure" Doc element Element Element Attribute Attribute Text Document Element XML Source MyXmlDoc.Load("Source.xml") MyXmlDoc.Save("Result.xml") "Save the result" "Save the result" Dim List As XmlNodeList MyXmlDoc.LoadXml(MyXmlData) List = myXmlDoc.GetElementsByTagName("Test") DOM memory structure DOM memory structure ***************************** ILLEGAL FOR NON - TRAINER USE ****************************** To use DOM, you need an XML processor that implements DOM. The Microsoft XML Parser (MSXML) version 2.0 implements DOM. The following procedure describes the steps a typical DOM application uses to process XML: 1. The application instructs the XML processor to load an XML document into memory by using its DOM filter. 2. The XML processor DOM filter loads the XML document into memory as a tree of nodes. 3. The application performs some operation on the DOM tree in memory. The application can locate a node according to some criterion that you specify, such as by node type or node content. When the application locates a node or set of nodes, it can perform some processing, such as change the node content or add nodes. 4. The application can save the result as a new XML file or transmit the XML result to some other software module for further processing. Introduction How a typical DOM application processes XML 4 Module 5: Creating and Manipulating Trees Using DOM An XML document is only one of many possible sources of XML. An XML source can be a section of XML data that is contained within a: ! String variable. ! Text document. ! Hypertext Markup Language (HTML) file. ! Microsoft SQL Server ™ 2000 database table. DOM provides a set of techniques for modifying the document tree. You can convert an original document that has been loaded into memory into some other tree structure that suits a different purpose. For example, one part of your order tracking system uses package tracking data that is generated by one of four possible delivery vendors. The structure of the package tracking data depends on which vendor was used to send the package. You can write an application that processes incoming package tracking data depending on the vendor who sends it. Then, you can apply a different DOM conversion routine so that the package tracking data conforms to your system requirements regardless of its origins. What is an XML source? DOM Use Case Module 5: Creating and Manipulating Trees Using DOM 5 What Are the .NET Framework XML Classes? ! Classes that reflect and extend DOM ! Base classes " XmlNode -- represents a single node " XmlNodeList -- represents a node list " XmlNamedNodeMap -- represents named nodes ! Extended classes " Inherit from XmlNode " Used to create objects corresponding to DOM node objects XmlElement -- represents an element XmlAttribute -- represents an attribute XmlComment -- represents a comment Many others ***************************** ILLEGAL FOR NON - TRAINER USE ****************************** The World Wide Web Consortium (W3C) developed DOM in levels. DOM Level 1 and Level 2 have reached Recommendation status. A third level is in the Working Draft stage of development. The Microsoft .NET Framework software development kit (SDK) fully supports and extends the classes described in the W3C DOM Level 1 and Level 2 Recommendations. These extensions to DOM make it easier to work with XML data. For example, several of the new classes enable you to access relational data. They give you methods for synchronizing with ADO.NET data and simultaneously exposing data as XML. If you want to use Microsoft Visual Studio ® .NET to write a program that uses DOM, you will most likely use the following XML DOM base classes. Use the members of this class: To represent: XmlNode A single node in the document tree. It is the base class for accessing data in the XML object model. The XmlNodeType enumeration defines valid node types. XmlNodeList An ordered collection of nodes. It supports iteration and indexed access methods on the live collection of XmlNode objects. This object immediately mirrors changes to the document. XmlNamedNodeMap A collection of nodes that can be accessed by name or index. It provides iteration and access by name to the collection of attributes. This class includes support for namespaces. Introduction XML DOM base classes 6 Module 5: Creating and Manipulating Trees Using DOM The extended classes enable you to create objects that correspond to the DOM node types found in XML documents. Most of these objects inherit properties and methods of the base XmlNode object. Class Description XmlDocument Represents the top node of the XML document. The XmlDocument class implements the W3C DOM Core Level 1 and the DOM Core Level 2. XmlAttribute Represents an attribute of the XmlElement object. Valid and default values for the attribute are defined in a document type definition (DTD) or schema. XmlAttributeCollection Represents attributes of the XmlElement object. Valid and default values for the attribute are defined in a DTD or schema. XmlCDataSection Includes blocks of text containing characters that would otherwise be recognized as markup. These characters are included in their literal forms, without expanding entities or interpreting element tags. XmlCharacterData Provides methods for text manipulation that are used by other classes, such as XmlText and XmlCDataSection. XmlComment Represents the content of an XML comment. XmlDeclaration Represents the XML declaration nodes, <?xml version='1.0' … ?>. XmlDocumentFragment Represents a fragment or portion of a document’s tree. This is useful for tree insert operations. XmlDocumentType Contains information associated with the document type declaration. XmlElement Represents an element object. XmlEntity Represents a parsed or unparsed entity in the XML document. XmlEntityReference Represents an entity reference node. XmlImplementation Provides methods for performing operations that are independent of any particular instance of DOM. XmlLinkedNode Gets the node immediately preceding or following this node. Nodes as the instance of this class or classes derived from it can be linked inside the DOM tree and their proceeding or following nodes can be retrieved. XmlNotation Contains a <!NOTATION declared in the DTD or schema. XML DOM extended classes [...]... petNodes ModifyAttributes(petNode) Next myxmlDoc.Save(" \pets4.xml") End Sub 16 Module 5: Creating and Manipulating Trees Using DOM Lab 5: Changing a DOM Tree ! Exercise 1: Using DOM with Inline Data ! Exercise 2: Using DOM with an External File ! Exercise 3: Manipulating DOM and Creating an Output File ! If Time Permits: Creating a Solution in Visual C# *****************************ILLEGAL FOR NON-TRAINER... code again, and then observe what happens The code compiles without pausing The console window appears briefly and then disappears The output window at the bottom of the screen shows a successful completion Module 5: Creating and Manipulating Trees Using DOM 19 ! Run your DOM application from the command line 1 Click Start, and then click Run 2 Type cmd, and then click OK to open a command prompt window... element or attribute XmlText Represents the text content of an element or attribute 8 Module 5: Creating and Manipulating Trees Using DOM Lesson: Using DOM and the NET Framework XML Classes ! How to Load an XML Source ! How to Navigate the DOM Node Tree ! How to Retrieve and Manipulate Properties and Data ! How to Save a DOM Tree as an XML File *****************************ILLEGAL FOR NON-TRAINER USE******************************... pets2.xml file 13 Run the program as you did before, and then observe the results Be sure that it shows your new data 14 Run the program from a command prompt as you did before When you are finished close the command prompt window 15 On the File menu, click Close Solution 22 Module 5: Creating and Manipulating Trees Using DOM Exercise 3 Manipulating DOM and Creating an Output File In this exercise, you will... myxmlDoc.LoadXml(myXMLData) 10 Module 5: Creating and Manipulating Trees Using DOM How to Navigate the DOM Node Tree ! To navigate the tree to retrieve metadata and data Dim Dim Dim Dim Dim Dim Dim Dim … … demoPetNodes As XmlNodeList demoPetNodes As XmlNodeList demoPetNode As XmlNode demoPetNode As XmlNode domDataNodes As XmlNodeList domDataNodes As XmlNodeList domDataNode As XmlNode domDataNode As XmlNode... myxmlDoc.GetElementsByTagName("pet") For Each demoPetNode In demoPetNodes domDataNodes = demoPetNode.ChildNodes …[other code goes here] Console.Out.Write(domDataNode.Name & ": " & domDataNode.InnerText) When the complete code runs, it produces the following output to a console window Module 5: Creating and Manipulating Trees Using DOM 11 How to Retrieve and Manipulate Properties and Data Dim petAttributes As XmlAttributeCollection... appear as if they are the same ! Continue this exercise 1 Experiment with using different output file names 2 Create an additional output attribute of your own Remember to run the application again each time that you make a change 26 Module 5: Creating and Manipulating Trees Using DOM Review ! How Does DOM Work? ! Using DOM and the NET Framework XML Classes *****************************ILLEGAL FOR... languages in Visual Studio NET use the same classes This makes it easier to reuse code and to share it between applications Note To see many more examples of these kinds of DOM methods, search the NET Framework SDK for topics like xmlattribute and xmlelement Module 5: Creating and Manipulating Trees Using DOM 15 How to Save a DOM Tree as an XML File ! After a file is modified, it must be saved … … ‘code to... application that modifies a DOM tree ! Write an application that saves a new DOM tree to disk ! Prerequisites Modify a Visual Basic NET project Create a Microsoft Visual C#™ project Before working on this lab, you must have familiarity with XML documents Estimated time to complete this lab: 30 minutes Module 5: Creating and Manipulating Trees Using DOM 17 Exercise 1 Using DOM with Inline Data In this... 7" & _ " " & _ "" 18 Module 5: Creating and Manipulating Trees Using DOM ! Dimension variables for DOM objects 1 Locate the comment 'Insert Dim code here, and then add the following variable declarations below the comment Dim Dim Dim Dim Dim myxmlDoc As New XmlDocument() demoPetNodes As XmlNodeList demoPetNode As XmlNode domDataNodes As XmlNodeList domDataNode As XmlNode 2 Can you describe . Example 16 Module 5: Creating and Manipulating Trees Using DOM Lab 5: Changing a DOM Tree ! Exercise 1: Using DOM with Inline Data ! Exercise 2: Using DOM with. Does DOM Work? 2 Lesson: Using DOM and the .NET Framework XML Classes 8 Lab 5: Changing a DOM Tree 16 Review 26 Module 5: Creating and Manipulating Trees Using

Ngày đăng: 10/12/2013, 16:15

Từ khóa liên quan

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

  • Đang cập nhật ...

Tài liệu liên quan