Link to home
Start Free TrialLog in
Avatar of rosswiggins
rosswiggins

asked on

Refering to a txt file, then display in text box

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.
Avatar of JakobA
JakobA

which languages are you using ?   The XML format can be used with practically all modern languages from HTML to Java
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.
Avatar of rosswiggins

ASKER

Sorry forgot to add the vital part - I'm using VB 6.
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....
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
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
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....
Thanks alot for the explanation.

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

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
I will post the code to just set up the arrary later today / this evening.
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..... )
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
Aspect_tech@Phreaker.net

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

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
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....

ASKER CERTIFIED SOLUTION
Avatar of Tele_tech
Tele_tech

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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
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.

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
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.....