Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retruning a string between 2 strings 2

Status
Not open for further replies.

Overlord44

Programmer
Mar 9, 2004
26
GB
I've been agonising over this for a few hours now, and it's really beginning to do my head in. Put me out of my misery, please. :)

I have a text file loaded into a String (that I've called InString), which contains several XML-like fields such as

<name>*different every time*</name>
<description>*different every time*</description>

etc. Basically, I need a way of going through the String for each of them (there's 7 - name, version, description, creator, URL, minVersion and maxVersion) and returning the data that's between each start and end tag - and for maxVersion, a method for writing a different value back to the relevant spot in InString again.

Any help welcome :)
 
if it's TRUE XML data, you can convert the XML string into a recordset like this:


Set objStream = New ADODB.Stream
objStream.Open
objStream.WriteText xmlData ' xmlData is the variable holding the XML.
objStream.Position = 0
Set rs = New ADODB.Recordset
rs.Open objStream
objStream.Close
if not rs.eof then
' parse recordset
end if

HTH


ciao for niao!

AMACycle

American Motorcyclist Association
 
And if it's not a valid XML Document, you could do it this way.

Add this function

Code:
Public Function GetValue(ByVal AllData As String, ByVal KeyValue As String) As String
    
    Dim cTemp As String
    
    If InStr(AllData, "<" & KeyValue & ">") = 0 Then
        Exit Function
    End If
    
    cTemp = Split(AllData, "<" & KeyValue & ">")(1)
    cTemp = Split(cTemp, "</" & KeyValue & ">")(0)
    
    GetValue = cTemp
    
End Function

Then use it like this...

Msgbox "Name: " & GetValue(InString, "name")



-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Well, it is, but some of the tags are rather deeply nested in several layers of tags I don't care about, I'd prefer not to go XML on it and rather just go through as if it was a regular text file.

Plus I have no idea how to use recordsets ;)
 
Aaaand gmmastros's post was NOT there when i clicked to reply to this topic. That does the trick nicely, thanks!

How about writing back maxVersion?
 
Try something like the following. Since name is a reserved word in VB, you can't use that for a variable name.

Dim xname As String, xdescription As String
'etc.

xname = GetTagContents("name", InString)
xdescription = GetTagContents("description", InString)

'and so on

Function GetTagContents(tagname As String, stringtocheck As String) As String

Dim varstart As Integer, varstop As Integer, varlength as Integer
Dim starttag As String, endtag As String

starttag = "<" & tagname & ">"
endtag = "</" & tagname & ">"

varstart = InStr(stringtocheck, starttag) + Len(starttag)
varstop = InStr(stringtocheck, endtag)
varlength = varstop - varstart

GetTagContents = Mid$(stringtocheck, varstart, varlength)
End Function

Function ReplaceTagContents(tagname As String, stringtocheck As String, newcontents As String) As String

Dim varstart As Integer, varstop As Integer
Dim starttag As String, endtag As String

starttag = "<" & tagname & ">"
endtag = "</" & tagname & ">"

varstart = InStr(stringtocheck, starttag) + Len(starttag)
varstop = InStr(stringtocheck, endtag)

ReplaceTagContents = Left$(stringtocheck, varstart - 1) & newcontents & Mid(stringtocheck, varstop)

End Function

Lee
 
'Since name is a reserved word in VB, you can't use that for a variable name.'

Heh, thanks. That'd be why I kept getting an error in another section of the app, didn't know that.


I just put Lee's function in for the writing - works great, and my file has been modified correctly! Cheers very much. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top