请检查此代码,如果我在$ .parseXML中使用URL,则documentElement将无法工作。
我正在尝试将此XML file传递到表中。但是parseXML如果使用URL解析器将无法正常工作。
var xml = $.parseXML("<ExtractSummaryDateSet><_date>2017-09-20</_date><_portfolioSummaries><ExtractSummaryDateSetDetail><_portfolioName>52613661</_portfolioName><_detail><Before>0</Before><After>-329</After><ChangeMaturing>0</ChangeMaturing><ChangeNew>-329</ChangeNew></_detail></ExtractSummaryDateSetDetail><ExtractSummaryDateSetDetail><_portfolioName>52613661_LP</_portfolioName><_detail><Before>0</Before><After>-329</After><ChangeMaturing>0</ChangeMaturing><ChangeNew>-329</ChangeNew></_detail></ExtractSummaryDateSetDetail><ExtractSummaryDateSetDetail><_portfolioName>526136|Total</_portfolioName><_detail><Before>0</Before><After>-329</After><ChangeMaturing>0</ChangeMaturing><ChangeNew>-329</ChangeNew></_detail></ExtractSummaryDateSetDetail></_portfolioSummaries></ExtractSummaryDateSet>");
var extractTableData = function(x){
var detailField = [];
detailField.push({});
detailField[0].title = 'Name';
detailField[0].values = [];
for (var i =0; i<x.length; i++){
detailField[0].values.push(x[i].childNodes[0].innerHTML);
var detail = x[i].childNodes[1].childNodes;
for(var j =0 ; j<detail.length; j++){
var detailf = detailField.find(function(arr){
return arr.title === detail[j].localName
});
if(!detailf){
detailField.push({
'values' : [],
'title' : null
});
detailField[detailField.length -1].values.push(detail[j].innerHTML);
detailField[detailField.length -1].title = detail[j].localName;
}
else{
detailf.values.push(detail[j].innerHTML);
}
}
}
return detailField;
};
$(document).ready(function(){
$("#test1").html(function(i, origText){
debugger;
var doc = xml.documentElement;
var table = "<table>";
table+= "<tr><th>"+doc.childNodes[0].localName+"</th><td>"+doc.childNodes[0].innerHTML+"</td></tr>"+
"<tr><th>"+doc.childNodes[1].localName+"</th><td>_</td>";
table+="</table>";
table+="<hr>"
var x = doc.childNodes[1].childNodes;
var tableData = extractTableData(x);
table+="<table>"
for(var i=0; i<tableData.length; i++)
{
table+="<tr><th>"+tableData[i].title+"</th>";
for(var j=0;j<tableData[i].values.length;j++){
table+="<td>"+tableData[i].values[j]+"</td>"
}
table+="</tr>"
}
return table+="</table>";
});
});
table {
color: #333; /* Lighten up font color */
font-family: Helvetica, Arial, sans-serif; /* Nicer font */
width: 640px;
border-collapse:
collapse; border-spacing: 0;
}
td, th { border: 1px solid #CCC; height: 30px; } /* Make cells a bit taller */
th {
background: #F3F3F3; /* Light grey background */
font-weight: bold; /* Make sure they're bold */
}
td {
background: #FAFAFA; /* Lighter grey background */
text-align: center; /* Center our text */
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<body>
<div id="test1">Table</div>
</body>
情境
我在parseXML中找到的大多数文档都使用直接导入,例如
var xml = "<rss version='2.0'><channel><title>RSS Title</title></channel></rss>",
xmlDoc = $.parseXML( xml ),
有可能做喜欢
xhttp.open("GET", "URL", true);
var xml = xhttp.responseXML;
xmlDoc = $.parseXML( xml )
我了解到responseXML不能与parseXML一起使用,仅是一个示例来说明$ .parseXML的情况,它可以直接从URL调用还是有其他方法来实现?
最佳答案
由于您已经在使用jQuery,因此可以使用$.get(url, callback)
对URL执行Ajax提取,然后在响应中自动检测XML,并为回调提供已解析的Document
对象:
$.get("https://myurl/file.xml", function(xml) {
// parsed document named `xml` exists in here
});
// parsed document does not exist out here
由于
$.parseXML
是同步的并立即返回其结果,而网络获取是异步的,并将它们的值提供给将来某个时间调用的回调函数,因此您将需要稍微重新组织代码。如果需要帮助来了解如何重组以允许异步Ajax提取,请参见How do I return the response from an asynchronous call?