Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading XML Attribute values...

Status
Not open for further replies.

week

MIS
Feb 14, 2001
118
US
How do I get the value for "ID" from this XML?
<T3 Version=&quot;5&quot; DateCreated=&quot;2003/04/30&quot;>
<Layer ID=&quot;AB&quot;>
<LOB>xxxx</LOB>
<PolicyIndicator ID=&quot;Standard&quot; Effective=&quot;2003/05/11&quot;>
</PolicyIndicator>
<PolicyIndicator ID=&quot;Nonstandard&quot; Effective=&quot;2003/07/01&quot;>
</PolicyIndicator>
</Layer>
<Layer ID=&quot;CD&quot;>
<LOB>xxxxx</LOB>
<PolicyIndicator ID=&quot;Standard&quot; Effective=&quot;2003/05/11&quot;>
</PolicyIndicator>
<PolicyIndicator ID=&quot;Nonstandard&quot; Effective=&quot;2003/07/01&quot;>
</PolicyIndicator>
</Layer>
</T3>



My code so far is like this. I had this working but lost the codes and for some reason I am having a hard time gettthing this working...very frustrating. ??? is the area I am having trouble with. Please, be my other set of eyes....very much appreciated.

Dim lndTopNode As Msxml2.IXMLDOMNode
Dim lndChildNode As Msxml2.IXMLDOMNode
Dim lndGrandChildNode As Msxml2.IXMLDOMNode
-- and some more Dim stmt for variables used here...

Set lndTopNode = xmlDoc.selectSingleNode(&quot;T3&quot;)
For Each lndChildNode In lndTopNode.childNodes
If lndChildNode.Attributes.Item(0).Text = lcStateSearch Then
For Each lndGrandChildNode In lndChildNode.childNodes

If lndGrandChildNode.baseName = &quot;LOB&quot; Then
lcLOB = lndGrandChildNode.Text
Else
If lndGrandChildNode.Attributes.length <> 0 Then
If lndGrandChildNode.Attributes.getNamedItem(&quot;ID&quot;) = &quot;Standard&quot; Then
' Do something But it's blowing up on this line....???
End If
End If

End If
Next
End If
Next
 
if I remember correctly:

if you dim the node as MSXML2.IXMLDOMElement, you can get the value of attributes with:
Code:
If lndGrandChildNode.getAttribute(&quot;ID&quot;)= &quot;Standard&quot; then

if you dim the node as MSXML2.IXMLDOMNode, you'll have to use Node.Attributes.getNamedItem(&quot;ID&quot;). However, this returns a node, not a textvalue.
The correct syntax in your code would be:
Code:
If lndGrandChildNode.Attributes.getNamedItem(&quot;ID&quot;).text = &quot;Standard&quot; then
 
As I was driving back to home after my tries at work, I thought I need to change my syntax to what you suggested:

If lndGrandChildNode.Attributes.getNamedItem(&quot;ID&quot;).text = &quot;Standard&quot; then

I am pretty sure that's what it takes to solve my problem....but will let you know for sure when I try it.

Thanks a lot.
 
If lndGrandChildNode.Attributes.getNamedItem(&quot;ID&quot;).text = &quot;Standard&quot; then

Week -

You need to handle the case for when the attribute is missing, or empty. If you don't, when you access the .text property, you'll get a &quot;illegal operation&quot; error.
Try this:
Code:
Dim IDAttr As MSXML2.IXMLDOMAttribute
Dim IDAttrValue As String

Set IDAttr = lndGrandChildNode.Attributes.getNamedItem(&quot;ID&quot;)
If IDAttr Is Nothing Then
   ' Handle error, or use a default value
   IDAttrValue = &quot;&quot; 
Else
   IDAttrValue = IDAttr.Text
End If
If IDAttrValue = &quot;Standard&quot; Then
Also don't forget that attribute names are case-sensitive (been burned by that enough times!)

Chip H.
 
Thanks for your tip Chip. I will put that in. By the way, as it is very obvious

If lndGrandChildNode.Attributes.getNamedItem(&quot;ID&quot;).text = &quot;Standard&quot; then

worked greatly. Just wanted to let other people know who might not be very familiar with this subject.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top