Please check this code , If i am using URL in $.parseXML , the documentElement wontwork.
I am trying to pass this XML file into table. but parseXML wont work if using URL paser.
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>
Scenario
Most of the Doc I found in parseXML are using direct import like
var xml = "<rss version='2.0'><channel><title>RSS Title</title></channel></rss>",
xmlDoc = $.parseXML( xml ),
Is it possible to do like
xhttp.open("GET", "URL", true);
var xml = xhttp.responseXML;
xmlDoc = $.parseXML( xml )
I understand the responseXML are not go alone with parseXML, just an example to show the scenario that $.parseXML, is it able to call directly from URL or there are other ways to do it?