Page 1 of 4 123 ... LastLast
Results 1 to 10 of 32

Thread: Getting a value from a text file

  1. #1
    Join Date
    Nov 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Getting a value from a text file

    Hi
    I have a text file (values.txt) that contains information in the form of

    A1=99
    A2=3
    A3=255

    A454=23

    I was wondering if there was a script that I can have on my html page that returns the value of the variable pointed to.

    Eg:
    A1 = getvalue(A1)

    Regards
    Craig

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Here is a little demo you might find interesting. There may be more efficient methods and I am not sure how this will react with large data files, hopefully someone with a clue on that will contribute to this thread. Also, since you say you already have this big file with the values in it, you could test it out on that and see how much lag time there is with a large file being parsed for individual values. For the purposes of this demo, I created data.txt and get_data.htm -

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

    A1=99
    A2=3
    A3=255

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

    get_data.htm
    ------------------------
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
    <html>
    <head>
    <title>Get Data - Demo</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    function getFile(filename)
      { oxmlhttp = null;
        try
          { oxmlhttp = new XMLHttpRequest();
            oxmlhttp.overrideMimeType("text/xml");
          }
        catch(e)
          { try
              { oxmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
              }
            catch(e)
              { return null;
              }
          }
        if(!oxmlhttp) return null;
        try
          { oxmlhttp.open("GET",filename,false);
            oxmlhttp.send(null);
          }
        catch(e)
          { return null;
          }
        return oxmlhttp.responseText;
      }
    
    function getData(file, param){
    return getFile(file).split(param+'=', 2)[1].split('\n', 1)[0]
    }
    
    </script>
    </head>
    <body>
    <input type="button" onclick="alert(getData('data.txt', 'A1'))" value="A1"><br>
    <input type="button" onclick="alert(getData('data.txt', 'A2'))" value="A2"><br>
    <input type="button" onclick="alert(getData('data.txt', 'A3'))" value="A3"><br>
    </body>
    </html>
    Last edited by jscheuer1; 12-14-2005 at 06:26 AM.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    The function getData(file, param) might be more efficient (I'm thinking about 'on a large data file', again) like so:

    Code:
    function getData(file, param){
    var re = new RegExp('\n|.*'+param+'=(.*)$|.*', 'gm')
    return getFile(file).replace(re, '$1')
    }
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
            catch(e)
              { return null;
              }
          }
        if(!oxmlhttp) return null;
    You're repeating yourself :-\
    That second "return null" will never be reached; if the object can't be created, you've already returned null in the catch block above.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by Twey
    That second "return null" will never be reached; if the object can't be created, you've already returned null in the catch block above.
    If true, it will harm nothing. I didn't write the function that part of the code belongs to and I do not fully understand it. As a result, I cannot be certain if that bit may or may not be needed. Perhaps it is a fallback for things like a 404 or 403 response.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Ah, I see. It would be necessary were it in a seperate function.
    Code:
    function getFile(filename) {
      oxmlhttp = null;
      try {
        oxmlhttp = new XMLHttpRequest();
        oxmlhttp.overrideMimeType("text/xml");
      } catch(e) {
        try {
          oxmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) { return null; }
      }
      try {
        oxmlhttp.open("GET",filename,false);
        oxmlhttp.send(null);
      } catch(e) { return null; }
      return oxmlhttp.responseText;
    }
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I was just testing out the getFile function and, as I believe Mike pointed out in an earlier thread, it doesn't handle a 404 too well. Also I couldn't get it to reach that part in the code that you (Twey) say is redundant. Nevertheless, it is serviceable for the purpose I used it for.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    function getFile(filename) {
      oxmlhttp = null;
      try {
        oxmlhttp = new XMLHttpRequest();
        oxmlhttp.overrideMimeType("text/xml");
      } catch(e) {
        try {
          oxmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) { return null; }
      }
      try {
        oxmlhttp.open("GET",filename,false);
        if(oxmlhttp.status == 404) {
          window.alert("URL doesn't exist!  Please contact the webmaster.");
          return null;
        } else if((oxmlhttp.status != 200 /*Request OK*/) && (oxmlhttp.status != 304 /*Not modified, don't know if this will be needed, but better safe than sorry*/)) {
          window.alert("An error " + oxmlhttp.status + " has occurred!  Please alert the webmaster.");
          return null;
        }
        oxmlhttp.send(null);
      } catch(e) { return null; }
      return oxmlhttp.responseText;
    }
    Quote Originally Posted by John
    it doesn't handle a 404 too well.
    Now she does
    Last edited by Twey; 12-14-2005 at 07:26 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Nov 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I would like to thank all of you that help with the code here.
    Your code works like a treat.

    Thanks again.
    Regards
    CA

  10. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I decided that I didn't like this whole:

    XMLHttpRequest()/ActiveXObject("Msxml2.XMLHTTP")

    method because Opera didn't seem to like it. I went ahead and created an entirely different approach, using an iframe. I will test out Twey's neat looking code soon but, I am not too optimistic about its working in Opera. Here is my latest:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
    <html>
    <head>
    <title>Get Data Iframe- Demo</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    
    /*Get Data Iframe script- © John Davenport Scheuer
      as first seen in Dynamic Drive Forums
      http://www.dynamicdrive.com/forums
      user name: jscheuer1
      */
    
    //set source for data file.  Use a plain text file with one name=value pair per line.
    var datasrc='data.txt'
    
    ///////////////No Need to Edit Below/////////////////
    if(document.getElementById)
    document.write('<iframe name="source" id="data" src="'+datasrc+'" style="position:absolute;top:-1000px;left:-1000px;visibility:hidden;"></iframe>')
    
    function getData(param){
    if(!document.getElementById)
    return;
    var frameObj=document.getElementById('data');
    // Local Error Check (IE and Mozilla):
    if (typeof source.location.href!=='string'||source.location.href.indexOf(datasrc)==-1){
    alert('file: "'+datasrc+'" Unavailable')
    return false;
    }
    var dataSet=frameObj.contentDocument? frameObj.contentDocument.body : frameObj.Document.body
    dataSet=dataSet.innerHTML
    var re = new RegExp(param+' *=', 'gm')
    // Remote Error Check (IE, Mozilla and Opera) (Opera Local Error Check, as well)
    if (!re.test(dataSet)){
    alert('file: "'+datasrc+'" Unavailable\nor\nvariable: "'+param+'" Undefined')
    return false;
    }
    re=new RegExp('\r|\n|.*'+param+' *=(.*)$|.*', 'gm')
    return dataSet.replace(re,'$1').replace(/\r|<\/[XMPpRrEe]*>/, '');
    }
    
    </script>
    </head>
    <body>
    <input type="button" onclick="alert(getData('A1'))" value="A1"><br>
    <input type="button" onclick="alert(getData('A2'))" value="A2"><br>
    <input type="button" onclick="alert(getData('A3'))" value="A3"><br>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •