VB6.0如何读写XML结点

SUGENG328 2009-11-24 12:43:46
<Values>
<FirstName>Mei
<First>test1</First>
<Second>test2</Second>
</FirstName>
</Values>
如上,我要读取<FirstName>中的Text:Mei,不包含其字结点的内容,如何读取?
Dim xml_document As DOMDocument
Dim values_node As IXMLDOMNode

Dim values_node2 As IXMLDOMNode

Set xml_document = New DOMDocument
xml_document.Load m_AppPath & "Values.xml"
If xml_document.documentElement Is Nothing Then
Exit Sub
End If
Dim str As String
Set values_node = xml_document.selectSingleNode("Values")
txtFirstName = GetNodeValue(values_node, "FirstName", "")
其中:
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
Set value_node = start_at_node.selectSingleNode(".//" & node_name)
If value_node Is Nothing Then
GetNodeValue = AA
Else
GetNodeValue = value_node.Text
End If
End Function
通过:GetNodeValue = value_node.Text可以读出<FirstName>下所有结点的Text,但我不要其字结点的Text,应该如何做?
另外,如何比较容易的在父结点下添加子结点?先通过:
Set values_node2 = xml_document.selectSingleNode(".//" & "FirstName")
values_node2.appendChild xml_document.createTextNode(vbCrLf)
xml_document.appendChild values_node2
在下刚学VB,还请各位大侠多多指教!
...全文
970 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
chinaboyzyq 2009-11-25
  • 打赏
  • 举报
回复

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
孤独剑_LPZ 2009-11-25
  • 打赏
  • 举报
回复
另一法:用inte下载源代码,再分析此文本

s= " .... <FirstName>Mei ......"
for i 1 to len(s)
j=instr(i,s,"<FirstName>")
if j>1 then
debug.print trim(mid(j,s,10))
exit for
end if
net
king06 2009-11-25
  • 打赏
  • 举报
回复
单单取值
GetNodeValue = value_node.Attributes(0).nodeTypedValue
Tiger_Zhao 2009-11-25
  • 打赏
  • 举报
回复
我5楼的函数试过没有?结果对不对?
  • 打赏
  • 举报
回复
BillNumberTag 是我的函数名,换
  • 打赏
  • 举报
回复
1, Node.selectSingleNode(ElementName).Text
2, Set objAttr = objXML.createAttribute(BillNumberTag)
objAttr.Text =
如有xml6,用6
贝隆 2009-11-24
  • 打赏
  • 举报
回复
帮顶
SUGENG328 2009-11-24
  • 打赏
  • 举报
回复
谢谢king06,不过这样只能取到对应Node的属性值,而不是对应的结点的值
如:
<Values>
<FirstName type="123">Mei
<First>test1 </First>
<Second>test2 </Second>
</FirstName>
</Values>
Node.attributes(0).nodeName (取得对应值为:FirstName)
Node.attributes(0).xml (取得对应值为:type="123")
我想要取的是"Mei",我看算了,还是把所有这整个节点的值都读出来,再做分离动作了

  • 打赏
  • 举报
回复
dom结构其实很简单 无非就是结点(node) 属性(attribute) 文本(text) 之间的关系

而之间关系靠的是xpath去寻找的
  • 打赏
  • 举报
回复
Set values_node2 = xml_document.selectSingleNode(".//" & "FirstName")

实际上你对于xpath的理解很糟糕

selectsinglenode里面就是字符串...而内容就是xpath
king06 2009-11-24
  • 打赏
  • 举报
回复
    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
king06 2009-11-24
  • 打赏
  • 举报
回复
仅要取得节点的名称及其内容:
Node.attributes(0).nodeName
Node.attributes(0).xml
Tiger_Zhao 2009-11-24
  • 打赏
  • 举报
回复
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
SUGENG328 2009-11-24
  • 打赏
  • 举报
回复

<Values>
<FirstName>Mei
<First>test1 </First>
<Second>test2 </Second>
</FirstName>
</Values>
需要得到<FirstName>的文本Mei,按照Node.selectSingleNode(ElementName).Text
得到的会是Mei test1 test2这三个text,这样会取出含该结点及该结点下的子结点的Text,现在只想取到Mei,想请教如何做?
king06 2009-11-24
  • 打赏
  • 举报
回复
1) GetNodeValue = Node.selectSingleNode(node_name).Text

2) e.g.
    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

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧