Expand my Community achievements bar.

SOLVED

Failure to import XML data into a form

Avatar

Level 5

For a long time now, I have been using a script to import data from XML data files into a LiveCycle form, using a script originally offered by @radzmar . That script starts as follows:

function importXML(){
	// Wenn PDF-Viewer älter als 9.2 ist, funktioniert der Import nicht!
	if (parseFloat(app.viewerVersion) < 9.2) {
	    xfa.host.messageBox("You must use Adobe Reader version 9.2 or greater for this import function to operate.", "Your version of Adobe Reader is too old", 0, 0);
	} else {
		// Externe Datei laden.
	    var vStream = util.readFileIntoStream();
	    if (vStream) {
	        var vImport, vImportData, vImportXML, vNGEXML;
	        try {
	        	// Die folgende Zeile sieht nur weges des regulären Ausdrucks aus, als ob sie auskommentiert wäre, ist aber sehr wichtig!!!
	            vImport = util.stringFromStream(vStream)
	            .replace(/(\<\?.*\?\>\s*)|(\<!-{2}(.|\n|\r)*-{2}\>)|(\r\n|\r|\n)/g, "");
	            vImportData = eval(vImport);
				vImportXML = vImportData.toXMLString();

				// Assign the XML code as a string value to Textfield1
				form1.SfDataImport.TfAurImport.rawValue = vImportXML;

Occasionally, that import script failed and I figured it was because the XML data file included curly brackets {} in the data fields.

I am wondering if there is a way to modify the script so it would not fail but replace curly brackets by parentheses and therefore prevent the script's failure.

 

By the way, can you explain what the following line does in that script?

	            .replace(/(\<\?.*\?\>\s*)|(\<!-{2}(.|\n|\r)*-{2}\>)|(\r\n|\r|\n)/g, "");

Thank you in advance.

1 Accepted Solution

Avatar

Correct answer by
Level 10

Hi,

 

the import function isn't the problem, it's your additional code that fails.

I've stripped down the code to the bare neccessariest, and the import works without problems with your XML.

You can see the imported date in the field under form1.SfDataImport.TfNgeImport.

function importXML(){
	// Import is only available with Reader 9.2 and above
	if (parseFloat(app.viewerVersion) < 9.2) {
	    xfa.host.messageBox("You must use Adobe Reader version 9.2 or greater for this import function to operate.", "Your version of Adobe Reader is too old", 0, 0);
	} else {
		// Import external file
	    var vStream = util.readFileIntoStream();
	    if (vStream) {
	        var vStreamData, oXML;
	        try {
	           // Version 1: Import XML file and remove its XML declarations
	           // vStreamData  = util.stringFromStream(vStream, "utf-8").replace(/^\<\?.*\?\>$/gm, "");
	           // Version 2: Import XML file and remove its XML declarations and whitespaces
	           vStreamData  = util.stringFromStream(vStream).replace(/(\<\?.*\?\>\s*)|(\<!-{2}(.|\n|\r)*-{2}\>)|(\r\n|\r|\n)/g, "");
			   
			   // Optional: Create a data node in the RAM and store the imported data in it. It can be used to analyse the imported data.
			   // With oXML.nodes.item(0).name; for example, you can check the name of the first element in the XML tree to control the correct XML file was imported.
	           oXML = xfa.datasets.createNode("dataGroup", "importData");
	           oXML.loadXML(vStreamData, false, false);
	           
				// Version 1: Assign the XML code as a string value to text field from import
				// form1.SfDataImport.TfNgeImport.rawValue = vStreamData;
				// Version 2: Assign the XML code as a string value to text field from data node
				form1.SfDataImport.TfNgeImport.rawValue = oXML.nodes.item(0).saveXML("pretty").replace(/^\<\?.*\?\>$/gm, "");
			            	
				return true;
			// When import has failed.
	        } catch (e) {
	            vImport = "";
	            var cMsg = "The import failed because the file may not be an XML data file.\n\nThe import was interrupted.\n\nError details: " + e;
	            xfa.host.messageBox(cMsg, "Error during Import", 0, 0);
	            return false;
	        }
	    }
	}
}

You should run you additional code in a separate function outside the try-catch since every little error will interrupt the import function.

 

View solution in original post

7 Replies

Avatar

Level 10

The replace method replaces the XML declarations that can appear at the beginning of every XML file. The import function can't handle this, so it has to be removed.

 

Do you have a sample file with the curly brackets?

Avatar

Level 5

Thanks for taking the time. 
here is the link to the form: https://drive.google.com/file/d/1pBiPnw5E0m9bppGMDkLph0JnYnQxvOhB/view?usp=drivesdk

 

here is the link to the XML data file:   https://drive.google.com/file/d/1LbPgVXfae-sGoZmWeC8g7h4R5HVa-gcF/view?usp=drivesdk

in the form, the import XML function is on both buttons on the top right of the page. The dummy data file should be uploaded using the « Import NGE button »

 

 

i know I can edit the XML data file to remove the curly brackets… which is the only solution I currently have. My concern is that there may be many different users of this form and I won’t be able to train them all to that level. 

thx.

Avatar

Correct answer by
Level 10

Hi,

 

the import function isn't the problem, it's your additional code that fails.

I've stripped down the code to the bare neccessariest, and the import works without problems with your XML.

You can see the imported date in the field under form1.SfDataImport.TfNgeImport.

function importXML(){
	// Import is only available with Reader 9.2 and above
	if (parseFloat(app.viewerVersion) < 9.2) {
	    xfa.host.messageBox("You must use Adobe Reader version 9.2 or greater for this import function to operate.", "Your version of Adobe Reader is too old", 0, 0);
	} else {
		// Import external file
	    var vStream = util.readFileIntoStream();
	    if (vStream) {
	        var vStreamData, oXML;
	        try {
	           // Version 1: Import XML file and remove its XML declarations
	           // vStreamData  = util.stringFromStream(vStream, "utf-8").replace(/^\<\?.*\?\>$/gm, "");
	           // Version 2: Import XML file and remove its XML declarations and whitespaces
	           vStreamData  = util.stringFromStream(vStream).replace(/(\<\?.*\?\>\s*)|(\<!-{2}(.|\n|\r)*-{2}\>)|(\r\n|\r|\n)/g, "");
			   
			   // Optional: Create a data node in the RAM and store the imported data in it. It can be used to analyse the imported data.
			   // With oXML.nodes.item(0).name; for example, you can check the name of the first element in the XML tree to control the correct XML file was imported.
	           oXML = xfa.datasets.createNode("dataGroup", "importData");
	           oXML.loadXML(vStreamData, false, false);
	           
				// Version 1: Assign the XML code as a string value to text field from import
				// form1.SfDataImport.TfNgeImport.rawValue = vStreamData;
				// Version 2: Assign the XML code as a string value to text field from data node
				form1.SfDataImport.TfNgeImport.rawValue = oXML.nodes.item(0).saveXML("pretty").replace(/^\<\?.*\?\>$/gm, "");
			            	
				return true;
			// When import has failed.
	        } catch (e) {
	            vImport = "";
	            var cMsg = "The import failed because the file may not be an XML data file.\n\nThe import was interrupted.\n\nError details: " + e;
	            xfa.host.messageBox(cMsg, "Error during Import", 0, 0);
	            return false;
	        }
	    }
	}
}

You should run you additional code in a separate function outside the try-catch since every little error will interrupt the import function.

 

Avatar

Level 5

Wow. Thank you so much.

I'll do exactly as you suggest... 

Avatar

Level 1

hey, can you help me with my xml file (inside .zip), I have exported using xfa.datasets.data.saveXML('pretty'); and now I would like to import it back. However, I can do that with xfa.host.importData(); using Acrobar Pro, but when the form is saved as 'Reader Extended form', import button no longer opens the dialog for selecting files from my PC, so I am searching for alternative.

 

The case is following: if form behaves not expected, I make export of entered data, fix the form, import again the data.

 

Is the hierarchy of my "noWorkingXML" form to complicated for the import?

Just to mention,  in working import top name of the form is "Nalozi" and in non working import top name of the form is "knjigovodstvo" - and I adjusted the function to those needs

If you can take a look, and tel me what is wrong,

I appreciate your time, tnx

Ivica

Avatar

Level 5

You are correct that the importData function is not allowed in the Reader Extended version of the form.

The script offered by @radzmar is great in particular because it allows to import and entire datafile as one textfield and then, you can parse the information to re-populate the form. This is great to import a subset of data only (i used it to transfer some data from 1 form to another) ... but it would get cumbersome if you were to import a large number of fields. 

Avatar

Level 1

I just use the original, and it works. @radzmar tnx for the script

 

function importXML () {
	// Wenn PDF-Viewer älter als 9.2 ist, funktioniert der Import nicht!
	if (parseFloat(app.viewerVersion) < 9.2) {
	    xfa.host.messageBox("Sie müssen Adobe Reader 9.2 oder höher benutzen, um diese Funktion auszuführen.", "Adobe Reader zu alt", 0, 0);
	} else {
		// Externe Datei laden.
	    var vStream = util.readFileIntoStream();
	    if (vStream) {
	        var vImport, vImportData, vImportXML;
	        try {
	        	// Die folgende Zeile sieht nur weges des regulären Ausdrucks aus, als ob sie auskommentiert wäre, ist aber sehr wichtig!!!
	            vImport = util.stringFromStream(vStream)
	            .replace(/(\<\?.*\?\>\s*)|(\<!-{2}(.|\n|\r)*-{2}\>)|(\r\n|\r|\n)/g, "");
	            vImportData = eval(vImport);
				vImportXML = vImportData.toXMLString();
				xfa.datasets.data.loadXML(vImportXML, false, true);
				xfa.form.remerge();
				xfa.form.recalculate(1);    	            	
				return true;
			// Wenn Import fehlschlägt, Meldung anzeigen.
	        } catch (e) {
	            vImport = "";
	            var cMsg = "Die importierten Daten sind keine gültigen XML-Daten.\n\nImport abgebrochen.\n\nFehlerdetails: " + e;
	            xfa.host.messageBox(cMsg, "Fehler beim Import", 0, 0);
	            return false;
	        }
	    }
	}
}