Results 1 to 7 of 7

Thread: XML and VB

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2001
    Posts
    75

    XML and VB

    I have a text file that contains the following elements:

    <PersonList><Person title="Miss" firstname="Joan" surname="Collins" age="62"/><Person title="Mr" firstname="Jack" surname="Smith" age="35"/></PersonList>

    I need to retrieve the elements an put them in an XML dom, which i have done like so:

    Public Function Retrieve(filename) As Collection

    Dim Filenum As Integer
    Set Retrieve = New Collection

    'Get the file number of a free file to read the file's contents to
    Filenum = FreeFile()

    'Open the file
    Open filename For Input As #Filenum

    Dim strXML As String

    '1. get the contents of this file and put it in a xml dom

    Do While Not EOF(Filenum)
    Line Input #Filenum, strXML
    Debug.Print strXML
    Loop

    Close #Filenum


    End Function

    Now I want to be able to loop through each element and retrieve the attribute details i.e Joan, Collins etc. Does anyone know how to create a piece of code that can identify the number of elements in the xml dom or would i have to identify each element via say the Person tag and split the strings?

    Thanks

    Madeline

  2. #2
    veebee
    Guest
    You should use the MSXML3 parser object for this. Makes things easy. You can easily step thru the document, check counts, add nodes, retrieve nodes, etc... with the parser. Also, you would not have to read the document line-by-line to load it. You would use the .Load method and specify the file path. Start by adding a reference in your project to Microsoft XML, v3.0.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2001
    Posts
    75

    Any more info

    Thanks, but i'm quite new to this and could do with a little bit more information than that, do you know anywhere i can find out more about this or can you give me an example of how it can be used to count through the elements?

    Maddy

  4. #4
    veebee
    Guest
    BTW, Line Input will not always work with an XML file. If your project is internet based where XML will be sent to your server, the XML file tags may not necessarily end with CRLF. If you use the parser, you do not need to be concerned with this.

  5. #5
    Fanatic Member Jerry Grant's Avatar
    Join Date
    Jul 2000
    Location
    Dorset, UK
    Posts
    810

    Check out an example at my site!

    Jerry Grant................tnarG yrreJ
    Website: <JG-Design></.net>
    Email: [email protected]
    Working towards a bug free world......
    (Not a Microsoft employee)

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jul 2001
    Posts
    75

    Question MSMXL

    I have MSXML referenced anyhow, can you show me how you would load the file using it?

  7. #7
    veebee
    Guest
    This is just a quick example, by no means complete:

    VB Code:
    1. '* Define XML variables
    2. Dim l_xmlDoc As DOMDocument
    3. Dim l_xmlRootNode as IXMLDOMNode
    4. Dim l_xmlNode1 As IXMLDOMNode
    5. Dim l_xmlNode2 As IXMLDOMNode
    6. Dim l_xmlNode3 As IXMLDOMNode
    7.  
    8. '* Define other stuff
    9. Dim l_OK as Boolean
    10. Dim l_i as Integer
    11. Dim l_j as Integer
    12. Dim l_Txt as String
    13.  
    14. Set xmlDoc = New DOMDocument
    15. xmlDoc.async = False
    16. l_OK = xmlDoc.Load(RequestFileName)
    17.  
    18. If l_OK = True Then
    19.     '* The load was successful
    20.     Set l_xmlRootNode = l_xmlDoc.documentElement
    21.     '* At this point, l_xmlRootNode.childNodes.length = the
    22.     '* number  of nodes in the document.  You have many
    23.     '* functions available to get info from all nodes in the
    24.     '* document.  Too many to list here.  Should look at the
    25.     '* object browser after adding
    26.     '* the MSXML3 reference.
    27.     Set l_xmlNode1 = l_xmlRootNode.firstChild   '* Now you have the first node in the document in l_xmlNode
    28.     '* Or alternately, you could do this:
    29.     For l_i = 1 to l_xmlRootNode.childNodes.length
    30.     Set l_xmlNode1 = l_xmlRootNode.childNodes.Item(l_i)
    31.     '* In your case, l_xmlNode1 will step through <PersonList> nodes here.  So you only need to get the data.
    32.     '* Some examples:
    33.     l_Txt = l_xmlNode1.firstChild.text             '* Gives you the data from the first child.
    34.     l_Txt = l_xmlNode1.firstChild.baseName   '* Gives you the Tag Name (such as Person)
    35.     l_j = l_xmlNode1.firstChild.attributes.length  '* Gives you the number of attributes on your entry, 4 in your
    36.                            '* example.  This could vary if the number of attributes was
    37.                         '* different for each Person
    38.     l_Txt = l_xmlNode1.firstChild.attributes.item(1).text gives you the text for the first attribute (such as title)   
    39.     Next l_i
    40. End If

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