What is the best solution to appen a line at the beginning of a String variable in Java?

366 views Asked by At

I have the following situation.

Into a Java application I have a String variable named *fattura** that contains structured data extracted from an XML...so this fattura variable contains text like this:

<FatturaElettronicaBody>                                                        
    <DatiGenerali>                                                                  
        <DatiGeneraliDocumento>                                                         
            <TipoDocumento>TD01</TipoDocumento>                                             
            <Divisa>EUR</Divisa>                                                            
            <Data>2015-03-16</Data>                                                         
            <Numero>004600002117</Numero>                                                   
            <ImportoTotaleDocumento>9.57</ImportoTotaleDocumento>                           
        </DatiGeneraliDocumento>                                                        
    </DatiGenerali>
</FatturaElettronicaBody>

Now what I need to do is to append at the beginning of the previous XML code the line that identify that this XML section is an XML document itself, this one:

<?xml version="1.0" encoding="UTF-8"?> 

So the final result have to be something like this:

<?xml version="1.0" encoding="UTF-8"?>   
<FatturaElettronicaBody>                                                        
        <DatiGenerali>                                                                  
            <DatiGeneraliDocumento>                                                         
                <TipoDocumento>TD01</TipoDocumento>                                             
                <Divisa>EUR</Divisa>                                                            
                <Data>2015-03-16</Data>                                                         
                <Numero>004600002117</Numero>                                                   
                <ImportoTotaleDocumento>9.57</ImportoTotaleDocumento>                           
            </DatiGeneraliDocumento>                                                        
        </DatiGenerali>
</FatturaElettronicaBody> 

So this could be done creating a new String fatturaXml variable and first append this line into it:

    <?xml version="1.0" encoding="UTF-8"?>

and finally append the fattura variable content.

But exist a smarter solution? I have the fattura variable that contains the XML content, can I directly put the line before the content of the fattura variable without passing for a new variable?

5

There are 5 answers

2
Aditya On BEST ANSWER

It you just need to work on the string, there are simple string prepend options:

fattura  = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + fattura ;

I guess someone already mentioned that.

But I think StringBuilder is a better way for string manipulation operations as Strings are immutable in Java. So it would help if you're manipulating string more than once, otherwise it might be an overkill.

StringBuilder _fatturaBuilder = new StringBuilder(fattura);
_fatturaBuilder.insert(0, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
3
JFPicard On

Maybe you can use a StringBuilder (https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html)

Like this:

StringBuilder sb = new StringBuilder(fattura);
sb.insert(0, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
1
Miguel On

If you have to use this process a lot of times I recommend you use StringBuffer to save memory. If you use:

String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + fattura;

You are creating a new object String.

On the other hand, if you use:

String fattura = "xml text of fattura";
StringBuffer stb_fattura = new StringBuffer("<?xml version=\"1.0\"encoding=\"UTF-8\"?>");
stb_fattura.append(fattura);

You are modifying the same object stb_fattura.

0
ewhoch On

Strings are by definition immutable, meaning their content cannot change without a new variable creation. The mutable form is StringBuilder, by definition a mutable sequence of characters.

The best options would be to trace the creation of the "fattura" variable and replace it's lifecycle with a StringBuilder from the start.

As others have noted, the simplest is to express the value as

fattura = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + fattura;

But be aware that this is in essence creating a new string, discarding the old, and assigning fattura to the newly created String.

There would be little benefit to create a new StringBuilder at this point in the code unless there is reason to believe the sequence will need to be further modified. That would create even further overhead.

2
TheCodingFrog On

It might be over killing, but I would do it this way:

package com.test;

public class XmlDeclaration {

    private String version;
    private String encoding;

    public XmlDeclaration(String version, String encoding) {
        this.version = version;
        this.encoding = encoding;
    }

    public String toString() {
        return "<?xml version=\""+ version +"\" encoding=\" "+ encoding +"\"?>";
    }

    public String append(String xml) {
        return new StringBuilder(this.toString()).append(xml).toString();
    }

    public void main () {
        String xml = "My XML";
        String xmlWithDeclaration = new XmlDeclaration("1.0", "UTF-8").append(xml);
    }
}