1

xml file

<?xml version="1.0" encoding="UTF-8"?>
<checkers>
    <data>0</data>
    <text>Blank Checker/White square</text>
</checkers>
<checkers>
    <data>3</data>
    <text>black square blank></text>
</checkers>
<checkers>
    <data>0</data>
    <text>Blank Checker/White square</text>
</checkers>

All I want to do is parse out the data and just get a array/list like this:

[0,3,0] from the XML file using HTTPrequest and use it in my javascript code.

Can someone put me in the right direction/send some learning help to me? Thank you.

function loadXMLDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      this.responseText;
    }
  };
  xhttp.open("GET", "data.xml", true);
  xhttp.send();
  console.log(xhttp)


var grid = [0,3,0]

end result needed

1 Answer 1

1

XML needs to have one root element. Then you can parse it using DOMParser:

const someXML = `<?xml version="1.0" encoding="UTF-8"?>
    <document>
        <checkers>
            <data>0</data>
            <text>Blank Checker/White square</text>
        </checkers>
        <checkers>
            <data>3</data>
            <text>black square blank></text>
        </checkers>
        <checkers>
            <data>0</data>
            <text>Blank Checker/White square</text>
        </checkers>
    </document>`

const parser = new DOMParser()
const parsedXML = parser.parseFromString(someXML, 'text/xml')

const nodes = parsedXML.querySelectorAll('checkers')

const myArray = []
nodes.forEach(node => myArray.push(node.children[0].textContent))
Sign up to request clarification or add additional context in comments.

Comments

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.