|
-
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?
-
Mar 5th, 2002, 08:47 AM
#2
Frenzied Member
Why are Firstname and LastName sub elements of Person rather than attributes?
Each peron can only have one first name and one last name so using attributes would make it "tighter"
Or is that aspect beyond your control?
-
Mar 5th, 2002, 08:59 AM
#3
Thread Starter
Hyperactive Member
Well Mark the whole thing's ficticious. I'm trying to get my mind around the concepts right now!
So given that yes, let's say that's the format of the data, have I gone about the ficticious task the right way?
Ta...
-
Mar 5th, 2002, 09:21 AM
#4
Frenzied Member
Firstly let me confess that I've only used XML with java sax parsers
My instincts tell me that looping within the same loop doesn't seem quite right...
especially if there is only expected to be one firstname and one lastname per person.
Code:
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
I would have thought holding the strings in a couple of variables would be better
Code:
Dim lastname As String
Dim firstname As String
For Each Child In xmlRoot.childNodes
If Child.nodeName = "Person" Then
For Each Child2 In Child.childNodes
If Child2.nodeName = "LastName" Then
lastname = Child2.Text
End If
If Child2.nodeName = "FirstName" Then
firstname = Child2.Text
End If
Next Child2
If lastname = TextLastName Then
ListFirstName.AddItem firstname
End If
lastname = "": firstname = ""
Else
MsgBox "Node name: " & Child.nodeName, , "Non person!"
End If
Next Child
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
|