Results 1 to 4 of 4

Thread: Pls can someone check my VB/XML thinking

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2000
    Posts
    350

    Question 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:
    1. 'just the part that loops thru' the nodes as described above..
    2.  
    3. For Each Child In xmlRoot.childNodes
    4.   If Child.nodeName = "Person" Then
    5.      For Each Child2 In Child.childNodes
    6.          If Child2.nodeName = "LastName" And Child2.Text = TextLastName Then
    7.              For Each Child3 In Child.childNodes
    8.                 If Child3.nodeName = "FirstName" Then
    9.                  ListFirstName.AddItem Child3.Text
    10.                 End If
    11.              Next Child3
    12.          End If
    13.      Next Child2
    14.   Else
    15.   MsgBox "Node name: " & Child.nodeName, , "Non person!"
    16.   End If
    17. Next Child

    So any thoughts anyone?

  2. #2
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845
    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?
    Mark
    -------------------

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2000
    Posts
    350
    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...

  4. #4
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845
    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
    Mark
    -------------------

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width