Solved

Refering to a txt file, then display in text box

Posted on 2004-04-24
22
414 Views
Last Modified: 2010-04-17
Hi.

I'm after some help please.

I am designing a program which on clicking a Commandbutton, imports data from an XML file (not my own created XML file) into text boxes.

But have come across a problem..........

Some of the data in the XML is not a straight forward description e.g. "Mrs" instead the data is set as a numeric value.........therefore I need to code my program so when it imports the XML file and comes across a numeric value to look at a text file for the specific description. (I am thinking a text file is the best way to do it as then if the descriptions were to change in the future it'll be easier to maintain)

Example of the numeric value in the XML and their relevant description:

1 would = Mr
2 would = Mrs
3 would = Ms
4 would = Dr

Thanks in advance


Ross.
0
Comment
Question by:rosswiggins
  • 12
  • 7
  • 2
  • +1
22 Comments
 
LVL 15

Expert Comment

by:JakobA
ID: 10909105
which languages are you using ?   The XML format can be used with practically all modern languages from HTML to Java
0
 
LVL 6

Expert Comment

by:danths
ID: 10909942
Use an XSL to transform the XML from one format to the other. Its easy to learn, atleast 1/2 as powerful as any programming language and easy to maintain.
0
 

Author Comment

by:rosswiggins
ID: 10911293
Sorry forgot to add the vital part - I'm using VB 6.
0
 
LVL 6

Expert Comment

by:danths
ID: 10911630
You should be easily be able to do xsl transformation, the so called text file that is easier to maintain as specified in your original post could be this XSL file, Xsl will provide you with greater flexibility in doing other stuff as well.
However you need to be realistic in weighing the chances of something like that being changed. Further more Iam not sure why you are just importing the XML into a text file into a textbox /area? Just curious since in my 4+ yrs exp. in XML, I never came across a regular user interested in looking at the contents of a  XML file....
0
 

Author Comment

by:rosswiggins
ID: 10913036
Like i said in the first post it's not my XML that i need to import.  I'm creating a program for my work so that it helps in the day to day problems etc.

Unfortuatley the XML I am importing is not as straight forward as I hoped, meaning some of the descriptions are not contained, instead a numeric value is used which refers to a specific description.

It's these descriptions I need.  Originally i thought about a Select Case statement but because some of the data that is representeded as a numeric value is likely to be amended in the future I thought a text file is the best way for me.

Ross
0
 
LVL 3

Expert Comment

by:Tele_tech
ID: 10936090
I can give you an overview of what you need.  if you want specific code.. let me know and I will post.

end your text file with and (END) to act as flag
set up a global text array the size of which woul be larger than any future growth

set up a sub()
  if will open the txt file
  set a counter to 1
   with a while loop set to look for the (END) statement
       read the lines from the text file
       associate the description to the counter
when done end the sub  the array will have your descriptions and the value associated to them

You would only need to call this sub at startup and the values would remain for the duration of the run...
that should get it for you......
I used the same trick for loading error msg's in QBASIC for use in error control and reporting oh so long ago.. :)  the good ole days...... hehehe
0
 
LVL 3

Expert Comment

by:Tele_tech
ID: 10936108
by the way  if the values are not 1,2,3,4,..... but have gaps.... 1,5,9,22,.... you can still use this.. but you need a double dimension array and some string parsing to make it work....
0
 

Author Comment

by:rosswiggins
ID: 10943095
Thanks alot for the explanation.

If you could show me the code that would be great :)

0
 
LVL 3

Expert Comment

by:Tele_tech
ID: 10952356
could you set the file so it could be accessed?  so I could look at the coding that would be needed.. even a partital as long as it contains enough of the formating and differences

The fields and organization of the file will affect the way it is written
0
 
LVL 3

Expert Comment

by:Tele_tech
ID: 10952372
I will post the code to just set up the arrary later today / this evening.
0
 
LVL 3

Expert Comment

by:Tele_tech
ID: 10965954
I'm sorry something un-expected came up and has used my time.  I will endeaver to get the code up.... (major project on the paying job due by this weekend..... )
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:rosswiggins
ID: 10966460
Thats ok no problem.

What do you mean by "Could you set the file so it could be accessed?"

Do you mean sending a copy of the XML I would be using?

If so, give me your email address and I'll send asap

Thanks
0
 
LVL 3

Expert Comment

by:Tele_tech
ID: 10968376
Aspect_tech@Phreaker.net

0
 
LVL 3

Expert Comment

by:Tele_tech
ID: 10971306
I typed this up fast.. did not really check syntax as I don't know what language you are going to do this in
but this is the theory of the load and use...
based on what you sent me, I showed one of the ways to cover the basis of multible types of information you mentioned.

Let me know if you need any clarification....

Here is the code to load multible types of data from text files to key off certain values
this loads it into an array that can be accessed on the fly
it could also be done to scan the text file each time, but that would be much slower
if there are no overlaps or duplicates in the codes for the separate types you could skip the file$()
array and reduce the information() array to information$(number_of_codes,2)
and place everthing in 1 text file
-----------------------------------------------------------------------------
file=4 ; number of different txt files, salutation,car info,misc
number_of_codes=6 ;number of codes for this,  this value has to be as high as the largest file
;
;files cardata.txt, gendrdata.txt

global Information$(file,type_code,2)
global file$(types)


sub loaddata()

; load the names of the data files
numberfile=1
;filedata.txt
;Car
;salution
;value


open "filedata.txt" for input as 1
             filenumber=1
      while not eof(1)
        input #1,file$(filenumber)
        filenumber=filenumber+1
      wend
close 1
;file$() now contains the names of all the information text files

for filenumber = 1 to numberfiles
      open file$(filenumber) for input as 1      ;open the information text files 1 at a time
        code=1 ;start with first code in file
          while not eof(1)
            ;load information string with data
            ;information$(1,1,1) = "001"
            ;information$(1,1,2) = "Mr."
            ;filenumber is the type of data..  car type, value, etc.
            ;code is the value in the field in the xml file that you are looking up in the text file
            ;the third position descripton
            ;file$(1) ="cardata.txt"
            ;cardata.txt
            ;004,chevy
            ;006,ford

            input #1,information$(filenumber,code,1),information$(filenumber,code,2)
            ;now information$(1,1,1) = 004
            ;    information$(1,1,2) = chevy
            ;get ready for next code
            code=code+1
            wend
        ;close 1
      ;this text file fully loaded
      ;get next if there is one
next filenumber

end sub()

--------------------------------------------------------------------------------------------------------------


In use you would set the value of filenumber to the type of information you are looking for

filenumber = 1      ;car data

set find_code$ to the value you read from the xml file

then scan for it

for code=1 to number_codes
      if find_code$=information$(filenumber,code,1) then
            what_I_need$=information$(filenumber,code,2)
            ;what_I_need$ would contain chevy based on above example
            code=number_codes+1       ;force end of for next loop
      end if
next code

0
 

Author Comment

by:rosswiggins
ID: 10973321
Thanks for the code

I'm using VB 6.0.

I was thinking of having separate text files for each one (salutation, vehicle model etc).

Sorry to be a pain I'm pretty new to programming so are you able to show me the code for VB 6.0

If not I'll do my best to use the guidance above

Thanks
0
 
LVL 3

Expert Comment

by:Tele_tech
ID: 10973496
for the most part what I have put in will work for vb 6  may be a couple of places the syntax needs clean up....

It is set up for seperate files, so that should work good for you.  I will try and paste this up in VB 6 later this evening and test it...

In the mean time... go ahead start type this in...If you have a specific problem with it let me know....

0
 
LVL 3

Accepted Solution

by:
Tele_tech earned 100 total points
ID: 10974593
Here is code tested in VB 6.  I have not created any forms or interface.. for testing it prints in the immediate mode
notice in the main loop you can change the value of look_type and you can use the same code in many files.
the files I created to test this are listed at the bottom. Since you said you already had some work done on the xml portion.. here is the piece to read from the text files and get info based on values.   If you have any more questions let me know
I made minor changes to the code above..... removed the ;  rem statements... the flow is the same...
also changed the comparison in the lookup loop to use an instr instead of = .  the , in the text file is read with white spaces behind the actual item.. so instr will extract it better.
you could also use
     if find_code$=left$(information$(filenumber,code,1),3) then
but what if your code is 4 digits?  the instr is more flexable



Rem THE VALUES IN THE NEXT STATEMENTS ARE DEPENDENT ON HOW YOU USE THIS
Global information$(6, 20, 2)
Global file$(6)
Global what_I_need$
Global Number_Codes
Rem

Sub Main()
    Set fMainForm = New frmMain
    fMainForm.Show
    ChDir "c:\data"
    Call loaddata
    Rem Look Type and Find$ would be from routines dealing with your xml
    car = 1
    salutation = 3
    look_type = car
    find$ = "001"
    Call srch(look_type, find$)
    Debug.Print ">"; what_I_need$; "<"
    End
End Sub


Sub loaddata()
FileNumber = 1
Open "filedata.txt" For Input As 1
    While Not EOF(1)
      Input #1, file$(FileNumber)
      numberfiles = FileNumber
      FileNumber = FileNumber + 1
    Wend
Close 1
Rem file$() now contains all the information text files

For FileNumber = 1 To numberfiles
    Open file$(FileNumber) For Input As 1
    code = 1
    While Not EOF(1)
        Input #1, information$(FileNumber, code, 1), information$(FileNumber, code, 2)
        Rem Debug.Print information$(FileNumber, code, 1), information$(FileNumber, code, 2)
        code = code + 1
        If code > Number_Codes Then Number_Codes = code
    Wend
    Close 1
    Next FileNumber

End Sub

Sub srch(lktype, Find_code$)

For code = 1 To Number_Codes
    Rem Debug.Print code, Find_code$, information$(lktype, code, 2)
   
    If InStr(Find_code$, information$(lktype, code, 1)) Then
        Debug.Print information$(lktype, code, 2)
        what_I_need$ = information$(lktype, code, 2)
        code = Number_Codes + 1: Rem force end of for next loop
    End If
Next code

End Sub

filedata.txt
-------------------------
cardata.txt
data.txt
gendrdata.txt
-------------------------


cardata.txt
---------------------------
001,Chevy Nova
003,Ford Taurus
004,Dodge Caravan
010,Ford Windstar
011,Ford F150
015,Dodge Ram
016,Dodge Viper
020,Chrysler Leberon
021,Dodge Caravan
030,German Tank
---------------------------------------

data.txt
----------------------------------
----------------------------------

gendrdata.txt
--------------------------
001,Mr.
002,Mrs.
003,Miss
004,DR.
--------------------------


0
 

Author Comment

by:rosswiggins
ID: 11054734
Got another problem, using the following code it always will pick up the last line of data in my text file rather then the specific numeric value:

            Set oTitle = xmlDoc.selectSingleNode("//ProposerPolicyholder_TitleCode/@Val")
            oTitle = myTitle
            Open "C:\TitleCode.txt" For Input As #1
            Do Until EOF(1)
                Line Input #1, myTitle
            Loop
            Close #1
            txtTitle.Text = myTitle

This returns: 007 = Mrs

Here is a copy of my text file:

            004 = Mrs
            005 = Mr
            007 = Ms

Also I have tried using the Left, Right, Mid functions to return only the text rather then the numeric value and the text and I can't seem to get that to work!

Any idea's on how to resolve both problems??

Thanks
0
 
LVL 3

Expert Comment

by:Tele_tech
ID: 11054888
what value are you looking for?  I don't see the value.
lets say you put the value in varible called findvalue
You have to select the one you want while reading the file or
load an array and have it for the compares.

findvalue=004

            Do Until EOF(1)
                Line Input #1, myTitle
                tst$=left$(myTitle,3)
                if tst$=findvalue then txtTitle.txt=mid$(mytitle,7)
            loop

when you leave the loop the value of txtTitle.txt should be Mrs.
the other way is to create the text file as in my example....
004,Mrs
005,Mr
007,ms

then do it like this
(this is the way I would do it if my job
findvalue=004

            Do Until EOF(1)
                Line Input #1,tst$,mytitle
                if tst$=findvalue then txtTitle.txt=myTitle
            loop

Let me know if ya need more
0
 

Author Comment

by:rosswiggins
ID: 11088930
It's the description I am after not the numeric value.

Using the code I provided above, it displays the whole line from the text file depending on the numeric value in the XML, but I only want the description to be viewed in my text box.

0
 
LVL 3

Expert Comment

by:Tele_tech
ID: 11094684
I understand, in the reference I posted. you use the number to look it up. and then assign the text to mytitle

you have to use the number, since that is what you have as the search item.  then when you find it.. grap the field that contains the description.  but you have to do it in the loop.

If you want to use the code as you have it here it is with the changes to make it work.


            Set oTitle = xmlDoc.selectSingleNode("//ProposerPolicyholder_TitleCode/@Val")   ; Is oTitle the value from the XML b?
            oTitle = myTitle    ; not sure why after getting the oTitle value from the file you reset it ?
            value_to_search_for$=               ;place the numeric value you are looking for in here as a string
            Open "C:\TitleCode.txt" For Input As #1
            Do Until EOF(1)
                Line Input #1, myTitle
                in_val$=left$(myTitle,3)
                if in_val$=value_to_search_for$ then
                               txtTitle.Text =mid$(myTitle,7)   ; Assign this when you find it in the file
                end if
            Loop
            Close #1
            txtTitle.Text = myTitle   ; If you assign the value here what you will get is the last value in the file..

This returns: 007 = Mrs        ; not really sure how this can return this the 007 is the last value and Mrs is the first... and you are only assigning 1 value... it should be with the code as you have it

                   007 = Ms

Here is a copy of my text file:

            004 = Mrs
            005 = Mr
            007 = Ms
0
 
LVL 3

Expert Comment

by:Tele_tech
ID: 11094736
note:
  Out of habit I went back to an = sign... you could substitue this in the loop

Take out:
===============================================
                in_val$=left$(myTitle,3)
                if in_val$=value_to_search_for$ then
                               txtTitle.Text =mid$(myTitle,7)   ; Assign this when you find it in the file
                end if
================================================

Replace with
======================================================
               if instr(value_to_search_for$,in_val$) then txtTitle.Text =mid$(myTitle,7)  
======================================================

if your not sure why this will work... do a pencil walk through.. print your section of code for the search .. write the varibles on a sheet and as you read each line of code write down what the varible with be when that line is finish.....
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

A short article about problems I had with the new location API and permissions in Marshmallow
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now