How to XML

By Jenny Wang on February 26, 2017
~3 min. read

.xml FILE STRUCTURE

<?xml version="1.0" encoding="UTF-8"?>
<firstThing>
    <second attribute="this">actual thing</second>
    <yeah lang="en">alsdkjfa</yeah>
</firstThing>

see https://www.w3schools.com/xml/dom_nodes.asp

HOW TO ACCESS

setup

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        myFunction(this);
    }
};
xhttp.open("GET", "books.xml", true);
xhttp.send();

get elements

var xmlDoc = xml.responseXML;
var x = xmlDoc.getElementsByTagName("title");

get value

x[3].childNodes[0].nodeValue;
    ^^ 4th child of doc + the 1st child of that element's value/text

OBJECT DESCRIPTION

XMLHttpRequest
    .open(requestType, url, asynchronous/synchronous)
        requestType- "GET" or "POST"
        url- "http://aldkjfalsd" or "file.xml"
        last boolean- true = asyncrhonous (one thing at a time)
            false = synchronous (all at once/real time)
    .send()
        send the request
    .onreadystatechange (event)
        this.
            readyState
                0- request not intialized
                1- server connection established
                2- request recieved
                3- processing request
                4- request finished and response ready
            status
                200- ok
                404- page not found
Tags: coding_notes