7,763
社区成员
发帖
与我相关
我的任务
分享
Private Sub Command1_Click()
Dim xmlDoc As New Msxml2.DOMDocument30
'If using SAX lexical handler, the following line is required.
xmlDoc.validateOnParse = False
Dim nodeList As IXMLDOMNodeList
Dim wrt As New MXXMLWriter30
Dim cnth As IVBSAXContentHandler
'If using SAX lexical handler, the following line is required.
Dim lexh As IVBSAXLexicalHandler
Dim atrs As New SAXAttributes30
Dim objNodeList
Set cnth = wrt
'If using SAX lexical handler, the following line is required.
Set lexh = wrt
wrt.output = xmlDoc
'Configures the writer to indent elements.
wrt.indent = True
'Starts the document.
cnth.startDocument
'Adds the XML declaration.
cnth.processingInstruction "xml", "version='1.0'"
'Inserts DOCTYPE delcaration for DTD in DOM output.
lexh.startDTD "catalog", "", "books.dtd"
lexh.endDTD
'You can remove or comment out previous two lines if
'you are not linking to a DTD.
'Adds the <catalog> element to the page.
cnth.startElement "", "", "catalog", atrs
'Adds the id attribute to the collection witht he "bk0101" value.
atrs.addAttribute "", "", "id", "CDATA", "bk101"
'Creates the <book id="bk101"> tag.
cnth.startElement "", "", "book", atrs
'Clears the attribute collection.
atrs.Clear
'Creates the <author>Gambardella, Matthew</author> string.
cnth.startElement "", "", "author", atrs
cnth.characters "Gambardella, Matthew"
cnth.endElement "", "", "author"
'Creates the <title>XML Developer's Guide</title> string.
cnth.startElement "", "", "title", atrs
cnth.characters "XML Developer's Guide"
cnth.endElement "", "", "title"
'Creates the <description>An in-depth look at...</description> string.
cnth.startElement "", "", "description", atrs
cnth.characters "An in-depth look at creating applications with XML"
cnth.endElement "", "", "description"
'Adds closing tags for <book> and <catalog> elements.
cnth.endElement "", "", "book"
cnth.endElement "", "", "catalog"
'Ends the document.
cnth.endDocument
'Displays the author's name in a message box.
Set objNodeList = xmlDoc.getElementsByTagName("author")
MsgBox objNodeList.Item(0).Text
End Sub
If value_node Is Nothing Then
GetNodeValue = AA
Else
if value_node.nodetype=1 then
GetNodeValue =Node.attributes(0).xml
elseif value_node.nodetype=3 then
GetNodeValue = value_node.Text
endif
End If
Private Function GetNodeValue(ByVal start_at_node As IXMLDOMNode, ByVal node_name As String, _
Optional ByVal AA As String = "") As String
Dim value_node As IXMLDOMNode
Dim child_node As IXMLDOMNode
Set value_node = start_at_node.selectSingleNode(".//" & node_name)
If value_node Is Nothing Then
GetNodeValue = AA
Else
For Each child_node In value_node.childNodes
If child_node.nodeType = NODE_TEXT Then
GetNodeValue = child_node.Text
Exit Function
End If
Next
GetNodeValue = value_node.Text
End If
End Function
Dim aElement As IXMLDOMElement
Set aElement = xml_document.createElement("xxxxyyyy") '创建一个名称为xxxxyyyy的节点
aElement.nodeTypedValue = "ddddddddd" '节点值
xml_document.documentElement.childNodes.Item(0).appendChild aElement '第一个节点下增加了一个子节点
Debug.Print xmlRootElement.childNodes.Item(0).childNodes.Length
Debug.Print xmlRootElement.childNodes.Item(0).Text