|
-
Mar 5th, 2002, 07:59 AM
#1
Thread Starter
Hyperactive Member
Pls can someone check my VB/XML thinking
Hey everyone,
I've looked at XML and VB/XML tutorials 'till they're coming out my ears and I'm still quite confused. So I thought I'd tackle a trivial task to see if my understanding is OK.
I thought I'd create an XML database of People, where each Person therein has a LastName and a FirstName, thusly:
Code:
<People>
<Person>
<LastName>Fred</LastName>
<FirstName>Bloggs</FirstName>
</Person>
.... more persons
</People>
The task was to load the XML file first; that's easy and I hardcoded the data into the VB.
Then put a LastName in a textbox, and search the XML for that name. For each one found, put the corresponding FirstName in a listbox.
Criteria:
- By mistake, there may be elements other than <Person> right off the root; handle that
- The <FirstName>, <LastName> elements in a <Person> may be missing or in the wrong order; handle that
- There will likely be more than one 'FirstName' for any 'LastName; hence the listbox
My confusion came in looping thru' the nodes. Please someone check the following code to see if I've gone about this the long way round....
For each node off the root, I check it's a Person; if not it's a spurious entry.
Then in each Person, I check if there's a LastName whose text is the req'd surname from the textbox. If it is, then I check LastName's siblings to see if there's a FirstName, and if there is I bung it in the listbox.
What worries me is that this seems a very long way of doing a SELECT..... FROM..... WHERE sort of thing?
VB Code:
'just the part that loops thru' the nodes as described above..
For Each Child In xmlRoot.childNodes
If Child.nodeName = "Person" Then
For Each Child2 In Child.childNodes
If Child2.nodeName = "LastName" And Child2.Text = TextLastName Then
For Each Child3 In Child.childNodes
If Child3.nodeName = "FirstName" Then
ListFirstName.AddItem Child3.Text
End If
Next Child3
End If
Next Child2
Else
MsgBox "Node name: " & Child.nodeName, , "Non person!"
End If
Next Child
So any thoughts anyone?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|