XMLDOM大全1Dom Objects introduce.docx
《XMLDOM大全1Dom Objects introduce.docx》由会员分享,可在线阅读,更多相关《XMLDOM大全1Dom Objects introduce.docx(15页珍藏版)》请在冰豆网上搜索。
XMLDOM大全1DomObjectsintroduce
MicrosoftDOMObjectsTableofContents
Overview
DOMobjects
1.DOMDocument
2.XMLDOMElement
3.XMLDOMNode
4.XMLDOMNodeList
5.XMLDOMNamedNodeMap
6.XMLDOMCDATASection
7.XMLDOMAttribute
8.XMLDOMDocumentType
9.XMLDOMEntity
10.XMLDOMProcessingInstruction
11.XMLDOMParseError
XMLHTTPRequest
DOMobjectproperties(pleaseseeanotherDocument)
1.async
2.Attributes
3.childNodes
4.data
5.docType
6.documentElement
7.length
8.namespaceURI
9.nodeName
10.nodeType
11.The12Nodetypes
12.nodeTypedValue
13.nodeTypeString
14.nodeValue
15.ondataavailable
16.ownerDocument
17.parentNode
18.parsed
19.parseError
20.prefix
21.preserveWhiteSpace
22.resolveExternals
23.tagName
24.text
25.url
26.validateOnParse
27.value
DOMobjectmethods(pleaseseeanotherDocument)
1.abort()
2.appendChild()
3.cloneNode()
4.createAttribute(),createCDATASection(),createComment(),createElement(),createEntityReference(),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.removeChild()
19.removeNamedItem()
20.replaceChild()
21.reset()
22.save()
23.selectNodes()
24.selectSingleNode()
25.send()
26.setAttribute()
27.setAttributeNode()
28.setNamedItem()
29.transformNode()
30.transformNodeToObject()
MicrosoftDOMDocumentObjectTutorial
XMLProgrammingwithVBandASPgivesaVB/ASPdeveloperafrom-the-ground-upstarttoXMLandXMLprogramminginVBandASP.Funtoreadandeasygoing,thisisano-frillsbookwhichgetsrighttothepoint.MoreadvancedtopicsincludeBizTalk,schemas,webclassesandXSL.
Whatthischapterofthebookcovers:
✶DescriptionsoftheMicrosoftXMLobjects
✶Samplecodeformostofthemethodsandproperties
✶Easy-referencetableguidetotheobjects
Overview
MicrosofthasshippedsomeveryusefulobjectswithIE5thatcanbeusedfromanyprogramminglanguagethatcanmakeuseofCOMobjects.Inthispartofthebook,wewillfocusontheXMLDOMobjectsusingVBorVBScript.
TobetterunderstandcodeordescriptionsoftheDOMDocument,trytothinkofanXMLdocumentasatree.Anotherusefulanalogyistothinkofparentsandtheirchildren.
TobetterunderstandcodeordescriptionsabouttheDOM,trytothinkofanXMLdocumentasatree.Anotherusefulanalogyistothinkofparentsandtheirchildren.Westartwiththeactualrootofthedocument,thenitmeandersdowntoeachprocessinginstruction,thedocumentrootelement,attributes,etc.TheseobjectsintheDOMareknownbythereowntype(e.g.,Element,Attribute,etc.),buteachtypeisgenerallycalledaNode.TheDOMgivesuscollectionsobjectsaswell,knownasNodeListswhenaparenthasmanychildrenthatneedtobegrouped.Figure describeshowthetreestructureisimplemented:
Thischapter,whilecomprehensive,shouldnotbeusedasacompletereference.
TheXMLDOMObjectimplementstheXMLDOMinterfaces.TheseinterfacesmakereusabilitythroughouttheDOMpractical.Becausethesetwoobjectssharethesameinterfaces,thecodebecomeseasiertounderstandandworkwith,whichwewillseeinashortwhile.
DOMobjects
Firstweneedtoexplainthevarious,commonDOMobjectsthatyouwillusewiththeMSXMLDOMObject.
WewillfocusontheexamplesoftheseXMLDOMobjectsshownintable .
0.0.1XMLDOMObjects
Objectname
Description
DOMDocument
ThisobjectrepresentstherootoftheXMLfile.
XMLDOMElement
ThisobjectrepresentseachelementintheDOM-Document,namelytheroot,rootelement,andeachotherelement.
XMLDOMNode
ThisobjectrepresentsasingleNodeinthedocumenttreeandincludessupportfordatatypes,namespaces,DTDs,andXMLSchemas.
XMLDOMNodeList
Usethisobjecttoaccess(byname)anditeratethroughtheXMLDOMNodecollection.
XMLDOMNamedNodeMap
Usethisobjecttoaccessanditeratethroughtheattributesinanelement.
XMLDOMCDATASection
ThisobjectrepresentsasectioninthevalueofanelementthatisclosedintheCDATAsectionbrackets,whicharecharactersthatcannotbeparsedbytheXML.
XMLDOMAttribute
ThisobjectrepresentsasingleattributeNodeforagivenelement.
XMLDOMDocumentType
ThisobjectrepresentstheDocumentType(DTD)inanXMLfile.
XMLDOMEntity
ThisobjectrepresentsanentityintheDTDsectionoftheXMLfile.
XMLDOMProcessingInstruction
ThisobjectrepresentsaprocessinginstructionfoundintheXMLfile.
XMLDOMParseError
Thisobjectreturnsdetailedinformationaboutthelasterror,includingthelinenumber,characterposition,andatextdescription.
XMLHTTPRequest
Thisobjectenablesyoutoestablishaconnectiontoawebserverfromyourcodeandsendput,get,andotherstandardHTMLrequests.
Inourexamples,wewillrefertotheseobjectsusingthenameslistedintable .
0.0.1XMLobjectnamingconventions(continued)
XMLobjectinterface
Ournamingconvention
DOMDocument
objDOMDocument
XMLDOMNode
objXMLDOMNode
XMLDOMNodeList
objXMLDOMNodeList
XMLDOMNodeListMap
objXMLDOMNodeListMap
XMLDOMParseError
objXMLDOMParseError
XMLDOMElement
objXMLDOMElement
XMLDOMAttribute
objXMLDOMAttribute
ThismanuscriptisanabridgedversionofachapterfromtheManningPublicationsbookXMLProgrammingwithVBandASP.ThischapterlooksattheMicrosoftDOMobjects.NOTE:
Mostimageshavebeenremovedtoincreasespeedandmanyofthecodecommentshavealsobeenremovedforpresentation.Pleasepurchasethebooktoenjoythefullexperienceofallthechapterswithimagesandcodecomments!
DOMDocument
TheDOMDocumentobjectrepresentstherootoftheXMLfile.AsthefileisloadedintotheDOMDocument,theXMLfileanditsexternalreferences,suchasDTDs,getvalidatedbytheDOMDocument.
TheDOMDocumentisthefirstportofcalltotheXMLfile.Thisistheonlyobjectthatcanbecreated.Alltheotherobjects,liketheelements,canonlybecreatedoraccessedfromtheDOMDocumentobject.
Example
ThisexampleloadsanXMLfileintoaDOMDocumentobject:
DimobjDOMDocumentAsDOMDocument
SetobjDOMDocument=NewDOMDocument
objDOMDocument.async=False
objDOMDocument.Loadhttp:
//localhost/xmlcode/people2.dtd
XMLDOMElement
ThisobjectrepresentseachelementintheXMLtree.TheXMLDOMElementincludessupportformanipulatingtheelementandtheattributesassociatedwiththeelement.
TheattributesassociatedwithanelementareaddedandmanipulatedviatheXMLDOMElementobject.
Example
ThefollowingexamplereturnstherootelementoftheXMLfilefromaDOMDocument:
DimobjDOMDocumentAsDOMDocument
DimobjXMLDOMElementAsIXMLDOMElement
SetobjDOMDocument=NewDOMDocument
objDOMDocument.async=False
objDOMDocument.Load"http:
//localhost/xmlcode/people2.dtd"
SetobjXMLDOMElement=objDOMDocument.documentElement
XMLDOMNode
ThisisoneofthebaseobjectsoftheDOM.MostoftheotherDOMobjectsinheritthisobject;therefore,youwillseealotofpropertiesandmethodsrepeatedineachoftheseobjects.
Theseobjectsare:
✶DOMDocument
✶XMLDOMAttribute
✶XMLDOMCDATASection
✶XMLDOMComment
✶XMLDOMDocumentFragment
✶XMLDOMDocumentType
✶XMLDOMElement
✶XMLDOMEntity
✶XMLDOMEntityReference
✶XMLDOMNotation
✶XMLDOMProcessingInstruction
✶XMLDOMText
✶XTLRuntime
AlthoughalltheseobjectsinheritmethodsandpropertiesfromtheXMLDOMNodeobject,theywillallalsohavepropertiesandmethodsthatareuniquetotheirfunction.Forexample,theXMLDOMElementobjecthasextramethodsforobtainingattributeinformation.TheXMLDOMNodeinterfaceprovidesjustthebasicinformation,likethenameoftheNode,itstext,etc.
ToknowwhichtypeofNodeiscurrentlybeingaccessed,thenodeTypepropertyreturnswhichtypeofNodeyouarereferencingwhenusingtheXMLDOMNodeobject,whichisexplainedindetaillaterinthischapterunderthenodeTypeproperty.
Also,seethedocTypepropertyformoreinformationonDualinterfaces.Thisexplainshowtocastfromonetypeofobjecttotheother.
Example
ThefollowingexampleisthesameastheexamplefromtheXMLDOMElement,exceptitnowusestheXMLDOMNodeobjecttoreturntherootelementoftheXMLfilefromaDOMDocument.
DimobjDOMDocumentAsDOMDocument
DimobjXMLDOMNodeAsIXMLDOMNode
SetobjDOMDocument=NewDOMDocument
objDOMDocument.async=False
objDOMDocument.Load"http:
//localhost/xmlcode/people2.dtd"
SetobjXMLDOMNode=objDOMDocument.documentElement
XMLDOMNodeList
TheXMLDOMNodeListobjectisacollectionofNodes.ItsmethodsallowustoiteratethroughallthechildrenNodesofaNode.YoucanusetheForEach...Nextloopforthisiteration.However,youcanalsochoosetoiteratethroughtheseNodesusingitsmethodofnextNode().
Thisobjectisreturnedintheproperty,suchaschildNodes,ormethodssuchasgetElementsByTagName()andselectNodes().
Example
ThefollowingexampleusesthechildNodespropertyoftheDOMDocumenttoreturnthechildrenNodesoftherootelementoftheDOMDocument.(FormoredetailsseethechildNodesproperty,whichalsoshowsanexampleofhowtoiteratethroughtheXMLDOMNodeListcollection.)
DimobjDOMDocumentAsDOMDocument
DimobjXMLDOMNodeListAsIXMLDOMNodeList
SetobjDOMDocument=NewDOMDocument
objDOMDocument.async=False
objDOMDocument.Load"http:
//localhost/xmlcode/people2.dtd"
SetobjXMLDOMNodeList=objDOMDocument.documentElement.childNodes
XMLDOMNamedNodeMap
ThisistheothercollectionobjectintheDOM.Itisusedtoiteratethroughtheattributesforaspecificelement.Italsoallowsyoutomanipulatetheattributecollectionforanelement.Tonameaf