|
-
Sep 10th, 2001, 06:42 AM
#1
Thread Starter
Lively Member
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
-
Sep 10th, 2001, 07:45 AM
#2
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.
-
Sep 10th, 2001, 07:51 AM
#3
Thread Starter
Lively Member
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
-
Sep 10th, 2001, 07:55 AM
#4
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.
-
Sep 10th, 2001, 07:57 AM
#5
Fanatic Member
Check out an example at my site!
-
Sep 10th, 2001, 07:57 AM
#6
Thread Starter
Lively Member
MSMXL
I have MSXML referenced anyhow, can you show me how you would load the file using it?
-
Sep 10th, 2001, 08:29 AM
#7
This is just a quick example, by no means complete:
VB Code:
'* Define XML variables
Dim l_xmlDoc As DOMDocument
Dim l_xmlRootNode as IXMLDOMNode
Dim l_xmlNode1 As IXMLDOMNode
Dim l_xmlNode2 As IXMLDOMNode
Dim l_xmlNode3 As IXMLDOMNode
'* Define other stuff
Dim l_OK as Boolean
Dim l_i as Integer
Dim l_j as Integer
Dim l_Txt as String
Set xmlDoc = New DOMDocument
xmlDoc.async = False
l_OK = xmlDoc.Load(RequestFileName)
If l_OK = True Then
'* The load was successful
Set l_xmlRootNode = l_xmlDoc.documentElement
'* At this point, l_xmlRootNode.childNodes.length = the
'* number of nodes in the document. You have many
'* functions available to get info from all nodes in the
'* document. Too many to list here. Should look at the
'* object browser after adding
'* the MSXML3 reference.
Set l_xmlNode1 = l_xmlRootNode.firstChild '* Now you have the first node in the document in l_xmlNode
'* Or alternately, you could do this:
For l_i = 1 to l_xmlRootNode.childNodes.length
Set l_xmlNode1 = l_xmlRootNode.childNodes.Item(l_i)
'* In your case, l_xmlNode1 will step through <PersonList> nodes here. So you only need to get the data.
'* Some examples:
l_Txt = l_xmlNode1.firstChild.text '* Gives you the data from the first child.
l_Txt = l_xmlNode1.firstChild.baseName '* Gives you the Tag Name (such as Person)
l_j = l_xmlNode1.firstChild.attributes.length '* Gives you the number of attributes on your entry, 4 in your
'* example. This could vary if the number of attributes was
'* different for each Person
l_Txt = l_xmlNode1.firstChild.attributes.item(1).text gives you the text for the first attribute (such as title)
Next l_i
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|