Home All Groups Group Topic Archive Search About

Reading XML data to VB Variables

Author
3 Jun 2005 10:46 AM
Peter Newman
Thank you to all that helpped me earlier in the week. I have amanaged to come
up with the following code to read the XML file. 

Dim vbStatus  as string
DIM 'vbprocessingDay as string

    Dim oXMLDom As New DOMDocument30
    oXMLDom.async = False
    oXMLDom.validateOnParse = False
    oXMLDom.resolveExternals = False
    oXMLDom.preserveWhiteSpace = False
    If oXMLDom.Load(LoadPath) = False Then
        MsgBox "Failed to load xml data from file."
        Exit Sub
    End If


    Dim oNodes As IXMLDOMNodeList

    Set oNodes = oXMLDom.selectNodes("//PaymentFile")

    strout = strout _
           + "Results from selectNodes:" + vbNewLine
    For i = 0 To oNodes.length - 1
        Set oNode = oNodes.nextNode
        If Not (oNode Is Nothing) Then
            sname = oNode.nodeName

' assign data to variables
'vbStatus =
'vbprocessingDay =
        End If
    Next

if i do a debug.print on onode.xml  i can see that the xml data is loaded 
as follows

<PaymentFile xmlns="http://bacs.co.uk/submissions" status="complete"
index="2" paymentFileIdentifier="001" processingDay="2005-06-01"
currency="GBP" creditRecordCount="0" creditValueTotal="0"
debitRecordCount="19" debitValueTotal="150984" ddiRecordCount="0" workCode="1
DAILY  ">
    <OriginatingServiceUser userNumber="121212" name="COMPANY NUMBER 2"/>
</PaymentFile>


what i need to do is assign the value of  'status' from the xml data to
vbstatus variable ...  i will need to do thsi with a few other of the data
values from the xml data as well

any ideas on how to modify mu code ?

Author
3 Jun 2005 1:34 PM
Steve Barnett
Not too hot on XML, but try:
    vbStatus = oNode.getAttribute("status")

Steve

Show quoteHide quote
"Peter Newman" <PeterNew***@discussions.microsoft.com> wrote in message
news:A11C6F1C-7D1A-4EA8-8303-69C59A017625@microsoft.com...
> Thank you to all that helpped me earlier in the week. I have amanaged to
> come
> up with the following code to read the XML file.
>
> Dim vbStatus  as string
> DIM 'vbprocessingDay as string
>
>    Dim oXMLDom As New DOMDocument30
>    oXMLDom.async = False
>    oXMLDom.validateOnParse = False
>    oXMLDom.resolveExternals = False
>    oXMLDom.preserveWhiteSpace = False
>    If oXMLDom.Load(LoadPath) = False Then
>        MsgBox "Failed to load xml data from file."
>        Exit Sub
>    End If
>
>
>    Dim oNodes As IXMLDOMNodeList
>
>    Set oNodes = oXMLDom.selectNodes("//PaymentFile")
>
>    strout = strout _
>           + "Results from selectNodes:" + vbNewLine
>    For i = 0 To oNodes.length - 1
>        Set oNode = oNodes.nextNode
>        If Not (oNode Is Nothing) Then
>            sname = oNode.nodeName
>
> ' assign data to variables
> 'vbStatus =
> 'vbprocessingDay =
>        End If
>    Next
>
> if i do a debug.print on onode.xml  i can see that the xml data is loaded
> as follows
>
> <PaymentFile xmlns="http://bacs.co.uk/submissions" status="complete"
> index="2" paymentFileIdentifier="001" processingDay="2005-06-01"
> currency="GBP" creditRecordCount="0" creditValueTotal="0"
> debitRecordCount="19" debitValueTotal="150984" ddiRecordCount="0"
> workCode="1
> DAILY  ">
>    <OriginatingServiceUser userNumber="121212" name="COMPANY NUMBER 2"/>
> </PaymentFile>
>
>
> what i need to do is assign the value of  'status' from the xml data to
> vbstatus variable ...  i will need to do thsi with a few other of the data
> values from the xml data as well
>
> any ideas on how to modify mu code ?
Author
3 Jun 2005 1:49 PM
Mike D Sutton
> Thank you to all that helpped me earlier in the week. I have amanaged to
> come
> up with the following code to read the XML file.
<code snipped>
> what i need to do is assign the value of  'status' from the xml data to
> vbstatus variable ...  i will need to do thsi with a few other of the data
> values from the xml data as well

Try something like this:

'***
vbStatus = oXMLDom.selectSingleNode("/PaymentFile/@status").Text
'***

Hope this helps,

    Mike


- Microsoft Visual Basic MVP -
E-Mail: ED***@mvps.org
WWW: Http://EDais.mvps.org/
Author
3 Jun 2005 2:27 PM
Peter Newman
Mike i keep getting an error on that , but the suggestion Steve put works for
the status but not if i want to get the name  data as well from the
OriginatingServiceUser

the error i get from your suggestion is 

Object variable or With block variable not set



Show quoteHide quote
"Mike D Sutton" wrote:

> > Thank you to all that helpped me earlier in the week. I have amanaged to
> > come
> > up with the following code to read the XML file.
> <code snipped>
> > what i need to do is assign the value of  'status' from the xml data to
> > vbstatus variable ...  i will need to do thsi with a few other of the data
> > values from the xml data as well
>
> Try something like this:
>
> '***
> vbStatus = oXMLDom.selectSingleNode("/PaymentFile/@status").Text
> '***
>
> Hope this helps,
>
>     Mike
>
>
>  - Microsoft Visual Basic MVP -
> E-Mail: ED***@mvps.org
> WWW: Http://EDais.mvps.org/
>
>
>
Author
3 Jun 2005 3:39 PM
Steve Barnett
OriginatingServiceUser is a child node of the currently selected PaymentFile
node, so you need to get a reference to it before you can get it's
attributes. Assuming you have a PaymentFile node (called oNode) you'll need
code like this to access the field you're after:

    Dim xmlNode As IXMLDOMElement

    '*** I ASSUME THERE IS ONLY ONE CHILD NODE IN THIS CODE!!!
    Set xmlNode =
oNode.getElementsByTagName("OriginatingServiceUser").Item(0)
    vbName = oNode.getAttribute("name")

The getElementsByTagName returns an array of elements, so you might want to
add code to check the number of items in the array (it has a length
property). Because I'm working off a specific PaymentFile node, I'll only
pick up OriginatingServiceUser nodes that exist below it.

Steve

Show quoteHide quote
"Peter Newman" <PeterNew***@discussions.microsoft.com> wrote in message
news:66386BCE-67C8-44A2-ABE5-5832A34C0CBB@microsoft.com...
> Mike i keep getting an error on that , but the suggestion Steve put works
> for
> the status but not if i want to get the name  data as well from the
> OriginatingServiceUser
>
> the error i get from your suggestion is
>
> Object variable or With block variable not set
>
>
>
> "Mike D Sutton" wrote:
>
>> > Thank you to all that helpped me earlier in the week. I have amanaged
>> > to
>> > come
>> > up with the following code to read the XML file.
>> <code snipped>
>> > what i need to do is assign the value of  'status' from the xml data to
>> > vbstatus variable ...  i will need to do thsi with a few other of the
>> > data
>> > values from the xml data as well
>>
>> Try something like this:
>>
>> '***
>> vbStatus = oXMLDom.selectSingleNode("/PaymentFile/@status").Text
>> '***
>>
>> Hope this helps,
>>
>>     Mike
>>
>>
>>  - Microsoft Visual Basic MVP -
>> E-Mail: ED***@mvps.org
>> WWW: Http://EDais.mvps.org/
>>
>>
>>
Author
3 Jun 2005 3:39 PM
Mike D Sutton
> Mike i keep getting an error on that , but the suggestion Steve put works
> for
> the status but not if i want to get the name  data as well from the
> OriginatingServiceUser
>
> the error i get from your suggestion is
>
> Object variable or With block variable not set

Basically selectSingleNode cant find the node and so when VB tries to read
the .text property of it, it falls over and you get the error.
The code I posted was tested with the XML you posted in your original
message so should work fine unless the structure of your document is
different.  By the looks of it you may have multiple PaymentFile nodes
sitting within a root element, in which case the XPath query wouldn't work
in the same way since it's look for a root element called PaymentFile.
To make this work per node you can change it to this:

'***
Dim PaymentNode As IXMLDOMElement

For Each PaymentNode In oNodes
    Debug.Print PaymentNode.selectSingleNode("@status").Text
Next PaymentNode
'***

Hope this helps,

    Mike


- Microsoft Visual Basic MVP -
E-Mail: ED***@mvps.org
WWW: Http://EDais.mvps.org/
Author
3 Jun 2005 3:13 PM
Jeff Johnson [MVP: VB]
"Peter Newman" <PeterNew***@discussions.microsoft.com> wrote in message
news:A11C6F1C-7D1A-4EA8-8303-69C59A017625@microsoft.com...

> what i need to do is assign the value of  'status' from the xml data to
> vbstatus variable ...  i will need to do thsi with a few other of the data
> values from the xml data as well

vbstatus = oNode.Attributes.getNamedItem("status").nodeValue
Author
4 Jun 2005 6:01 PM
Bonj
BTW 'dim as new' is considered bad for production code.

Show quoteHide quote
"Peter Newman" <PeterNew***@discussions.microsoft.com> wrote in message
news:A11C6F1C-7D1A-4EA8-8303-69C59A017625@microsoft.com...
> Thank you to all that helpped me earlier in the week. I have amanaged to
> come
> up with the following code to read the XML file.
>
> Dim vbStatus  as string
> DIM 'vbprocessingDay as string
>
>    Dim oXMLDom As New DOMDocument30
>    oXMLDom.async = False
>    oXMLDom.validateOnParse = False
>    oXMLDom.resolveExternals = False
>    oXMLDom.preserveWhiteSpace = False
>    If oXMLDom.Load(LoadPath) = False Then
>        MsgBox "Failed to load xml data from file."
>        Exit Sub
>    End If
>
>
>    Dim oNodes As IXMLDOMNodeList
>
>    Set oNodes = oXMLDom.selectNodes("//PaymentFile")
>
>    strout = strout _
>           + "Results from selectNodes:" + vbNewLine
>    For i = 0 To oNodes.length - 1
>        Set oNode = oNodes.nextNode
>        If Not (oNode Is Nothing) Then
>            sname = oNode.nodeName
>
> ' assign data to variables
> 'vbStatus =
> 'vbprocessingDay =
>        End If
>    Next
>
> if i do a debug.print on onode.xml  i can see that the xml data is loaded
> as follows
>
> <PaymentFile xmlns="http://bacs.co.uk/submissions" status="complete"
> index="2" paymentFileIdentifier="001" processingDay="2005-06-01"
> currency="GBP" creditRecordCount="0" creditValueTotal="0"
> debitRecordCount="19" debitValueTotal="150984" ddiRecordCount="0"
> workCode="1
> DAILY  ">
>    <OriginatingServiceUser userNumber="121212" name="COMPANY NUMBER 2"/>
> </PaymentFile>
>
>
> what i need to do is assign the value of  'status' from the xml data to
> vbstatus variable ...  i will need to do thsi with a few other of the data
> values from the xml data as well
>
> any ideas on how to modify mu code ?
Author
4 Jun 2005 6:13 PM
Jim Edgar
"Bonj" <a@b.com> wrote in message
news:%238vCh9SaFHA.2212@TK2MSFTNGP14.phx.gbl...
> BTW 'dim as new' is considered bad for production code.
>

I haven't heard that before.  Why?

Isn't

Dim oXMLDom As New DOMDocument30

the same as (not the same, but similar)

Dim oXMLDom As DOMDocument30
Set oXMLDom = New DOMDocument30

Jim Edgar
Author
4 Jun 2005 6:56 PM
Larry Serflaten
"Jim Edgar @cox.net>" <djedgar<removethis> wrote in message
> "Bonj" <a@b.com> wrote in message

> > BTW 'dim as new' is considered bad for production code.
>
> I haven't heard that before.  Why?

Because it gives the programmer more control over construction
and destruction.  But, as with all the tools in the programmer's
tool box, there are cases for Dim As New, when performace and
control are not high on the list of desired behaviors...

LFS
Author
4 Jun 2005 9:22 PM
Bonj
>
> Dim oXMLDom As New DOMDocument30

Apparently, using the above automatically tells the VB compiler to
effectively replace every call in the function of the type

oXMLDom.AMethod()

with

If oXMLDom Is Nothing Then
    Set oXMLDom = New DOMDocument30
    oXMLDom.AMethod()
Else
    oXMLDom.AMethod()
End If

at compile time - for all uses in the entire function, i.e. involving a test
for Nothing on every method call. Whereas when you use

>
> Dim oXMLDom As DOMDocument30
> Set oXMLDom = New DOMDocument30
>

then it just spits out "oXMLDom.AMethod()" as-is, because by not using the
"Dim ... As New" you've effectively told the compiler that you'll take
responsibility for making sure the object is instantiated, so it doesn't
need to do it for you. But, as Larry says, in some situations the clarity of
having fewer lines of code may be more important to you than avoiding an
unnecessary but slight performance hit.
Author
4 Jun 2005 9:33 PM
Jim Edgar
Show quote Hide quote
"Bonj" <a@b.com> wrote in message
news:eJ6AwtUaFHA.720@TK2MSFTNGP15.phx.gbl...
> >
> > Dim oXMLDom As New DOMDocument30
>
> Apparently, using the above automatically tells the VB compiler to
> effectively replace every call in the function of the type
>
> oXMLDom.AMethod()
>
> with
>
> If oXMLDom Is Nothing Then
>     Set oXMLDom = New DOMDocument30
>     oXMLDom.AMethod()
> Else
>     oXMLDom.AMethod()
> End If
>
> at compile time - for all uses in the entire function, i.e. involving a
test
> for Nothing on every method call. Whereas when you use
>
> >
> > Dim oXMLDom As DOMDocument30
> > Set oXMLDom = New DOMDocument30
> >
>
> then it just spits out "oXMLDom.AMethod()" as-is, because by not using the
> "Dim ... As New" you've effectively told the compiler that you'll take
> responsibility for making sure the object is instantiated, so it doesn't
> need to do it for you. But, as Larry says, in some situations the clarity
of
> having fewer lines of code may be more important to you than avoiding an
> unnecessary but slight performance hit.
>
>

Thanks Larry and Bonj for the insight.  I'll be careful about declaring
variables as New in the future.

Jim Edgar