在java中的xml标记末尾创建空间

时间:2017-02-28 10:28:15

标签: java xml

我的xml标签是:

<Description />

我想要空间:

        String thisLine = "";
        String xmlString = "";
        BufferedReader br = new BufferedReader(new FileReader(originalXmlFilePath));
        while ((thisLine = br.readLine()) != null) {
            xmlString = xmlString + thisLine.trim();
        }
        br.close();

        ByteArrayInputStream xmlStream = new ByteArrayInputStream(xmlString.getBytes());

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        dbf.setIgnoringElementContentWhitespace(true);
        dbf.setValidating(false);
        Document doc = dbf.newDocumentBuilder().parse
            (xmlStream );

        doc.setXmlStandalone(true);



        DOMSignContext dsc = new DOMSignContext
            (keyEntry.getPrivateKey(), doc.getDocumentElement());


        javax.xml.crypto.dsig.XMLSignature signature = fac.newXMLSignature(si, ki);


        signature.sign(dsc);


        // Output the resulting document.
    //  OutputStream os = new FileOutputStream(new File(destnSignedXmlFilePath));
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer trans = tf.newTransformer();
         trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

        trans.setOutputProperty(OutputKeys.INDENT, "yes");


        StringWriter writer = new StringWriter();
        trans.transform(new DOMSource(doc), new StreamResult(writer));
        String output = writer.getBuffer().toString();//.replaceAll("\n|\r", "");


        System.out.println("output== "+output);

我怎样才能用Java做到这一点?

我正在签署xml文档,在原始文件空间中已经使用但是当我使用下面的代码并打印它时,它打印没有空间。

févr. 28, 2017 10:56:07 AM org.apache.catalina.startup.Catalina load
INFOS: Initialization processed in 8010 ms
févr. 28, 2017 10:56:09 AM org.apache.catalina.startup.Catalina start
INFOS: Server startup in 2389 ms

1 个答案:

答案 0 :(得分:0)

你做错了是签署任意未处理的文本,而不是提交文档的规范版本(标签中没有空格,但也有排序的属性,同一类型的引号等)到数字签名计算

The Canonical XMLExclusive Canonical XML W3C建议指定了消除任意差异的标准和全面方法。