Extract XML document data from XMLHtpRequest



  • There is a function that bluntly takes the whole page in the form of:

    function handleServerResponse() {
    var response = xmlHttp.responseText;
    myDiv = document.getElementById('myDivElement');
    var VRegExp = new RegExp(/<pre class="data">.*<\/pre>/);
    var VResult = response.match(VRegExp); 
    myDiv.innerHTML = VResult;
    }
    

    How do you make it from the response to then get a specific div value that belongs to a certain class and put it on the page?



  • There are two options.

    The first is by regular expression. This option would not work if the block (e.g. div) would be fitted with the same current (div).

    function handleServerResponse() {
        var response = xmlHttp.responseText;
        myDiv = document.getElementById('myDivElement');
        var VRegExp = new RegExp(/<div.* class="test".*>.*<\/div>/);//регулярное выражение
        var VResult = response.match(VRegExp);//поиск строки
        myDiv.innerHTML = VResult;
    }
    

    The second is to put everything in an invisible block (display:none) and then find the necessary block through the DOM and put the meanings where it takes.

    Second option:

    function handleServerResponse() {
        var response = xmlHttp.responseText;
        myDiv = document.getElementById('myDivElement');
        myDiv.innerHTML = "<div style='display:none'>"+response+"</div>";
        myDiv.innerHTML = myDiv.querySelector("pre.data").innerHTML;
    }
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2