View Full Version : unterminated string literal help

01-25-2006, 11:56 AM
Hi all,
im trying to read an xml file and put it into a textarea on my page.
here is the code that returns the xml file as a string:

Document doc = new Document();
SAXBuilder saxBuilder = new SAXBuilder("org.apache.xerces.parsers.SAXParser");
XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat());
doc = saxBuilder.build(filesName);
String xml = outputter.outputString(doc);

no when i try to pass this to a javascript as follows:

function putXML(fileName){
var blah = '<%= xml %>';

it gives me a unterminated string literal error.

also, i will never know the contents of this xml file so i cant split up the string.
has anyone any ideas how i will fix this?

thanks in advance,

01-25-2006, 12:08 PM
You need to replace any apostrophes/single quotes in the XML string with JavaScript escapes before you insert it into the JavaScript.

01-25-2006, 12:21 PM
thanks liorean.

is there a simple way of doing this seeing as i will never know the conents of the xml file?

thanks again,

01-25-2006, 01:05 PM
I don't know what would be best in your server-side language, but what you need to do is replace all occurences of ' in the string xml with \'. (Note: This is the output string, so if your server side language uses backslash for escaping you need to escape the backslash in the replacement text.)

Regex can do it, or a string compare. You don't need to know anything about the actual contents of the string, you just need to do the replacement.

01-25-2006, 01:08 PM
great. cheers.

i will give it a go.


01-25-2006, 06:26 PM
that worked perfectly.
on a seperate note, is there a way of inserting a carriage return into a string before every occurance of a double quote without knowing where the doulbe quotes exist?

String one = "Hello "Whatever" World";
String two = "Hello /"Whatever/" World";