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

VB6 - XML Xpath to return value of single node? 2

Status
Not open for further replies.

ftpdoo

Programmer
Aug 9, 2001
202
GB

Hi,

does anyone have a function that if passed in the tag name i'm searching for. ie getNode("<Help>") that it will return the value of Help in the XML file?

Many thanks,
JD
 
You might want to look at getElementsByTagName in Microsoft's XML library
 
I think you are looking for:
SelectSingleNode(xpath expression)

For multiple nodes (Node List) use:
SelectNodes(xpath expression)

Example XML:
Code:
<test>
  <help>This is the help tag</help>
  <item>
    <name>Item 1</name>
    <data>123</data>
  </item>
  <item>
    <name>Item 2</name>
    <data>456</data>
  </item>
  <item>
    <name>Item 3</name>
    <data>789</data>
  </item>
</test>

Example VB Single Node:
Code:
Dim Dom As Object
Set Dom = CreateObject("MSXML.DomDocument")
Dom.async = False
Dom.Load "XmlFile.xml"
MsgBox Dom.SelectSingleNode("//help").Text

Example VB Node List:
Code:
Dim Dom As Object
Dim X As Object
Dim msg As String
Set Dom = CreateObject("MSXML.DomDocument")
Dom.async = False
Dom.Load "XmlFile.xml"
For Each X In Dom.SelectNodes("//item")
  msg = msg & X.SelectSingleNode("name").Text
  msg = msg & " = "
  msg = msg & X.SelectSingleNode("data").Text
  msg = msg & vbCrLf
Next
MsgBox msg

Hope This Helps ;-)
-Josh

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
By the way, since you are using XML, with VB...

You can place code in strings then load with:
DomDocument.LoadXML

This is Great for including small bits of XML, that you don't want people to modify (such as XSL), to your program...

For small XML data blocks, use Const, or any String Variable...

For Large Files, you can use a Resource File...
(If you are interested in this method, Start A New Thread)

Here is an example using the above examples...
Create a new Project, add 2 command buttons, paste this code in the Code Area, and click Run:
Code:
[b]Const sXML = "<test><help>This is the help tag</help><item><name>Item 1</name><data>123</data></item><item><name>Item 2</name><data>456</data></item><item><name>Item 3</name><data>789</data></item></test>"[/b]

Private Sub Command1_Click()
  Dim Dom As Object
  Set Dom = CreateObject("MSXML.DomDocument")
  Dom.async = False
  [b]Dom.Loadxml sXML[/b]
  MsgBox Dom.SelectSingleNode("//help").Text
End Sub

Private Sub Command2_Click()
  Dim Dom As Object
  Dim X As Object
  Dim msg As String
  Set Dom = CreateObject("MSXML.DomDocument")
  Dom.async = False
  [b]Dom.Loadxml sXML[/b]
  For Each X In Dom.SelectNodes("//item")
    msg = msg & X.SelectSingleNode("name").Text
    msg = msg & " = "
    msg = msg & X.SelectSingleNode("data").Text
    msg = msg & vbCrLf
  Next
  MsgBox msg
End Sub

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top