Reading/parsing an XML file into VB.
Author |
Message |
Ian Cowle #1 / 6
|
 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 < |
D.. #2 / 6
|
 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 < |
the Wi #3 / 6
|
 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 < |
Dag Sund #4 / 6
|
 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 |
|
 |
Dag Sund #5 / 6
|
 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 |
|
 |
Ian Cowle #6 / 6
|
 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 |
|
|
|