Discussion Locked

Get particular value in XML using VB

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hi I currently have the following XML Document.

-
-
Lot1
-
Pallet1
PartNo1
WorkWeek1

I have a element Lot as seen below I would like to just get the values 'LOT1'. When I run methods like objNode.nodetypedvalue on the objNode I then get all the values under Lot element. Take note that I will be having more lots later. Thanks

5 replies

by brianharris

I loaded your XML into a Document and used this to retrieve Lot1:

xmlDocument.firstChild.firstChild.childNodes(0).nodeValue

Pretty messy. I would suggest that you move Lot1 into the Lot tag as an attribute and Pallet1 into Pallet. So your XML would look like this and be much easier to use the MSXML parser.

PartNo1
WorkWeek1

by for_serge

Hi,

Try to use the following code:

Sub GetLots()
Dim objXML As MSXML.DOMDocument
Dim objElems As MSXML.IXMLDOMNodeList
Dim objElem As MSXML.IXMLDOMNode
Set objXML = New MSXML.DOMDocument
If objXML.Load("test.xml") Then
Set objElems = objXML.selectNodes("/DriveDetails/Lot/textnode()")
For Each objElem In objElems
Debug.Print objElem.Text
Next objElem
Set objElems = Nothing
Set objElem = Nothing
End If Set objXML = Nothing
End Sub

You will obtain "Lot1" value in the Immediate window. This code will work fine for more Lots.

Serge

This discussion is closed to new replies.