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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

can't get attribute value

Status
Not open for further replies.

cdc

IS-IT--Management
Aug 4, 2001
1
US
To anyone that may help.

I have just started learning xml and am having trouble with accessing an attribute (I think it's called an attribute) in an xml document.

For an example here is some source code from the xml doc.

<reply>
<lastPrice value=&quot;6.550&quot; ></lastPrice>
</reply>

I have a good knowledge of visual basic and am trying to write a program that will access the xml doc with the following example code that I have written.

Dim xmldoc
Set xmldoc = CreateObject(&quot;Microsoft.XMLDOM&quot;)
xmldoc.async = False
xmldoc.Load (&quot;c:\My Documents\snapshot.xml&quot;)

MsgBox xmldoc.documentElement.childNodes.Item(0).getAttribte (value)

With this code I am trying to attain the value of 6.550 from the above xml document (above VB code example placing it in a message box).

If anyone has any suggestion on how I might get this to work I would be much appreciated.

Thanking you in advance.

 
Try this:
[tt]

Dim MyNodeList As IXMLDOMNodeList
Dim MyElemList As IXMLDOMElement

Set MyNodeList = MyDomDoc.getElementsByTagName(&quot;lastPrice&quot;)

For i = 0 To (MyNodeList.length - 1)
[tab]Set MyElemList = MyNodeList.Item(iCount)
[tab]dPrice = MyElemList.getAttribute(&quot;value&quot;)

[tab]' other code here

Next i
[/tt]

One thing -- I think you're OK using the attribute name &quot;value&quot;, but it's kind of close to being a reserved word. Maybe you ought to choose another name for it. Also, if there's only one attribute, you might be better off storing your value in the element.
[tab]<listPrice>6.550</listPrice>

One other thing, sometimes element names can be case-sensitive (depends on the parser). You might be better off using all upper-case element names.
[tab]<LISTPRICE>6.550</LISTPRICE>

Hope this helps.

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top