|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Reading XML data to VB Variablesup 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 ? 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 ? > Thank you to all that helpped me earlier in the week. I have amanaged to <code snipped>> come > up with the following code to read the XML file. > what i need to do is assign the value of 'status' from the xml data to Try something like this:> vbstatus variable ... i will need to do thsi with a few other of the data > values from the xml data as well '*** vbStatus = oXMLDom.selectSingleNode("/PaymentFile/@status").Text '*** Hope this helps, Mike - Microsoft Visual Basic MVP - E-Mail: ED***@mvps.org WWW: Http://EDais.mvps.org/ 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/ > > > 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/ >> >> >> > Mike i keep getting an error on that , but the suggestion Steve put works Basically selectSingleNode cant find the node and so when VB tries to read > 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 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/ "Peter Newman" <PeterNew***@discussions.microsoft.com> wrote in message vbstatus = oNode.Attributes.getNamedItem("status").nodeValuenews: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 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 ? "Bonj" <a@b.com> wrote in message I haven't heard that before. Why?news:%238vCh9SaFHA.2212@TK2MSFTNGP14.phx.gbl... > BTW 'dim as new' is considered bad for production code. > 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 "Jim Edgar @cox.net>" <djedgar<removethis> wrote in message Because it gives the programmer more control over construction> "Bonj" <a@b.com> wrote in message > > BTW 'dim as new' is considered bad for production code. > > I haven't heard that before. Why? 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 > Apparently, using the above automatically tells the VB compiler to > Dim oXMLDom As New DOMDocument30 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 > then it just spits out "oXMLDom.AMethod()" as-is, because by not using the > Dim oXMLDom As DOMDocument30 > Set oXMLDom = New DOMDocument30 > "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.
Show quote
Hide quote
"Bonj" <a@b.com> wrote in message Thanks Larry and Bonj for the insight. I'll be careful about declaringnews: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. > > variables as New in the future. Jim Edgar |
|||||||||||||||||||||||