Loading XML config file for web page

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TMN

    Loading XML config file for web page

    Hi All

    If I want to use an xml file to hold some config parameters for a web
    page do I need to load it with JS
    or is there a way of including it in the <head?

    Thanks
    Tim
  • Martin Honnen

    #2
    Re: Loading XML config file for web page

    TMN wrote:
    If I want to use an xml file to hold some config parameters for a web
    page do I need to load it with JS
    Yes, use XMLHttpRequest for best cross-browser support to load the file.
    or is there a way of including it in the <head?
    Only IE has a concept of a so called XML data island where you can load
    XML data inside of a HTML document using an 'xml' element e.g.
    <xml id="x1" src="file.xml"> </xml>
    But that is not supported by other browsers.


    --

    Martin Honnen

    Comment

    • The Magpie

      #3
      Re: Loading XML config file for web page

      TMN wrote:
      Hi All
      >
      If I want to use an xml file to hold some config parameters for a web
      page do I need to load it with JS
      or is there a way of including it in the <head?
      >
      Use XMLHttpRequest to collect the data.

      Comment

      • TMN

        #4
        Re: Loading XML config file for web page

        On May 14, 6:33 pm, The Magpie <use...@pigsins pace.co.ukwrote :
        TMN wrote:
        Hi All
        >
        If I want to use an xml file to hold some config parameters for a web
        page do I need to load it with JS
        or is there a way of including it in the <head?
        >
        Use XMLHttpRequest to collect the data.
        Thanks - It just seems something like <xml id="x1" src="file.xml"</
        xmlis so natural for a browser !!!

        regards
        Tim

        Comment

        • joe

          #5
          Re: Loading XML config file for web page

          TMN <nash.rsa@gmail .comwrote:
          >Hi All
          >
          >If I want to use an xml file to hold some config parameters for a web
          >page do I need to load it with JS
          >or is there a way of including it in the <head?
          >
          >Thanks
          >Tim

          Here a sample how to load a page with Javascript.
          Be adivsed that it returns immediately. So if you use it to intialize variables
          do them in "whatever" function.

          Here' what I mean (pseudocode):

          line 0: ..
          line 1: blah, blah;
          line 2: loadXMLDoc(..); //loads a value to "myvar"
          line 3: if (myvar==1)
          line 4: blah, blah;
          line 5: ...

          When execution of code reaches Line 4 you'd expect "myvar" to have some value.
          Not so! Caveat! "whatever" is executed (see below example code) when it's good
          and ready. loadXMLDoc(..) returns immediately (well almost).









          loadXMLDoc("htt p://www.yourhomepag e.com/somefile.txt");



          function loadXMLDoc(url)
          {
          xmlhttp=null;
          if (window.XMLHttp Request)
          {// code for IE7, Firefox, Opera, etc.
          xmlhttp=new XMLHttpRequest( );
          }
          else if (window.ActiveX Object)
          {// code for IE6, IE5
          xmlhttp=new ActiveXObject(" Microsoft.XMLHT TP");
          }
          if (xmlhttp!=null)
          {
          xmlhttp.onready statechange=wha tever;
          xmlhttp.open("G ET",url,true) ;
          xmlhttp.send(nu ll);
          }
          else
          {
          alert("Your browser does not support it.");
          }
          }

          function whatever()
          {
          if (xmlhttp.readyS tate==4)
          {// 4 = "loaded"
          if (xmlhttp.status ==200)
          {// 200 = "OK"
          myvar=1; //remove this line, not necessary
          alert("It worked, here's the contents:" + xmlhttp.respons eText);
          }
          else if (xmlhttp.status ==404)
          {
          alert("Dang! no workee:" + xmlhttp.respons eText);
          }
          else
          {
          alert("Som uther problemo:" + xmlhttp.respons eText);
          }
          }
          }


          Comment

          • Tim Nash (aka TMN)

            #6
            Re: Loading XML config file for web page

            On May 15, 7:53 am, joe <m...@invalid.c omwrote:
            TMN <nash....@gmail .comwrote:
            Hi All
            >
            If I want to use an xml file to hold some config parameters for a web
            page do I need to load it with JS
            or is there a way of including it in the <head?
            >
            Thanks
            Tim
            >
            Here a sample how to load a page with Javascript.
            Be adivsed that it returns immediately. So if you use it to intialize variables
            do them in "whatever" function.
            >
            Here' what I mean (pseudocode):
            >
            line 0: ..
            line 1: blah, blah;
            line 2: loadXMLDoc(..); //loads a value to "myvar"
            line 3: if (myvar==1)
            line 4: blah, blah;
            line 5: ...
            >
            When execution of code reaches Line 4 you'd expect "myvar" to have some value.
            Not so! Caveat! "whatever" is executed (see below example code) when it's good
            and ready. loadXMLDoc(..) returns immediately (well almost).
            >
            loadXMLDoc("htt p://www.yourhomepag e.com/somefile.txt");
            >
            function loadXMLDoc(url)
            {
            xmlhttp=null;
            if (window.XMLHttp Request)
            {// code for IE7, Firefox, Opera, etc.
            xmlhttp=new XMLHttpRequest( );
            }
            else if (window.ActiveX Object)
            {// code for IE6, IE5
            xmlhttp=new ActiveXObject(" Microsoft.XMLHT TP");
            }
            if (xmlhttp!=null)
            {
            xmlhttp.onready statechange=wha tever;
            xmlhttp.open("G ET",url,true) ;
            xmlhttp.send(nu ll);
            }
            else
            {
            alert("Your browser does not support it.");
            }
            >
            }
            >
            function whatever()
            {
            if (xmlhttp.readyS tate==4)
            {// 4 = "loaded"
            if (xmlhttp.status ==200)
            {// 200 = "OK"
            myvar=1; //remove this line, not necessary
            alert("It worked, here's the contents:" + xmlhttp.respons eText);
            }
            else if (xmlhttp.status ==404)
            {
            alert("Dang! no workee:" + xmlhttp.respons eText);
            }
            else
            {
            alert("Som uther problemo:" + xmlhttp.respons eText);
            }
            }
            >
            }
            Thanks to all who responded.

            Tim
            South Africa

            Comment

            Working...