在Java中处理大字符串常量

Use*_*er1 5 java string

在Java中处理大字符串常量的最佳方法是什么?

想象一下,我有一个SOAP的测试夹具,我想发送以下字符串:

<?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>
Run Code Online (Sandbox Code Playgroud)

我宁愿不在每一行都加上引号和加号.如果我把它放在一个文件中它是额外的代码,并且在同一个文件中放置几个​​字符串会有些困难.XML在转义文本时遇到问题(我必须使用CDATA丑陋).有没有更简单的方法?

cha*_*tle -1

使用 StringBuilder 怎么样?您始终可以使用 StringBuilder.toString() 来获取字符串...