说我有一些xml。如何使用VB获取属性值?
答案 0 :(得分:3)
假设您使用的是MSXML库?
以下代码将输出子节点的所有属性值。在这种情况下,XML是: -
<?xml version="1.0" encoding="utf-8"?>
<documents>
<document id="12345" created="2002-09-24" owner="Andy" />
</documents>
因此输出将显示id,created和owner的值。
这是代码: -
Dim XML As String
Dim objXML As New MSXML2.DOMDocument
Dim objElem As MSXML2.IXMLDOMElement
Dim objSub As MSXML2.IXMLDOMElement
XML = "<?xml version=""1.0"" encoding=""utf-8""?><documents><document id=""12345"" created=""2002-09-24"" owner=""Andy"" /></documents>"
If Not objXML.LoadXML(XML) Then
Err.Raise objXML.parseError.ErrorCode, , objXML.parseError.reason
End If
Set objElem = objXML.selectSingleNode("//documents")
For Each objSub In objElem.childNodes
Debug.Print objSub.nodeName
If objSub.Attributes.length > 0 Then
For i = 0 To objSub.Attributes.length - 1
Debug.Print objSub.Attributes(i).nodeName & " - " & objSub.Attributes(i).nodeValue
Next i
End If
Next
答案 1 :(得分:0)