Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.5k views
in Technique[技术] by (71.8m points)

excel - Read XML Attribute VBA

I am trying to get the attribute of a single node in VBA, but can't manage it using DOM

The XML looks like following:

<?xml version="1.0" encoding="utf-8"?>
   <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <soap:Body>
         <GetUserInfoResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/directory/">
            <GetUserInfoResult>
               <GetUserInfo>
                  <User ID="16" Name="" LoginName="login" Email="" Notes="" IsSiteAdmin="False" IsDomainGroup="False" />
            </GetUserInfo>
         </GetUserInfoResult>
      </GetUserInfoResponse>
   </soap:Body>
</soap:Envelope>

I am basically just trying to get the value of the ID attribute. Any help would be appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try:

(Include a reference to Microsoft XML v3, I saved your xml to a file on my desktop)

Dim xmlDoc As DOMDocument30
Set xmlDoc = New DOMDocument30
xmlDoc.Load ("C:usersjondesktopest.xml")

Dim id As String
id = xmlDoc.SelectSingleNode("//GetUserInfo/User").Attributes.getNamedItem("ID").Text

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...