VB6.0如何读写XML结点
<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,还请各位大侠多多指教!
--------------------编程问答-------------------- 1, Node.selectSingleNode(ElementName).Text
2, Set objAttr = objXML.createAttribute(BillNumberTag)
objAttr.Text =
如有xml6,用6 --------------------编程问答-------------------- BillNumberTag 是我的函数名,换 --------------------编程问答-------------------- 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
<Values>
<FirstName>Mei
<First>test1 </First>
<Second>test2 </Second>
</FirstName>
</Values>
需要得到<FirstName>的文本Mei,按照Node.selectSingleNode(ElementName).Text
得到的会是Mei test1 test2这三个text,这样会取出含该结点及该结点下的子结点的Text,现在只想取到Mei,想请教如何做?
--------------------编程问答--------------------
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
Node.attributes(0).nodeName
Node.attributes(0).xml
--------------------编程问答--------------------
If value_node Is Nothing Then--------------------编程问答-------------------- Set values_node2 = xml_document.selectSingleNode(".//" & "FirstName")
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
实际上你对于xpath的理解很糟糕
selectsinglenode里面就是字符串...而内容就是xpath --------------------编程问答-------------------- dom结构其实很简单 无非就是结点(node) 属性(attribute) 文本(text) 之间的关系
而之间关系靠的是xpath去寻找的 --------------------编程问答-------------------- 谢谢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",我看算了,还是把所有这整个节点的值都读出来,再做分离动作了
--------------------编程问答-------------------- 帮顶 --------------------编程问答-------------------- 我5楼的函数试过没有?结果对不对? --------------------编程问答-------------------- 单单取值
GetNodeValue = value_node.Attributes(0).nodeTypedValue
--------------------编程问答-------------------- 另一法:用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 --------------------编程问答--------------------
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
补充:VB , 基础类