Trying to load and parse some XML data:
<image name='image1' xsize='200' ysize='100'>
<solidcolor startxy='0,0' endxy='50,75' color='0072ff'>
<text startxy='50,50' endxy='150,70' color='000000' size='12'>text to print</text>
</image>
<image name='image2' xsize='200' ysize='100'>
<img src='background.jpg' startxy='0,0' endxy='200,100'>
<text startxy='50,50' endxy='150,70' color='000000' size='12'>text to print</text>
<text startxy='0,0' endxy='150,20' color='000000' size='12'></text>
</image>
The code below works, kind of, but I'm unable to get all the data like 'name', 'xsize', or any of the other data from between the <>. Also if there is nothing between the tags it will crash and if there is no closing tag it will crash. I'm new to JavaScript but HTML can do all this so I assume JavaScript can as well?
var x, i, xmlDoc;
var txt = "";
var text = "<book>" +
"<title size='1'>Everyday Italian</title>" +
"<text style='1'></text>" +
"<author>Giada De Laurentiis</author>" +
"<year>2005</year>" +
"</book>";
parser = new DOMParser();
xmlDoc = parser.parseFromString(text, "text/xml");
// documentElement always represents the root node
x = xmlDoc.documentElement.childNodes;
for (i = 0; i < x.length; i++) {
txt += x[i].nodeName + ": " + x[i].childNodes[0].nodeValue + "<br>";
}
document.getElementById("demo").innerHTML = txt;
<p id="demo"></p>