我正在尝试加载本地XML文件并解析它。为此,我尝试编写代码并在此处进行操作。请有人帮助我继续进一步
function xmlSet() {
var files = document.getElementById('files').files;
if (!files.length) {
alert('Please select a file!');
return;
}
var start = 0;
var stop = file.size - 1;
var str = "";
var reader = new FileReader();
reader.onloadend = function(evt) {
str = //Total XML string
};
var xml = $.parseXML(str);
xml.find('*').each(function() {
// XML parsing code
});
}
答案 0 :(得分:0)
使用DOMParser API解析所选文件:
function readXML()
{
var fileString = arguments[0].target.result;
with (new DOMParser)
{
var xmlDOM = parseFromString(fileString, "text/xml");
console.log(xmlDOM.documentElement.nodeName);
}
}
with (new FileReader)
{
var files = document.getElementById('files').files[0];
onloadend = readXML;
readAsText(files, "utf-8");
}
<强>参考强>