PDA

View Full Version : VB6 XML Parse



Soredavide
03-22-2019, 08:30 AM
I have a VB6 (yes believe it or not it is an XP machine and requires XP, because of a com port barcode scanner). The program is using XML in VB6, I can get the code to load the XML into a richtextbox. I can load my required contents into a nodelist; but when I go to show the parsed contents, it only shows one item twice (I need it to show all items that would match Serial_No )?

I am not really sure how to read XML properly, to determine if I am selecting the correct data in the select nodes (or requesting the correct single node information). If anyone can help I would really appreciate it.


This is the code I came up with

Option Explicit
Dim strXML As String

Private Sub Command1_Click()
On Error Resume Next
Dim objDoc As MSXML2.DOMDocument
Dim objNodelist As IXMLDOMNodeList
Dim objNode As IXMLDOMNode
Dim x As Integer
'x = 0
'load the XML
Set objDoc = New DOMDocument
objDoc.async = False
objDoc.Load App.Path & ("\Inventory4.xml")



' See what you loaded
' Debug.Print objDoc.xml
RichTextBox1.Text = objDoc.xml

' Get a nodelist with all the Inventory nodes
Set objNodelist = objDoc.selectNodes("//Column[Name='Serial_No']/Value")

'Loop through the nodelist and pull the vaules you need
For Each objNode In objNodelist
Text1.Text = objNode.selectSingleNode("//Column[Name='Serial_No']/Value").xml & vbCrLf & Text1.Text
Next objNode

'Cleanup
Set objNodelist = Nothing
Set objDoc = Nothing
'
End Sub

Private Sub Form_Load()
Text1.Text = ""
End Sub


This the XML

<soap:Envelope xmlns:soap="[Only registered and activated users can see links]"
xmlns:xsi="[Only registered and activated users can see links]"
xmlns:xsd="[Only registered and activated users can see links]">
<soap:Body>
<ExecuteDataSourceResponse xmlns="[Only registered and activated users can see links]">
<ExecuteDataSourceResult>
<StatusNo>0</StatusNo>
<Error>false</Error>
<ErrorNo>0</ErrorNo>
<Message>Success</Message>
<InstanceNo>227</InstanceNo>
<DataSourceKey>1833</DataSourceKey>
<QuarantinedForDevelopment xsi:nil="true"/>
<LastPrimaryDeployment xsi:nil="true"/>
<LastTestDeployment xsi:nil="true"/>
<Version xsi:nil="true"/>
<DataSourceName>Inventory_By_Type_Get</DataSourceName>
<ResultSets>
<ResultSet>
<RowCount>2</RowCount>
<Rows>
<Row>
<Columns>
<Column>
<Value>CF000007</Value>
<Name>Serial_No</Name>
</Column>
</Columns>
</Row>
<Row>
<Columns>
<Column>
<Value>CF000008</Value>
<Name>Serial_No</Name>
</Column>
</Columns>
</Row>
</Rows>
</ResultSet>
</ResultSets>
</ExecuteDataSourceResult>
</ExecuteDataSourceResponse>
</soap:Body>
</soap:Envelope>

Text1.text shows the below information, I can't get the other Serial_No?
CF000007
CF000007

Bat
03-22-2019, 08:58 AM
Sorry about the misdirect, Soredavide! Try changing this:


Text1.Text = objNode.selectSingleNode("//Column[Name='Serial_No']/Value").xml & vbCrLf & Text1.Text

to this:


Text1.Text = objNode.xml & vbCrLf & Text1.Text

Soredavide
03-22-2019, 09:53 AM
Wow, that was easy. Thanks. I wish I new a little more about reading XML.

---------- Post added at 10:53 AM ---------- Previous post was at 10:20 AM ----------

Is it possible to split all Values and Names into an array, something that can be used later?

<Value>CF000007</Value>
<Name>Serial_No</Name>

<Value>CF000008</Value>
<Name>Serial_No</Name>

Bat
03-22-2019, 10:29 AM
Wow, that was easy. Thanks. I wish I new a little more about reading XML.

---------- Post added at 10:53 AM ---------- Previous post was at 10:20 AM ----------

Is it possible to split all Values and Names into an array, something that can be used later?

<Value>CF000007</Value>
<Name>Serial_No</Name>

<Value>CF000008</Value>
<Name>Serial_No</Name>

Sure! Your "objNodelist" will need to look like this:

Set objNodelist = objDoc.selectNodes("//Column[Name='Serial_No']")


This will select the "Column" node instead just the "Value" node inside.

Next, change your "Text1.Text" propagator to:


Text1.Text = (Text1.Text & objNode.childNodes.item(0).xml & vbCrLf & objNode.childNodes.item(1).xml & vbCrLf)


This will read-in the "Column" node's "Value" and "Name" nodes in the order that you specified above.

If you want the "Column" node itself and not just its children, then you can change your propagator to:


Text1.Text = (Text1.Text & objNode.xml & vbCrLf)


This will include the "Column" node along with everything inside it.

Soredavide
03-22-2019, 11:39 AM
Text1.Text = (Text1.Text & objNode.childNodes.item(0).xml & vbCrLf & objNode.childNodes.item(1).xml & vbCrLf)

objNode.childNodes. does not allow item do you have to assign objNode as array at some point?

Bat
03-22-2019, 11:46 AM
objNode.childNodes. does not allow item do you have to assign objNode as array at some point?

objNode is an IXMLDOMNode ([Only registered and activated users can see links](v%3dvs.85)) object, and the childNodes ([Only registered and activated users can see links]) property should be an IXMLDOMNodeList ([Only registered and activated users can see links](v%3Dvs.85)). I'm retrieving the "Name" and "Value" child nodes of objNode (which is a "Column" node), then I'm retrieving their xml ([Only registered and activated users can see links]) property while separating them with a new line in order to make the output look like what you mentioned above. Are you experiencing any errors with that, or do you not like the formatting?

Soredavide
03-22-2019, 12:02 PM
Text1.Text = (Text1.Text & objNode.childNodes.item(0).xml & vbCrLf & objNode.childNodes.item(1).xml & vbCrLf)

objNode.childNodes. does not allow item do you have to assign objNode as array at some point?

I don't get any autofill after objNode.childnode. and if I use that code I get function of interface marked as restricted or the function uses an automation type not supported in Visual Basic

Bat
03-22-2019, 01:26 PM
objNode.childNodes. does not allow item do you have to assign objNode as array at some point?

I don't get any autofill after objNode.childnode. and if I use that code I get function of interface marked as restricted or the function uses an automation type not supported in Visual Basic

I wasn't able to replicate that issue, but perhaps this will work for you instead:


Text1.Text = (Text1.Text & objNode.selectSingleNode("Value").xml & vbCrLf & objNode.selectSingleNode("Name").xml & vbCrLf)

Soredavide
03-22-2019, 01:45 PM
That worked, I actually went with objNode.parentNode.parentNode.Text. This gives me everything in the Node textwise, the XML I posted was just a tiny sample (the One I am actually using is a lot bigger and I am doing some comparisons, before selecting a serial number).

Thanks again Odd (I am finally starting to understand XML "a little")