Issue in unterminated regular expression literal with eval() function
Copy link to clipboard
Copied
Hi,
I am creating a fillable form on Acrobat Pro DC (acroform) and I would like to parse my xml but in this line
var strData = util.stringFromStream(myXMLFile);
var xmlData = eval(strData.replace(/^\<\?.*\?\>\s*/,""));
I am getting
"SyntaxError: unterminated regular expression literal
182:Document-Level:PopulateDropdown"
it seems the eval() is causing the error but if I remove eval() I will have an issue in this line:
var nLen = xmlData.child("Branch").length();
I am getting this:
TypeError: xmlData.child is not a function
13:Document-Level:PopulateDropdown
Any help will be much appreciated.
Thanks.
Copy link to clipboard
Copied
I don't know JavScript very well and I am still learning, but I couldn't help but notice the following (please excuse if my observations are too rookie):
I am unsure why these variables are declared like this:
1) In the line below, it seems like a variable is missing a replacement function to search for the desired string (but I may be wrong)
- var strData = util.stringFromStream(myXMLFile);
You may have come accross this article by ACP Thom Parker: Text matching with regular expressions using JavaScript
Also see the example on Page 20 of the Acrobat JS Developer Guide , "Debugging with the JavaScript Console".
2) And in this line what is the use of ,"" at the end ?
- var xmlData = eval(strData.replace(/^\<\?.*\?\>\s*/,""));
See also JavaScript™ for Acrobat® API Reference util methods for the following:
- getDataObjectContents
streamFromString
stringFromStream
3-) Last, this variable:
-
var nLen = xmlData.child("Branch").length();
Shouldn't it be: var nLen = xmlData.child("Branch").length; without the "()" ?
Again, please excuse if my observations are not up to par with your JavaScript knowledge or the experts of these support forums.
Copy link to clipboard
Copied
Why do you wrap strdata.replace(...) in eval at all? Is the result of the replace a valid JavaScript statement or expression? What do you see the purpose of eval as?
also what (informally) are you trying to achieve with the replace? Are you really trying to delete from the start of each line constructs like
<?anything anything?>anything...<?anything?> followed by spaces, but not at all if not followed by spaces?
Copy link to clipboard
Copied
My guess is they want to convert that string into a literal object, to be used further in the script.
Copy link to clipboard
Copied
But that's not what eval does (convert to a literal object)... or is it...? To my mind, it executes a JavaScript command, and has no other purpose...
var string="app.alxrt(\"hxllo world\")";
eval(string.replace(/x/,"e")) ;
Copy link to clipboard
Copied
It can be used to do it, for example:
eval("var data = {a:{name:'abc'}}");
app.alert(data.a.name);
Copy link to clipboard
Copied
Hello,
Thank you for your reply. Actually I got some really good guidance here in this: https://www.pdfscripting.com/public/FreeStuff/PDFSamples/PopulateFieldsFromXML_Sample.pdf
but when I tried to access the child nodes, excerpt from the link above:
//Check to see if it is the correct XML Gramar
var nLen = xmlData.child("Customer").length();
TypeError: xmlData.child is not a function
Can you help me access the child nodes and children based from the example on the link.
That would be really helpful.
Copy link to clipboard
Copied
Review the instructions on how to install this folder level script:
https://www.pdfscripting.com/public/Installing_Automation_Tools.cfm?sd=40
Copy link to clipboard
Copied
Hmmm. I am not on top of "eval" but, I don't understand what it's doing in
eval("var data = {a:{name:'abc'}}");
app.alert(data.a.name);
Surely this works well without the eval
var data = {a:{name:'abc'}}
app.alert(data.a.name);
Copy link to clipboard
Copied
LOL... You're right, of course.
But imagine if the data comes in the form of a string from an external source, like a .json or .xml file. You can use eval to convert it into a variable that can be used in the rest of the code with a single command, instead of having to parse it.

