jeudi 21 mai 2015

How can I obtain the size in byte of this String? [duplicate]

This question already has an answer here:

I have the following problem.

Into a Java application I have something like:

String docXML = ediMonUtils.creaXmlRifiutaRequestCharge(currentRequestCharge);

where this is the code of the creaXmlRifiutaRequestCharge() method:

public static String creaXmlRifiutaRequestCharge(RequestCharge requestCharge) {

    logger.debug("GestioneFatturaSchemaDAO.creaFileXML()");

    // Valore campi XML:
    String id = requestCharge.getPk_coda();
    String pIvaMittente = requestCharge.getpIvaMittente();
    String pIvaDestinatario = requestCharge.getpIvaDestinatario();
    String dataInvio = requestCharge.getData_in();
    String tipoDoc = "Ricevuta";
    String tipo = "380";
    String numeroDocumento = "ff";
    String stato ="ko";
    String data = requestCharge.getData_doc();       // Da controllare se il valore è queesto
    String codice = "000";
    String descrizione = "Documento NON Conforme / NON dovuto";

    // Creazione dell'XML:
    StringBuffer xml = new StringBuffer();

    xml.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n");
    xml.append("<Messaggio>\n");

        xml.append("<Intestazione>\n");
            xml.append("<Da>").append(pIvaMittente).append("</Da>\n");
            xml.append("<A>").append(pIvaDestinatario).append("</A>\n");
            xml.append("<id>").append(id).append("</id>\n");
            xml.append("<idEnel/>\n");
            xml.append("<DataInvio>").append(dataInvio).append("</DataInvio>\n");
            xml.append("<DataRicezione/>\n");
            xml.append("<InRisposta/>\n");
            xml.append("<TipoDoc>").append(tipoDoc).append("</TipoDoc>\n");
        xml.append("</Intestazione>\n");

        xml.append("<Documenti>\n");
            xml.append("<Ricevuta>\n");
                xml.append("<Testata>\n");
                    xml.append("<Documento>\n");
                        xml.append("<Tipo>").append(tipo).append("</Tipo>\n");
                        xml.append("<NumeroDocumento>").append(numeroDocumento).append("</NumeroDocumento>\n");
                        xml.append("<Stato>").append(stato).append("</Stato>\n");
                        xml.append("<Data>").append(data).append("</Data>\n");
                    xml.append("</Documento>\n");
                xml.append("</Testata>\n");

                xml.append("<Dettaglio>\n");
                    xml.append("<Messaggio>\n");
                        xml.append("<Codice>").append(codice).append("</Codice>\n");
                        xml.append("<Descrizione>").append(descrizione).append("</Descrizione>\n");
                    xml.append("</Messaggio>\n");
                xml.append("</Dettaglio>\n");
            xml.append("</Ricevuta>\n");
        xml.append("</Documenti>\n");

    xml.append("</Messaggio>\n");

    System.out.println("XML: " + xml.toString());

    return xml.toString();

}

As you can see the previous method simply create the content of an XML file and put it into a String named docXML, by:

String docXML = ediMonUtils.creaXmlRifiutaRequestCharge(currentRequestCharge);

How can I obtain the size of this docXML in byte?

Tnx

Aucun commentaire:

Enregistrer un commentaire