Scorb
Scorb

Reputation: 1870

Check attribute presence in XML

I have a PowerPoint VBScript that is trying to check if an XML node contains a certain attribute, before it accesses that attribute.

I can not figure out a way to do that?

My latest attempt is as follows:

If Not (xNode.Attributes.ItemOf("name") Is Nothing) Then
  winner_str = winner_str & xNode.GetAttribute("name") & " "
End If

But this code results in

Object doesn't support this property or method

What is going wrong here? What can I do to check if an attribute exists in an XML node?

Upvotes: 0

Views: 1659

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200303

I'm going to assume that you're using an Msxml2.DOMDocument object for parsing the XML data. The Attributes property returns an IXMLDOMNamedNodeMap object, which doesn't have a method ItemOf(). Simply use GetAttribute(). The method returns Null if the attribute doesn't exist.

attr = xNode.GetAttribute("name")
If Not IsNull(attr) Then
  winner_str = winner_str & attr & " "
End If

Upvotes: 2

Related Questions