I am currently having an annoying issue that I have been trying to solve for hours. I am developing a Web Page with multiple fields that then exports the data to XML to be read by Excel later (note: this is a project I have been tasked with at work).
You can find the latest of the form here.
In IE8, the following code:
function xmlToString(xmlData) {
var xmlString;
//IE
if (window.ActiveXObject){
xmlString = xmlData.xml;
}
// code for Mozilla, Firefox, Opera, etc.
else{
xmlString = (new XMLSerializer()).serializeToString(xmlData);
}
return xmlString;
}
Returns an "undefined" value, and in Chrome it doesn't return anything.
To parse the XML, I am using an AJAX call which gets an XML template:
$.ajax({
type:"GET",
url:"dl/Template_Simple.xml",
dataType: "xml",
success: function(xml) {
$simpleXml = $(xml);
},
error: function() {
alert("There was a problem loading the XML file.");
}
});
Later in the code, I attempt to stringify the $simpleXml object and allow it to be downloaded, but for some reason I cannot for the life of me, figure out how to solve the "undefined" and non-returning issues.
function exportData()
{
processSimpleXml();
var dataSimple = xmlToString($simpleXml);
alert(dataSimple);
$('<a href="data:' + dataSimple + '" download="' + getDownloadName($simpleXml) + '">Download Simple Data (Spreadsheet Compatible)</a>').appendTo("#exported");
//$("<a href='data:" + dataComplete + "' download='" + getDownloadName($completeXml) + "'>Download Complete Data</a>").appendTo("#exported");
}
function processSimpleXml()
{
$(".xml[xmlSimple]").each(function(){
if ($(this).attr("xmlSimple") == "FirstName")
{
$simpleXml.find("FirstName").text($(this).val());
alert($simpleXml.find("FirstName").text());
}
else if ($(this).attr("xmlSimple") == "LastName")
{
$simpleXml.find("LastName").text($(this).val());
}
else if ($(this).attr("xmlSimple") == "JobReference")
{
$simpleXml.find("JobReference").text($(this).val());
}
else if ($(this).attr("xmlSimple") == "HireDate")
{
$simpleXml.find("HireDate").text($(this).val());
}
else if ($(this).attr("xmlSimple") == "RefereeName")
{
$simpleXml.find("RefereeName").text($(this).val());
}
});
}
I have checked multiple solutions here and in other places but I simply cannot find one. Thank you for any help you can give me in advance.
Also, because Chrome does not seem to return anything it also does not append the Download link...
No comments:
Post a Comment