<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,还请各位大侠多多指教!
<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,还请各位大侠多多指教!
解决方案 »
- ado 指向新表
- 請教在VB編程中 , 怎樣控制Excel錯誤不出現錯誤提示框呢?謝謝!
- 自动编号类型数据作为select的参数,查询时出问题!如何写这个SQL语句?急!在线等,揭贴!
- treeview控件 当点击一个节点前的checkbox时,如果打勾,则把其子节点前的checkbox全部打勾,
- 在线等!!
- 一个奇怪的问题!代码写的ADO,DATAGRID控件显示不出来,用ADODC控件却可以!
- 必给分,很简单
- 有关win2000服务的问题?
- 始终没有答案的问题:如何让自己的EXE文件在WIN NT用户登录之前启动?
- 如何将程序中用的ACCESS数据库换成单机版的SQL SERVER
- 送分100分:关于VBA中参数的问题
- wise制作安装程序时,拖动安装界面的窗口,原窗口不消失,怎么解决?
2, Set objAttr = objXML.createAttribute(BillNumberTag)
objAttr.Text =
如有xml6,用6
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,想请教如何做?
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
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
如:
<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",我看算了,还是把所有这整个节点的值都读出来,再做分离动作了
GetNodeValue = value_node.Attributes(0).nodeTypedValue
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