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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading KML with VBScript 3

Status
Not open for further replies.

Julierme

Programmer
Jan 28, 2015
13
0
0
BR
Dear VBscript programmers

My code is reading a string with a special character, "Ã", from a KML file. After reading, it is wrinting in a TXT file and another KML file. However, after reading, the code converts automatically the special character:
"Ã" to "Ã"
"Ç" to "Ç"
"Ó" to "Ó"
And So on...

I tried to solve this bug using the following script:

strSearchString = "Ã"
strSearchFor1 = "Ã"
If InStr(1, strSearchString, strSearchFor1) > 0 then
NewString="Ã"
nome= Replace(strSearchString,strSearchFor1,NewString)
End If

But this way has not been efficient. Could someone help me solving this problem so that my variable "name" can store special character?
Any help is very appreciated. Thank you for your time and attention.
 
I don't see a problem storing special characters like that in a variable, but your InStr and Replace calls don't seem correct...

strSearchString should be the line of group of lines that you are replacing "Ã" with "Ã"... I think you want something more like below

Code:
strSearchString = "abcefghijkÃlmnop"
strSearchFor1 = "Ã"
If InStr(1, strSearchString, strSearchFor1) > 0 then
   NewString = "Ã"
   nome = Replace(strSearchString, strSearchFor1, NewString)
End If

nome should then be [tt]"abcefghijk[highlight #FCE94F]Ã[/highlight]lmnop"[/tt]
 
Hi VulcanJedi

Thank you for your reply.

That is the problem: my code has a variable named "nome". The code reads a string from a KML file and stores in "nome"

When the string has a special character such those found on the Standard CHR Values, the variable "nome" cannot store the special character. My point is: how can I read this special character from the string and store in "nome". When the code reads the special character, it converts to a junk of characters.

Best regards
 
Hi Guitarzan

Thank you for your prompt reply.

I wast my mistake while writing the e-mail. My code has the same response as yours. However this is not a good way to work, because the code reads special characters and converts in junk special characters.
I need to keep the special characters stored in variable "name". The code must read and store the special character in "name"

Many thanks

Best Regards
 
How are you determining the result is junk characters? Or more specifically, what are you doing with "nome" after the replace? I made a simple sample using the code I posted, using the FileSystem Object to write the "before" and "after" strings to a text file, and it worked fine for me.
 
Another point:

How can I read special character from a txt file?
 
Using the FileSystem Object. For example:
Code:
strSearchString = "abcefghijkÃlmnop"
strSearchFor1 = "Ã"
NewString = "Ã"

strMyPath = "c:\Mypath\Myfile.txt"

nome = Replace(strSearchString, strSearchFor1, NewString)

Const ForReading = 1, ForWriting = 2
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(strMyPath, ForWriting, True)
f.WriteLine strSearchString
f.WriteLine "becomes"
f.WriteLine nome

Set f = fso.OpenTextFile(strMyPath, ForReading)
nome = f.ReadLine
If InStr(1, nome, strSearchFor1) Then
   Wscript.echo "The contents of strSearchFor1 was found in the first line!!!"
End If
 
The problem is that KML files are (normally) UTF-8 encoded. And vbscript doesn't do UTF-8 (and nor does the filesystemobject). Hence the junk that you see, and hence the apparent conversion (it isn't the variable that is causing the issue).

So you need a component that does know UTF-8, such as the ADO stream. Here is an example:

Code:
[blue]    adSaveCreateOverWrite = 2
    Dim objStream, strData
    Set objStream = CreateObject("ADODB.Stream")
    objStream.Charset = "utf-8" [green]' select encoding stream will use
    
    ' Open for reading[/green]
    objStream.Open
    objStream.LoadFromFile ("C:\downloads\examplein.kml")
    strData = objStream.ReadText() [green]' read the whole file[/green]
    objStream.Close
    
    [green]' Now open for writing[/green]
    objStream.Open
    objStream.WriteText strData
    objStream.SaveToFile "C:\downloads\exampleout.kml", adSaveCreateOverWrite[/blue]

 
Hi Guitarzan

Thank you for your patience

I undestood your code. But my problem is beyond just checking wheter or not a tex file has a special character.

That is the real issue.

I have a kml which has the following line:

<description><![CDATA[<html><body><table border="1"><tr><th>Attribute</th><th>Value</th></tr><tr><td>INFORM</td><td>Ao Largo da Ilha da Convivência, São João da Barra</td></tr><tr><td>NINFOM</td><td>1948</td></tr><tr><td>NOBJNM</td><td>EXPEDICIONÁRIA</td></tr><tr><td>OBJNAM</td><td>...

My code is reading this line. when it reaches NOBJNM "EXPEDICIONÁRIA", the code converts the character "Á" to "Ã".

I need my code to read the right NOBJNM which is "EXPEDICIONÁRIA" with "Á" and not "Ã"

I wrote this piece of code just to show the characters. And when it reaches "Á" e shows "Ã". But that is not I want. I want the real one: "Á"

if Instr(ReadLineTextFile, ">NOBJNM<") <> 0 then
MyPosinicio = Instr(ReadLineTextFile,"<td>NOBJNM</td>")
posicao = mid(ReadLineTextFile, MyPosinicio + 19, len(ReadLineTextFile)-MyPosinicio + 15)
MyPosfim = Instr(posicao,"</td>")
'nome = mid(posicao, 1, MyPosfim - 1)
For i=1 to(MyPosfim-1)
MsgBox(mid(posicao,i,1))
Next

After reading from a file, how can I make the code store the right special character in a variable?

Best regards



 
Hi strongm

Thank you for your attention.

I tried creating the ADO stream, but I was not succssesful.

That's what I wrote before the ADO:

Const ForReading = 1, ForWriting = 2
'THESE TWO NEXT LINES READS A KML
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso_OpenTextFile("naufra.kml", ForReading)
'THESE TWO NEXT LINES WRITES ON NEW KML WHICH IS CREATED ON THE CODE
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile2 = fso_OpenTextFile("GE.kml", ForWriting, True)
'THESE TWO NEXT LINES WRITES ON A TEXT FILE WHICH IS CREATED ON THE CODE
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile3 = fso_OpenTextFile("SC.txt", ForWriting, True)

That´s the change after the ADO stream:

Const ForReading = 1, ForWriting = 2
'THESE TWO NEXT LINES READS A KML
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso_OpenTextFile("naufra.kml", ForReading)

'THESE TWO NEXT LINES WRITES ON NEW KML WHICH IS CREATED ON THE CODE
Set fso = CreateObject("ADODB.Stream")
fso.Charset = "utf-8"
Set MyFile2 = fso_OpenTextFile("GE.kml", ForWriting, True)

'THESE TWO NEXT LINES WRITES ON A TEXT FILE WHICH IS CREATED ON THE CODE
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile3 = fso_OpenTextFile("SC.txt", ForWriting, True)

I am having a erro.


 
Hi strongm

I need to go into the kml naufra and read the special characters from a piece of string.

Next, I need to store these characters in a variable and save in new kml GE, created on the code.

The code goes into kml naufra, reads line by line and find the NOBJNM which has special characters; stores in a variable and save it to GE kml.

Many Thanks


 
Nope. That's not going to work even if you were NOT getting an error. You are still trying to use the filesystemobject to read the file, thus it will get mangled.

And then you assume that the ADO stream object has the same methods as filesystemobject - which it does not. Hence the error.
And even if there wasn't an error you'd still not get a result, because your code doesn't actually write anything at all.

Here's your above code adapted for ADO stream:

Code:
[blue]    Const adSaveCreateOverWrite = 2
    Dim objStream, strData
    Set objStream = CreateObject("ADODB.Stream")
    objStream.Charset = "utf-8" [green]' select encoding stream will use
    
    ' Open stream (for reading)[/green]
    objStream.Open
    objStream.LoadFromFile "naufra.kml" [green]' load whole file into stream[/green]
    strData = objStream.ReadText() [green]' store contents of stream (currently the contents of our input file) into strData[/green]
    objStream.Close [green]' close and flush stream
    
    ' At this point strData contains the entire (unmangled) contents of our input file
    
    ' Open stream (for writing)[/green]
    objStream.Open
    objStream.WriteText strData [green]' Stick our string into the stream
    
    ' Write stream to KML file[/green]
    objStream.SaveToFile "GE.kml", adSaveCreateOverWrite

    [green]' Write stream to text file[/green]
    objStream.SaveToFile "SC.txt", adSaveCreateOverWrite
    
    objStream.Close [green]' close and flush stream[/green][/blue]




 
Hi strongm

How can I read line by line from the naufra.kml using a ADO stream? And How I can write a line from the naufra.kml?

I was using this:

'To read Lines:

ReadLineTextFile = MyFile.ReadLine

'To write Lines:

MyFile.WriteLine "VBScript".

 
Hi strongm

My code has three goals:

1 - Read line by line from naufra.kml (See MyFile in the code)

2 - Start writing a new KML file (Ge.kml) from header to footer. Ge.kml has SOME information read in naufra kml. (For that I need to read line by line from naufra.kml and write in Ge.kml (See Myfile2 in the code)

3 - Start writing a new TXT file (SC.txt) which has information from naufra.kml. (See MyFile3 in the code)

Code:
dim fso, MyFile, MyFile2, MyFile1
aspas = string(1,34)
Const ForReading = 1, ForWriting = 2
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.OpenTextFile("naufra.kml", ForReading)

Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile2 = fso.OpenTextFile("GE.kml", ForWriting, True)

Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile3 = fso.OpenTextFile("SC.txt", ForWriting, True)

cabecalho = "<?xml version=" & chr(34) &  "1.0" & chr(34) &  " encoding=" & chr(34) &  "UTF-8" & chr(34) &  "?>"
MyFile2.WriteLine cabecalho
cabecalho = "<kml xmlns=" & chr(34) &  "[URL unfurl="true"]http://www.opengis.net/kml/2.2"[/URL] & chr(34) &  " xmlns:gx=" & chr(34) &  "[URL unfurl="true"]http://www.google.com/kml/ext/2.2"[/URL] & chr(34) &  " xmlns:kml=" & chr(34) &  "[URL unfurl="true"]http://www.opengis.net/kml/2.2"[/URL] & chr(34) &  " xmlns:atom=" & chr(34) &  "[URL unfurl="true"]http://www.w3.org/2005/Atom"[/URL] & chr(34) &  ">"
MyFile2.WriteLine cabecalho
cabecalho = "<Document>"
MyFile2.WriteLine cabecalho
cabecalho = "<name>Naufragios</name>"
MyFile2.WriteLine cabecalho
cabecalho = "<atom:author><atom:name>Criado no CHM</atom:name></atom:author>"
MyFile2.WriteLine cabecalho
header ="Datum,WGS84,WGS84,0,0,0,0,0"	
MyFile3.WriteLine header
contador = 0
final = 0
do while contador<13
ReadLineTextFile = MyFile.ReadLine
.
.
.
Code:
MyFile2.Close
MyFile3.Close
Wscript.Echo "Done"
 
So hang on. Are you simply trying to parse out all the values of the NOBJNM attribute in the KML file (some of which - but not necessarily all - may have 'special' characters)? Or are you really looking for specific special characters?
 
We cross-posted ...

Personally, I'd use Microsoft's XML library to parse the KML file - but I don't think we've got time to work through that right now.

So, what I'd do would be to SPLIT the input, something like the following:

Code:
[blue]    Const adSaveCreateOverWrite = 2
    Dim objStream, strData
    Set objStream = CreateObject("ADODB.Stream")
    objStream.Charset = "utf-8" [green]' select encoding stream will use
    
    ' Open stream (for reading)[/green]
    objStream.Open
    objStream.LoadFromFile "naufra.kml" [green]' load whole file into stream[/green]
    strData = Split(objStream.ReadText(), vbLf) [green]' store contents of stream (currently the contents of our inpuit file) into array strData[/green]
    objStream.Close [green]' close and flush stream
    
    ' At this point array strData contains the entire contents of our input file with one line of text in each element
    
    ' Open stream (for writing)[/green]
    objStream.Open
    
    [green]' Take each line at a time[/green]
    For Each ReadLineTextFile In strData
        [green]' In this example we are just mirroring the input, but you might want to process them[/green]
        WriteLineTextFile = ReadLineTextFile
        objStream.WriteText WriteLineTextFile & vbLf [green]' add our desired output to the stream[/green]
    Next
    
    [green]' Write current stream contents to KML file[/green]
    objStream.SaveToFile "GE.kml", adSaveCreateOverWrite
    [green]' Write stream to text file[/green]
    objStream.SaveToFile "SC.txt", adSaveCreateOverWrite
    
    objStream.Close [green]' close and flush stream[/green][/blue]
 
Thank you all for the attention and great support to my questions.

Many thanks to
VulcanJedi
guitarzan
and
strongm,
you got the point of my problem and gave me the right tips.
Thank you very much for the patience and great support

"ADODB.Stream" That was it!!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top