XML Writer Java - PullRequest
       42

XML Writer Java

0 голосов
/ 21 мая 2018

Я пытаюсь реализовать средство записи XML на Java.Я могу создать файл, но у меня есть одна проблема.Проблема в том, что писатель добавляет символ (&) (#) (13;), когда он меняет строку.Я хочу удалить это.

Вот мой код для создания XML:

public class WriteXMLFile {
static Encryption encryption = new Encryption();

public void constructXmlFile(ArrayList<String> locationHash,ArrayList<String> encryptedValue,ArrayList<String> ids) throws Exception{
    try {

        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

        // root elements
        Document doc = docBuilder.newDocument();
        Element rootElement = doc.createElement("xxxxxxx");
        doc.appendChild(rootElement);

        // body elements
        Element body = doc.createElement("Body");
        rootElement.appendChild(body);

        // message elements
        Element message = doc.createElement("Message");
        body.appendChild(message);

        // Records elements
        Element records = doc.createElement("Records");
        message.appendChild(records);
        for (int counter = 0; counter < ids.size(); counter++) {              
            // ID elements
            Element id = doc.createElement("ID");
            id.appendChild(doc.createTextNode(ids.get(counter)));
            records.appendChild(id);

            // LocationInformation elements
            Element locationInformation = doc.createElement("LocationInformation");
            locationInformation.appendChild(doc.createTextNode(locationHash.get(counter)));
            records.appendChild(locationInformation);

            // BeneficiaryInformation elements
            Element beneficiaryInformation = doc.createElement("BeneficiaryInformation");
            beneficiaryInformation.appendChild(doc.createTextNode(encryptedValue.get(counter)));
            records.appendChild(beneficiaryInformation);


         }   

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        //for pretty print
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        DOMSource source = new DOMSource(doc);

        //write to console or file
        StreamResult file = new StreamResult(new File("xxxxxxxxx\\BenInformation.xml"));

        //write data
        transformer.transform(source, file);
        System.out.println("DONE");


      } catch (ParserConfigurationException pce) {
        pce.printStackTrace();
      } catch (TransformerException tfe) {
        tfe.printStackTrace();
      }
}

А вот пример результата

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xxxxxxxxx>
<Body>
    <Message>
        <Records>
            <ID>1368523</ID>
                <LocationInformation>[B@15db9742</LocationInformation>
                <BeneficiaryInformation>WQt3I/XOkZx/o1q9xzUlPhbcdp3V1TafwVK4x+roT3OsI1aZ21s6H0h7ki8peQ2tFWrLbc3gB4Gi&#13;
                    AVHkbPcHyfz7pZXOhmgoE+KiruI3yCc0qUHYZCxqNoAjxB6empiBDZEwcc1Dh22mTB2ZpaUsDhpf&#13;
                    m4+EVPN7e6ey66rXT7+igJ7Qp/xfvOJrIwcHqCEkgTOnubAnwRrtUw2ejPe6qw==</BeneficiaryInformation>
            <ID>853749</ID>
                <LocationInformation>[B@2cfb4a64</LocationInformation>
                <BeneficiaryInformation>pnlNRJIYiEWiQIPrUQc5hwFSCQAnCiNexcCjkxT395kdPE9iEf7Tr4BZ3rYvSJoQMYhQ7kGOf6Gb&#13;
                AU4QymLqMPEOla95CuQXvBSNDXVPWgxCVNmU8TOyU28USaEMEVXLyotY+mrsl3DGTjNGIH256IAS&#13;
                L/h4Fch/OVoV6a/pZ9w+HL7Xwvp/g6EixIW1g22Y</BeneficiaryInformation>
        </Records>
    </Message>
<Body>

Как видите, в конце строки появляется символ (&) (#) (13;).Как я могу удалить это во время экспорта?

1 Ответ

0 голосов
/ 21 мая 2018

Этот символ означает возврат каретки ('\ r') .Поэтому, пожалуйста, проверьте с кодом ниже:

 text = text.replace("\r", "");
...