如何在 vb6 递归中解析 xml-file
how to parse xml-file in vb6 recursive
我是 vb6 的新手,必须解析具有不同标签深度的 xml 文档,如下例所示:
<start>
<b>text1</b>
<c>
<c1>
<d>text</d>
</c1>
</c>
</start>
我正在使用 MSXML 并尝试递归解决此问题。我的代码是:
Sub1()
Set objXML = CreateObject("Msxml.DOMDocument")
objXML.async = True
objXML.Load "text.xml"
Dim nodeList As IXMLDOMNodeList
Dim node As IXMLDOMNode
Set nodeList = objXML.selectNodes("*")
For Each node In nodeList
print node.nodeName ' this works'
printNode (node) 'here is the problem explained below'
Next node
End Sub
Sub printNode(node As IXMLDOMNode)
Dim xmlNode As IXMLDOMNode
If node.hasChildNodes Then
For Each xmlNode In node.childNodes
printNode (xmlNode)
Next xmlNode
Print node.nodeName
End If
End Sub
问题是无法使用 类型的参数 node 调用 sub printNode ]IXmlDomNode。当我尝试 运行 程序时,我总是得到一个 运行timeerror 438,它说 object doesn't support this function
节点确实存在,我已经测试过了,可以打印节点的名称和值。
任何人都可以解释我的原因并给我提示 how/solution 我该如何解决这个问题?
您的代码中存在语法错误。如果您正在调用 Sub,您要么不将参数括在括号中,要么在 Sub 名称前使用 Call 关键字。
Public Sub Sub1()
Set objXML = CreateObject("Msxml.DOMDocument")
objXML.async = True
objXML.Load "text.xml"
Dim nodeList As IXMLDOMNodeList
Dim node As IXMLDOMNode
Set nodeList = objXML.selectNodes("*")
For Each node In nodeList
print node.nodeName ' this works'
Call printNode(node) 'here is the problem explained below'
Next node
End Sub
Public Sub printNode(node As IXMLDOMNode)
Dim xmlNode As IXMLDOMNode
If node.hasChildNodes Then
For Each xmlNode In node.childNodes
Call printNode(xmlNode)
Next xmlNode
Print node.nodeName
End If
End Sub
我是 vb6 的新手,必须解析具有不同标签深度的 xml 文档,如下例所示:
<start>
<b>text1</b>
<c>
<c1>
<d>text</d>
</c1>
</c>
</start>
我正在使用 MSXML 并尝试递归解决此问题。我的代码是:
Sub1()
Set objXML = CreateObject("Msxml.DOMDocument")
objXML.async = True
objXML.Load "text.xml"
Dim nodeList As IXMLDOMNodeList
Dim node As IXMLDOMNode
Set nodeList = objXML.selectNodes("*")
For Each node In nodeList
print node.nodeName ' this works'
printNode (node) 'here is the problem explained below'
Next node
End Sub
Sub printNode(node As IXMLDOMNode)
Dim xmlNode As IXMLDOMNode
If node.hasChildNodes Then
For Each xmlNode In node.childNodes
printNode (xmlNode)
Next xmlNode
Print node.nodeName
End If
End Sub
问题是无法使用 类型的参数 node 调用 sub printNode ]IXmlDomNode。当我尝试 运行 程序时,我总是得到一个 运行timeerror 438,它说 object doesn't support this function
节点确实存在,我已经测试过了,可以打印节点的名称和值。
任何人都可以解释我的原因并给我提示 how/solution 我该如何解决这个问题?
您的代码中存在语法错误。如果您正在调用 Sub,您要么不将参数括在括号中,要么在 Sub 名称前使用 Call 关键字。
Public Sub Sub1()
Set objXML = CreateObject("Msxml.DOMDocument")
objXML.async = True
objXML.Load "text.xml"
Dim nodeList As IXMLDOMNodeList
Dim node As IXMLDOMNode
Set nodeList = objXML.selectNodes("*")
For Each node In nodeList
print node.nodeName ' this works'
Call printNode(node) 'here is the problem explained below'
Next node
End Sub
Public Sub printNode(node As IXMLDOMNode)
Dim xmlNode As IXMLDOMNode
If node.hasChildNodes Then
For Each xmlNode In node.childNodes
Call printNode(xmlNode)
Next xmlNode
Print node.nodeName
End If
End Sub