Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Sep 2005
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question unterminated string literal help

    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:

    Code:
    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:
    Code:
    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

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    You need to replace any apostrophes/single quotes in the XML string with JavaScript escapes before you insert it into the JavaScript.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    New Coder
    Join Date
    Sep 2005
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #5
    New Coder
    Join Date
    Sep 2005
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    great. cheers.

    i will give it a go.

    colm

  • #6
    New Coder
    Join Date
    Sep 2005
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •