Java Doc Transformer重新排列xml

时间:2019-02-25 19:46:32

标签: java xml xml-document-transform

在Java中,我已经阅读了一个xml文件,添加了属性和值,然后将xml写回到文件中。

我遇到的问题是文档转换器正在重新排列我的xml。

示例:

原始

   <Valve lassName="AccessLogValveAdapter" directory="access_logs"  prefix="localhost." suffix=".log" pattern='%v %h %t "%r" %s %B %T "%{User-Agent}i"'/>

已修改

   <Valve className="AccessLogValveAdapter" directory="access_logs" pattern="%v %h %t "%r" %s %B %T "%{User-Agent}i"" prefix="localhost." suffix=".log"/>

这是我的代码:

    boolean returnValue = false;
    String methodName = "docTransFormer() ";
    File file = new File(filePath);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = null;
    try
    {
        transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);

        StreamResult result = new StreamResult(file.getPath());
        transformer.transform(source, result);
        returnValue = true;
    }
    catch (TransformerConfigurationException e)
    {
        logger.info(EXCEPTIONCAUGHT + methodName + e.getMessage());
    }
    catch (TransformerException e)
    {
        logger.info(EXCEPTIONCAUGHT + methodName + e.getMessage());
    }
    return returnValue;
}

有什么想法可以将其保留为原始格式吗?

0 个答案:

没有答案