XML编写器Java

XML编写器Java,java,xml,transformer,xmlstreamreader,Java,Xml,Transformer,Xmlstreamreader,我正在尝试用Java实现一个XML编写器。我能够生成该文件,但我有一个问题。问题是编写器在更改行时添加了符号(&)(#)(13;)。我想把它去掉 以下是生成XML的代码: public class WriteXMLFile { static Encryption encryption = new Encryption(); public void constructXmlFile(ArrayList<String> locationHash,ArrayList<String&

我正在尝试用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();
      }
}
公共类WriteXMLFile{
静态加密=新加密();
public void ConstructionXmlFile(ArrayList locationHash、ArrayList encryptedValue、ArrayList ID)引发异常{
试一试{
DocumentBuilderFactory docFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder=docFactory.newDocumentBuilder();
//根元素
Document doc=docBuilder.newDocument();
Element rootElement=doc.createElement(“xxxxxxx”);
doc.appendChild(rootElement);
//身体元素
元素主体=doc.createElement(“主体”);
根元素。子元素(体);
//消息元素
元素消息=doc.createElement(“消息”);
body.appendChild(消息);
//记录要素
元素记录=doc.createElement(“记录”);
message.appendChild(记录);
对于(int counter=0;counter
这是一个结果样本

<?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>

1368523
[B@15db9742
WQt3I/XOkZx/O1Q9XZULPBHBCDP3V1TAFWVK4X+roT3OsI1aZ21s6H0h7ki8peQ2tFWrLbc3gB4Gi和#13;
AVHkbPcHyfz7pZXOhmgoE+KiruI3yCc0qUHYZCxqNoAjxB6empiBDZEwcc1Dh22mTB2ZpaUsDhpf和#13;
m4+EVPN7e6ey66rXT7+igJ7Qp/xfvOJrIwcHqCEkgTOnubAnwRrtUw2ejPe6qw==
853749
[B@2cfb4a64
PNLnrjiyewiqipruqc5hwfscqancencjkxt395kdpe9ief7tr4bz3ryvsjoqmyhq7kgof6gb
;
au4qymlqmpeola95cuqxvbsndxvpwgxvnmu8toyu28usaemevxlyty+mrsl3DGTjNGIH256IAS和#13;
L/h4Fch/OVoV6a/pZ9w+HL7Xwvp/g6EixIW1g22Y


正如您所看到的,(&)(#)(13;)字符出现在行的末尾。如何在导出过程中删除该字符?

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

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

此符号表示回车('\r')。请检查text=text.replace(“\r”,”);?是的,我做了,它工作正常。谢谢…:)@apoellistsi,如果答案有用,请接受好吗?