2

我正在尝试用 Java 实现 XML 编写器。我能够生成该文件,但我有一个问题。问题是作者在换行时添加了符号 (&)(#)(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;) 字符出现在行尾。如何在导出过程中删​​除它?

4

1 回答 1

3

该符号表示回车 ('\r')。因此,请检查以下代码:

 text = text.replace("\r", "");
于 2018-05-21T12:41:22.780 回答