Windows上的Marshaller在文件末尾添加了新行

时间:2016-04-11 15:27:25

标签: java xml windows unix jaxb

我有一个使用JAXB编组的XML文件的项目,以便比较不同环境的配置状态。我注意到Windows下针对Unix版本的JAXB编组器的实现一定存在一些差异。当我比较在不同平台上创建的2个文件时,我的比较工具总是标记文件末尾的一个差异。在Windows上创建的文件在文件末尾有一个新行(CR和LF),而Unix版本没有。

请注意,问题不是两个平台之间新行字符的差异! Windows marshaller在文件末尾有效添加“新行”,而Unix marshaller在关闭“>”后停止根标签。

Windows marshaller adds a new line at the end

是否有任何参数我可以传递给编组器以防止这个额外的行,或者我必须在Windows上进行编组后明确删除它,以便我的比较工具不会标记差异?

这是编组代码的样子:

  public void marshal(final Object rootObject, final OutputStream outputStream) throws JAXBException, TransformerException {
    Preconditions.checkArgument(rootObject != null, "rootObject must not be null");
    Preconditions.checkArgument(outputStream != null, "outputStream must not be null");
    final JAXBContext ctx = JAXBContext.newInstance(rootObject.getClass());
    final Document document = getFactories().newDocument();
    document.setXmlStandalone(true);
    final Marshaller marshaller = ctx.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.setSchema(schema);
    marshaller.marshal(rootObject, document);
    createTransformer().transform(new DOMSource(document), new StreamResult(outputStream));
  }

  public static Transformer createTransformer() {
    final Transformer transformer = getFactories().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, JAXBDefaults.OUTPUT_CHARSET.name());
    transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, CDATA_XML_ELEMENTS);
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", IDENT_LENGTH);
    return transformer;
  }


  private static class JAXBFactories {

    private DocumentBuilderFactory documentBuilderFactory;

    public DocumentBuilderFactory getDocumentBuilderFactory() {
      if (documentBuilderFactory == null) {
        documentBuilderFactory = DocumentBuilderFactory.newInstance();
      }
      return documentBuilderFactory;
    }

    private DocumentBuilder documentBuilder;

    public DocumentBuilder getDocumentBuilder() {
      if (documentBuilder == null) {
        try {
          documentBuilder = getDocumentBuilderFactory().newDocumentBuilder();
        } catch (final ParserConfigurationException ex) {
          throw new RuntimeException("Failed to create DocumentBuilder", ex);
        }
      }
      return documentBuilder;
    }

    public Document newDocument() {
      return getDocumentBuilder().newDocument();
    }

    private TransformerFactory transformerFactory;

    public TransformerFactory getTransformerFactory() {
      if (transformerFactory == null) {
        transformerFactory = TransformerFactory.newInstance();
      }
      return transformerFactory;
    }

    public Transformer newTransformer() {
      try {
        return getTransformerFactory().newTransformer();
      } catch (final TransformerConfigurationException ex) {
        throw new RuntimeException("Failed to create Transformer", ex);
      }
    }

  }

  private static class FactoriesHolder {

    static final JAXBFactories FACTORIES = new JAXBFactories();
  }

  private static JAXBFactories getFactories() {
    return FactoriesHolder.FACTORIES;
  }

1 个答案:

答案 0 :(得分:1)

没有理由(或期望)漂亮的打印XML将从两个不同的系统产生完全相同的结果。但是,如果您关闭漂亮的打印(并让IDE /编辑器执行此操作),您可能会发现输出相同。

漂亮的打印XML是原始的变换,增加了布局。它不再是真正的 xml。