I'm trying to read a user XML file (a sitemap.xml file) like this:
<?xml version="1.0" encoding="UTF-8"?>
<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<url>
<loc>http://www.myhost.com/</loc>
<lastmod>2016-09-08T13:50:33+00:00</lastmod>
</url>
</urlset>
How can I read the XML parsed content in a structured/programatic way?
Tried 2 ways using DomParser (XmlDocument) and DOM interpreter (HtmlElement).
<input type="file" onchange="readfile(this)" >
<script>
function readfile(fileinputobj){
var reader = new FileReader();
reader.onload = function(e) {
alert(reader.result);
//Using DOMParser to get XMLDocument
var parser = new DOMParser(),
xmlDoc = parser.parseFromString(reader.result, "text/xml");
if (xmlDoc.documentElement){
console.log(xmlDoc.documentElement);
//xmlDoc.documentElement.nodeName == 'urlset'
}
//Using DOM interpreter to get HtmlDocument
var doc=document.createElement("div");
doc.innerHTML=reader.result;
if (doc.querySelector('urlset')){
console.log(doc.querySelector('urlset'));
//doc.querySelector('urlset').tagName == 'urlset'
}
}
reader.readAsText(fileinputobj.files[0]);
}
</script>
What is the best, secure and cross-browser way to do this, and traverse the XML document to query attributes and content?
EDIT: Searching for more info i've found solution for cross-browser compatibility in https://www.w3schools.com/Xml/xml_parser.asp and XML parsing of a variable string in JavaScript
It seems that XmlDocument is compatible with almost any well-known browser using DomParser
,or ActiveXObject("Microsoft.XMLDOM")
in IE6-IE8 .
For XML browsing i've found tipically using xmlElem=xmldoc.getElementById()
for selecting tags , xmlElem.childNodes
to get internal nodes, and xmlElem.getAttribute()
to read tag attributes. It's that ok?