5

What is the best way to handle large string constants in Java?

Imagine that I have a test fixture for SOAP and I want to send the following string:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
  xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
       <QuoteRequest xmlns="https://www.bcponline.co.uk">
            <header>
                <SourceCode>BN325</SourceCode>
                <MessageSource>B2B3</MessageSource>
                <Profile>B08A</Profile>
                <Password>AP3U86V</Password>
                <OperatorID>flightline</OperatorID>
                <ShowWebOptions>0</ShowWebOptions>
            </header>
            <serviceSelection>
                <ServiceProviderCode></ServiceProviderCode>
                <ProductCode>CarParking</ProductCode>
                <IATACode>lgw</IATACode>
            </serviceSelection>
            <quoteDetails>
                <DepartureDate>21-Jun-2005</DepartureDate>
                <DepartureTime>07:00</DepartureTime>
                <ReturnDate>28-Jun-2005</ReturnDate>
                <ReturnTime>07:00</ReturnTime>
                <QuoteReference></QuoteReference>
                <NoPax>1</NoPax>
            </quoteDetails>
            <sPostCode></sPostCode>
        </QuoteRequest>
    </soap:Body>
</soap:Envelope>

I'd rather not put quotes and pluses around every line. If I put it in a file it's extra code and it would be somewhat hard to put several strings in the same file. XML has problems escaping text (I have to use CDATA ugliness). Is there an easier way?

4
  • lame suggestion: how about encoding the line breaks as \ns? Commented Jan 25, 2011 at 21:43
  • 1
    You can put it in properties file. Still have to write code, but not that much and you can put more than one string in it Commented Jan 25, 2011 at 21:45
  • Related: stackoverflow.com/questions/782810/… Commented Jan 25, 2011 at 22:13
  • [rant]I can't believe Java still hasn't fixed this. It's one of the main reasons that Java sucks for so many applications. Why is it so hard for them to get this?[/rant] Commented Feb 8, 2013 at 16:34

2 Answers 2

3

If the strings are unrelated, you could put them in separate files even if it's a lot of files (what is the problem with that?).

If you insist on one file, you could come up with a unique delimiter, but you would be paying a price when attempting to randomly access a specific entry.

Data files should almost always be externalized (likely in a separate directory) and read when needed, rather than hardcoded into the code. It's cleaner, reduces code size, reduces need for compilation, and allows you to use the same data file for multiple test. Most test fixtures as well as build and integration tools support external files.

Or, you could write code or a builder that builds SOAP from arguments, making this all a lot more concise (if you're willing to pay the runtime cost). (Correction: I see you changed your sample, this would be nasty to auto-generate).

Sign up to request clarification or add additional context in comments.

4 Comments

If you insist on one file, you could come up with a unique delimiter - I'm using this with an empty line as delimiter, it's very easy to read. but you would be paying a price when attempting to randomly access a specific entry. - This normally never happens, he was thinking about putting it in program, so it's surely not in order of GB in size, so he can keep it all in memory.
@maaartinus: I would be very careful about using an empty line as a delimiter in a format that allows empty lines as whitespace (is that correct?). It takes one merge error or a careless cut-paste to mess things up.
Sure, but I am careful: Actually, in my format, the first line of a "paragraph" is it's name. I use an enum for all the names. In case I mess it up, there's a missing name or something like that. Any missing, duplicated, or unknown name raises an exception during load.
Where would you prefer to put such file? In the same folder as the class folder?
-1

What about using a StringBuilder? You can always use StringBuilder.toString() to get the String...

2 Comments

That sounds even harder than using quotes and pluses. At least it will be faster for the computer to process.
@User1 - it wouldn't even be faster. Little pieces of the string would be in your class file, which would have to get assembled at runtime, rather than just having the entire pre-assembled string compiled in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.