1、XMLDOM大全1Dom Objects introduceMicrosoft DOM Objects Table of ContentsOverviewDOM objects1. DOMDocument2. XMLDOMElement3. XMLDOMNode4. XMLDOMNodeList5. XMLDOMNamedNodeMap6. XMLDOMCDATASection7. XMLDOMAttribute8. XMLDOMDocumentType9. XMLDOMEntity10. XMLDOMProcessingInstruction11. XMLDOMParseErrorXMLHT
2、TPRequestDOM object properties(please see another Document)1. async2. Attributes3. childNodes4. data5. docType6. documentElement7. length8. namespaceURI9. nodeName10. nodeType11. The 12 Node types12. nodeTypedValue13. nodeTypeString14. nodeValue15. ondataavailable16. ownerDocument17. parentNode18. p
3、arsed19. parseError20. prefix21. preserveWhiteSpace22. resolveExternals23. tagName24. text25. url26. validateOnParse27. valueDOM object methods(please see another Document)1. abort()2. appendChild()3. cloneNode()4. createAttribute(), createCDATASection(), createComment(),createElement(), createEntit
4、yReference(),createProcessingInstruction(), createTextNode()5. createNode()6. getAttribute()7. getAttributeNode()8. getElementsByTagName()9. getNamedItem()10. hasChildNodes()11. insertBefore()12. load()13. loadXML()14. nextNode()15. nodeFromID()16. removeAttribute()17. removeAttributeNode() 18. remo
5、veChild()19. removeNamedItem()20. replaceChild()21. reset()22. save()23. selectNodes()24. selectSingleNode()25. send()26. setAttribute()27. setAttributeNode()28. setNamedItem()29. transformNode()30. transformNodeToObject()Microsoft DOM Document Object TutorialXML Programming with VB and ASP gives a
6、VB/ASP developer a from-the-ground-up start to XML and XML programming in VB and ASP. Fun to read and easy going, this is a no-frills book which gets right to the point. More advanced topics include BizTalk, schemas, webclasses and XSL. What this chapter of the book covers: Descriptions of the Micro
7、soft XML objects Sample code for most of the methods and properties Easy-reference table guide to the objectsOverviewMicrosoft has shipped some very useful objects with IE5 that can be used from any programming language that can make use of COM objects. In this part of the book, we will focus on the
8、 XML DOM objects using VB or VBScript.To better understand code or descriptions of the DOMDocument, try to think of an XML document as a tree. Another useful analogy is to think of parents and their children.To better understand code or descriptions about the DOM, try to think of an XML document as
9、a tree. Another useful analogy is to think of parents and their children. We start with the actual root of the document, then it meanders down to each processing instruction, the document root element, attributes, etc. These objects in the DOM are known by there own type (e.g., Element, Attribute, e
10、tc.), but each type is generally called a Node. The DOM gives us collections objects as well, known as NodeLists when a parent has many children that need to be grouped. Figure describes how the tree structure is implemented:This chapter, while comprehensive, should not be used as a complete referen
11、ce.The XML DOM Object implements the XML DOM interfaces. These interfaces make reusability throughout the DOM practical. Because these two objects share the same interfaces, the code becomes easier to understand and work with, which we will see in a short while.DOM objectsFirst we need to explain th
12、e various, common DOM objects that you will use with the MSXML DOM Object.We will focus on the examples of these XML DOM objects shown in table .0.0.1 XML DOM ObjectsObject nameDescriptionDOMDocumentThis object represents the root of the XML file.XMLDOMElementThis object represents each element in t
13、he DOM- Document, namely the root, root element, and each other element.XMLDOMNodeThis object represents a single Node in the document tree and includes support for data types, namespaces, DTDs, and XML Schemas.XMLDOMNodeListUse this object to access (by name) and iterate through the XMLDOMNode coll
14、ection.XMLDOMNamedNodeMapUse this object to access and iterate through the attributes in an element.XMLDOMCDATASectionThis object represents a section in the value of an ele ment that is closed in the CDATA section brackets, which are characters that cannot be parsed by the XML.XMLDOMAttributeThis o
15、bject represents a single attribute Node for a given element.XMLDOMDocumentTypeThis object represents the Document Type (DTD) in an XML file.XMLDOMEntityThis object represents an entity in the DTD section of the XML file.XMLDOMProcessingInstructionThis object represents a processing instruction foun
16、d in the XML file.XMLDOMParseErrorThis object returns detailed information about the last error, including the line number, character position, and a text description.XMLHTTPRequestThis object enables you to establish a connection to a web server from your code and send put, get, and other standard
17、HTML requests.In our examples, we will refer to these objects using the names listed in table .0.0.1 XML object naming conventions (continued)XML object interfaceOur naming conventionDOMDocumentobjDOMDocumentXMLDOMNodeobjXMLDOMNodeXMLDOMNodeListobjXMLDOMNodeListXMLDOMNodeListMapobjXMLDOMNodeListMapX
18、MLDOMParseErrorobjXMLDOMParseErrorXMLDOMElementobjXMLDOMElementXMLDOMAttributeobjXMLDOMAttributeThis manuscript is an abridged version of a chapter from the Manning Publications book XML Programming with VB and ASP. This chapter looks at the Microsoft DOM objects. NOTE: Most images have been removed
19、 to increase speed and many of the code comments have also been removed for presentation. Please purchase the book to enjoy the full experience of all the chapters with images and code comments!DOMDocumentThe DOMDocument object represents the root of the XML file. As the file is loaded into the DOMD
20、ocument, the XML file and its external references, such as DTDs, get validated by the DOMDocument. The DOMDocument is the first port of call to the XML file. This is the only object that can be created. All the other objects, like the elements, can only be created or accessed from the DOMDocument ob
21、ject.ExampleThis example loads an XML file into a DOMDocument object:Dim objDOMDocument As DOMDocumentSet objDOMDocument = New DOMDocumentobjDOMDocument.async = FalseobjDOMDocument.Load http:/localhost/xmlcode/people2.dtdXMLDOMElementThis object represents each element in the XML tree. The XMLDOMEle
22、ment includes support for manipulating the element and the attributes associated with the element.The attributes associated with an element are added and manipulated via the XMLDOMElement object.ExampleThe following example returns the root element of the XML file from a DOMDocument:Dim objDOMDocume
23、nt As DOMDocumentDim objXMLDOMElement As IXMLDOMElementSet objDOMDocument = New DOMDocumentobjDOMDocument.async = FalseobjDOMDocument.Load http:/localhost/xmlcode/people2.dtdSet objXMLDOMElement = objDOMDocument.documentElementXMLDOMNodeThis is one of the base objects of the DOM. Most of the other D
24、OM objects inherit this object; therefore, you will see a lot of properties and methods repeated in each of these objects. These objects are:DOMDocumentXMLDOMAttributeXMLDOMCDATASectionXMLDOMCommentXMLDOMDocumentFragmentXMLDOMDocumentTypeXMLDOMElementXMLDOMEntityXMLDOMEntityReferenceXMLDOMNotationXM
25、LDOMProcessingInstructionXMLDOMTextXTLRuntimeAlthough all these objects inherit methods and properties from the XMLDOMNode object, they will all also have properties and methods that are unique to their function. For example, the XMLDOMElement object has extra methods for obtaining attribute informa
26、tion. The XMLDOMNode interface provides just the basic information, like the name of the Node, its text, etc.To know which type of Node is currently being accessed, the nodeType property returns which type of Node you are referencing when using the XMLDOMNode object, which is explained in detail lat
27、er in this chapter under the nodeType property.Also, see the docType property for more information on Dual interfaces. This explains how to cast from one type of object to the other.ExampleThe following example is the same as the example from the XMLDOMElement, except it now uses the XMLDOMNode obje
28、ct to return the root element of the XML file from a DOMDocument.Dim objDOMDocument As DOMDocumentDim objXMLDOMNode As IXMLDOMNodeSet objDOMDocument = New DOMDocumentobjDOMDocument.async = FalseobjDOMDocument.Load http:/localhost/xmlcode/people2.dtdSet objXMLDOMNode = objDOMDocument.documentElementX
29、MLDOMNodeListThe XMLDOMNodeList object is a collection of Nodes. Its methods allow us to iterate through all the children Nodes of a Node. You can use the For Each . Next loop for this iteration. However, you can also choose to iterate through these Nodes using its method of nextNode().This object i
30、s returned in the property, such as childNodes, or methods such as getElementsByTagName() and selectNodes().ExampleThe following example uses the childNodes property of the DOMDocument to return the children Nodes of the root element of the DOMDocument. (For more details see the childNodes property,
31、 which also shows an example of how to iterate through the XMLDOMNodeList collection.)Dim objDOMDocument As DOMDocumentDim objXMLDOMNodeList As IXMLDOMNodeListSet objDOMDocument = New DOMDocumentobjDOMDocument.async = FalseobjDOMDocument.Load http:/localhost/xmlcode/people2.dtdSet objXMLDOMNodeList = objDOMDocument.documentElement.childNodesXMLDOMNamedNodeMapThis is the other collection object in the DOM. It is used to iterate through the attributes for a specific element. It also allows you to manipulate the attribute collection for an element. To name a f
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1