2

This should be really simple but for some reason I can't seem to get it to work; So I have a XML file as follows:

<board>
     <version>1</version>
<r>
    <c>
        <tile>g</tile>
    </c>

    <c>
        <tile>B</tile>
    </c>
</r>

<r>
    <c>
        <tile>C</tile>
    </c>

    <c>
        <tile>D</tile>
    </c>
</r>
</board>

And some JavaScript like:

    function get_cversion(){
    if (window.XMLHttpRequest) { 
        xmlhttp = new XMLHttpRequest(); 
    } 
    else { 
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlhttp.open("GET", "board.XML", false); 
    xmlhttp.send(); 
    xmlDoc = xmlhttp.responseXML;
    var mytext = 0


    var x=xmlDoc.getElementsByTagName("version");
    mytext = (x[0].childNodes[0].nodeValue);
    mytext += "";
    document.frmOne.input1.value = ""+mytext;
}

and last, I have a form on the page like this:

<FORM NAME = frmOne>

    1val: <INPUT TYPE = Text NAME = input1 SIZE = 4 value ="">
    <p>
    <Input Type = Button NAME = b1 VALUE = "Save val" onClick = update_XX()>
    <p> 
    <Input Type = Button NAME = b2 VALUE = "WOOOOO" onClick = get_cversion()>
</FORM>

I'm still really new at this whole XML game. I know I must be missing something really obvious but I just cant see it, any help would be greatly appreciated.

Thanks.

2
  • Is it a paste error or are you really missing </board> Commented May 12, 2011 at 15:12
  • This is enough - assuming that really was your code: document.frmOne.input1.value = (x[0].childNodes[0].nodeValue); Commented May 12, 2011 at 17:21

1 Answer 1

3

You might want to check the status of the request before processing the XML. Your code should look something like this,

    function get_cversion(){
    if (window.XMLHttpRequest) { 
        xmlhttp = new XMLHttpRequest(); 
    } 
    else { 
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlhttp.open("GET", "board.XML", false); 
    xmlhttp.send();                   
         if(xmlhttp.status == 200) {
          var xmlDoc = xmlhttp.responseXML;
          var mytext = 0       
          var x=xmlDoc.getElementsByTagName("version");
          mytext = (x[0].childNodes[0].nodeValue);
          mytext += "";
          document.frmOne.input1.value = ""+mytext;            
      }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Right, so I've added the if and then an else document.frmOne.input1.value = "whoops"; and it is going to the whoops...any ideas why?
BECAUSE IM AN IDIOT: board.xml != board.XML . Sorted. Thanks for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.