Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

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

4 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.