...

View Full Version : unterminated string literal help



lynchpin
01-25-2006, 10: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 %>';
document.btraxtestForm.HighwayMessage.innerText=blah;
}

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,
colm

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

lynchpin
01-25-2006, 11:21 AM
thanks liorean.

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

thanks again,
colm

liorean
01-25-2006, 12: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.

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

i will give it a go.

colm

lynchpin
01-25-2006, 05: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?

e.g.
String one = "Hello "Whatever" World";
becomes:
String two = "Hello /"Whatever/" World";

thanks,
colm



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum