Reading/parsing an XML file into VB. 
Author Message
 Reading/parsing an XML file into VB.

I have the following file format (downloaded, so I can't change the format):
<?xml version="1.0" encoding="ISO-8859-1"?>
 <loc version="1.0" src="EasyGPS">

  <waypoint>
   <name id="GCB9AD">
    <![CDATA[Random Name Here]]>
   </name>
   <coord lat="52.00815" lon="0.23545"/>
   <type>box</type>
   <link text="Details"> http://www.*-*-*.com/ ;/link>
  </waypoint>

  <waypoint>
   <name id="GCBD59">
    <![CDATA[Different Random Name here]]>
   </name>
   <coord lat="52.0750333333333" lon="0.4402"/>
   <type>box</type>
   <link text="Details"> http://www.*-*-*.com/ ;/link>
  </waypoint>

  <waypoint>
  .
  .
  .

</loc>

The file can contain anything up to 6000 <waypoint>...</waypoint> entries.
I am only interested in extracting the <name id=" ">, the <![CDATA..> and
<coord ..> entries - all the others can be scrapped.

Is there a nice easy, fast way of importing all this data from a text file
into a set of arrays
x = number_of_entries
names(1 to x)
cdata(1 to x)
lat(1 to x)
lon(1 to x)

AT present, I'm importing the whole file into a single text string, then
using a Do...Loop with several instr$ commands, but when operating on a
1.76MB string, it's really rather slow.

Sometimes the text files are nicely formatted, as above, sometimes they have
no LF CR or tab characters at all.  And the URLs and CDATA fields can
sometimes be as long as 500 characters, which scuppered a previous idea I
had of reading the file in chunks, as I kept ending up in the middle of data
fields.

Is there a nice XML-reading module/add-on for VB?

I'm using VB5 (SP3) on Windows XP.

Many thanks,

--
Ian Cowley (Not Reverend), Cambridge, UK
**Perfecting pedantry through practice**

Remove safety net before mailing me



Sat, 25 Jun 2005 00:51:58 GMT  
 Reading/parsing an XML file into VB.
Hi Ian,
Just food for thought... Don't know just how well this will work on something this big..
You could read the whole file into a string and then do a Split....
ie:
Dim sMyArray() As String
        sMyArray = Split(YourXMLString, "<")
Then run through the sMyArray looking for the first character of what you want.
ie:
If Left$(sMyArray(loop), 8) = "name id=" Then do what you need to do....

If Case is a problem then use the UCase or LCase in the If... Then

Like I said .. Just food for thought

Quote:

>I have the following file format (downloaded, so I can't change the format):
><?xml version="1.0" encoding="ISO-8859-1"?>
> <loc version="1.0" src="EasyGPS">

>  <waypoint>
>   <name id="GCB9AD">
>    <![CDATA[Random Name Here]]>
>   </name>
>   <coord lat="52.00815" lon="0.23545"/>
>   <type>box</type>
>   <link text="Details">http://www.randomurl.com/seek?47445</link>
>  </waypoint>

>  <waypoint>
>   <name id="GCBD59">
>    <![CDATA[Different Random Name here]]>
>   </name>
>   <coord lat="52.0750333333333" lon="0.4402"/>
>   <type>box</type>
>   <link text="Details">http://www.randomurl.com/seek?34569</link>
>  </waypoint>

>  <waypoint>
>  .
>  .
>  .

></loc>

>The file can contain anything up to 6000 <waypoint>...</waypoint> entries.
>I am only interested in extracting the <name id=" ">, the <![CDATA..> and
><coord ..> entries - all the others can be scrapped.

>Is there a nice easy, fast way of importing all this data from a text file
>into a set of arrays
>x = number_of_entries
>names(1 to x)
>cdata(1 to x)
>lat(1 to x)
>lon(1 to x)

>AT present, I'm importing the whole file into a single text string, then
>using a Do...Loop with several instr$ commands, but when operating on a
>1.76MB string, it's really rather slow.

>Sometimes the text files are nicely formatted, as above, sometimes they have
>no LF CR or tab characters at all.  And the URLs and CDATA fields can
>sometimes be as long as 500 characters, which scuppered a previous idea I
>had of reading the file in chunks, as I kept ending up in the middle of data
>fields.

>Is there a nice XML-reading module/add-on for VB?

>I'm using VB5 (SP3) on Windows XP.

>Many thanks,

Have a good day...

Don



Sat, 25 Jun 2005 01:41:09 GMT  
 Reading/parsing an XML file into VB.

Quote:

>I have the following file format (downloaded, so I can't change the format):
><?xml version="1.0" encoding="ISO-8859-1"?>
> <loc version="1.0" src="EasyGPS">

>  <waypoint>
>   <name id="GCB9AD">
>    <![CDATA[Random Name Here]]>
>   </name>
>   <coord lat="52.00815" lon="0.23545"/>
>   <type>box</type>
>   <link text="Details">http://www.randomurl.com/seek?47445</link>
>  </waypoint>

>  <waypoint>
>   <name id="GCBD59">
>    <![CDATA[Different Random Name here]]>
>   </name>
>   <coord lat="52.0750333333333" lon="0.4402"/>
>   <type>box</type>
>   <link text="Details">http://www.randomurl.com/seek?34569</link>
>  </waypoint>

>  <waypoint>
>  .
>  .
>  .

></loc>

>The file can contain anything up to 6000 <waypoint>...</waypoint> entries.
>I am only interested in extracting the <name id=" ">, the <![CDATA..> and
><coord ..> entries - all the others can be scrapped.

>Is there a nice easy, fast way of importing all this data from a text file
>into a set of arrays
>x = number_of_entries
>names(1 to x)
>cdata(1 to x)
>lat(1 to x)
>lon(1 to x)

>AT present, I'm importing the whole file into a single text string, then
>using a Do...Loop with several instr$ commands, but when operating on a
>1.76MB string, it's really rather slow.

>Sometimes the text files are nicely formatted, as above, sometimes they have
>no LF CR or tab characters at all.  And the URLs and CDATA fields can
>sometimes be as long as 500 characters, which scuppered a previous idea I
>had of reading the file in chunks, as I kept ending up in the middle of data
>fields.

>Is there a nice XML-reading module/add-on for VB?

>I'm using VB5 (SP3) on Windows XP.

>Many thanks,

Offhand, I'd suggest parsing on the ">" to separate entries, since every field
ends that way.  

If you open the file for binary, then you can seek forward and backward, which
allows you to read chunks and move to the beginning of an incomplete field for
the next read.  If the chunk ends with any character other than ">", search back
to the field's opening "<" and determine what the seek should be to start
reading the next chunk at the first character of the field.

VB3 source code: http://thelabwiz.home.mindspring.com/vbsource.html
VB6 source code: http://thelabwiz.home.mindspring.com/vb6source.html
VB6 - MySQL how to: http://thelabwiz.home.mindspring.com/mysql.html
Fix the obvious to reply by email.



Sat, 25 Jun 2005 11:35:11 GMT  
 Reading/parsing an XML file into VB.

Quote:
> I have the following file format (downloaded, so I can't change the
format):
> <?xml version="1.0" encoding="ISO-8859-1"?>

Hello!

Don't listen to the "parse the file manually" guys. This is an
xml file, and then you use an xml-parser. (Buildt into windows
since the arrival of IE5.0)

Just give mi a couple of minutes, and I'll put up a sample for you.

--
Dag.



Sat, 25 Jun 2005 16:46:12 GMT  
 Reading/parsing an XML file into VB.
Here's the code I promised:
(You need to add "Microsoft XML, v3" in References.)

HTH...

--
Dag.

Paste everything below into a form:
'---------------------------------------------------
Option Explicit

Private m_oDomDocument As MSXML2.DOMDocument
Private m_oRootNode As MSXML2.IXMLDOMNode

' The OP wanted the data in arrays,
' I would have kept them in the DOM-tree,
' Or made a UDT more fitting.
Private m_asName() As String
Private m_asDescription() As String
Private m_adLatitude() As Double
Private m_adLongitude() As Double

Private Sub Form_Load()

    ' Create a new DOM parser, and load our waypoint file
    Set m_oDomDocument = New MSXML2.DOMDocument
    If Not m_oDomDocument.Load(App.Path & "/Waypoint.xml") Then
        MsgBox "Error parsing WayPoint.xml, Reason: " & _
        m_oDomDocument.parseError.reason, vbCritical, "Parse Error"
        Exit Sub
    End If

    ' If we got here, all is well, and we grab the root (top) node
    ' of the xml-tree
    Set m_oRootNode = m_oDomDocument.documentElement

    ' Grab all the "waypoint" structs into a nodelist
    Dim oWayPoints As MSXML2.IXMLDOMNodeList
    Set oWayPoints = m_oRootNode.selectNodes("waypoint")

    ' Now we have all the Waypoints, so it's time to size our arrays
    ReDim m_asName(1 To oWayPoints.length) As String
    ReDim m_asDescription(1 To oWayPoints.length) As String
    ReDim m_adLatitude(1 To oWayPoints.length) As Double
    ReDim m_adLongitude(1 To oWayPoints.length) As Double

    ' Now we just iterate thru each waypoint in the nodelist,
    ' extracting the data we need as we go
    Dim oWayPoint As MSXML2.IXMLDOMNode
    Dim iPoint As Long
    iPoint = 1
    For Each oWayPoint In oWayPoints

        m_asDescription(iPoint) = oWayPoint.selectSingleNode("name").Text
        m_adLatitude(iPoint) =

        m_adLongitude(iPoint) =

        iPoint = iPoint + 1
    Next oWayPoint

    ' And just to prove we have them, send the arrays to the
    ' debug window
    For iPoint = 1 To UBound(m_asName)
        Debug.Print m_asName(iPoint) & vbTab;
        Debug.Print m_asDescription(iPoint) & vbTab;
        Debug.Print m_adLatitude(iPoint) & vbTab;
        Debug.Print m_adLongitude(iPoint) & vbCrLf
    Next iPoint
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)

    Set m_oRootNode = Nothing
    Set m_oDomDocument = Nothing

End Sub

'---------------------------------------------------



Sat, 25 Jun 2005 17:32:27 GMT  
 Reading/parsing an XML file into VB.

Quote:
> Here's the code I promised:
> (You need to add "Microsoft XML, v3" in References.)

> HTH...

Many thanks.
It runs about a million times faster than my manual parsing.

It can open and parse a 1.8MB file with 6282 waypoints in about 5 seconds,
compared to three to four minutes for manual parsing.

Cheers for the help!

--
Ian Cowley (Not Reverend), Cambridge, UK
**Perfecting pedantry through practice**

Remove safety net before mailing me



Sat, 25 Jun 2005 18:17:38 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. vb library/procedure for parsing html/xml/sgml like files

2. Parsing XML file in VB

3. Is it Possible to Parse an XML file using VB

4. Parsing XML from an ASP page that Creates XML fails

5. newbie- VB recordset - reading from XML file

6. how to parse wml (xml) files

7. Parse RSS XML files (news feeds)

8. parse xml file and vbscript

9. parse XML file

10. Parse external XML file in VBA

11. Parsing XML files in VB6

12. Abort SAX while is parsing xml file

 

 
Powered by phpBB® Forum Software