使用builder.parse方法&amp ;;保留xml中的换行符。变压器

时间:2015-01-12 19:14:05

标签: java xml javax.xml

目标是从xml文件中读取并写入新的xml文件,同时保留换行符。我们需要Document对象来执行其他xml任务。

假设source.xml如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <Code><![CDATA[code line1
code line 2
code line 3

code line 4]]></Code>

现在,目标应该与代码元素中的换行符相同。但相反,它忽略了换行符并使其成为一行。

写作时,我使用以下方法:

public static void writeFile(Document xml, File writeTo)
    {
        try
        {
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); 
            DOMSource source = new DOMSource(xml);
            StreamResult result = new StreamResult(writeTo);
            transformer.transform(source, result);
        }
        catch(TransformerException e)
        {
            System.out.println("Couldn't write file " + writeTo);
            e.printStackTrace();
        }
    }

使用DocumentBuilder中的Parse(File)方法获取Document xml。粗略地说:

File file; // a list of files is recursively obtained from a given folder.

DocumentBuilderFactory documentBuilderfactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = documentBuilderfactory.newDocumentBuilder();
Document xml = builder.parse(file);

builder.parse似乎丢失了Code元素的CDATA中的换行符。

我们如何保留换行符? 我是Java API的新手。

1 个答案:

答案 0 :(得分:1)

当我把你的片段放在一起时,我得到了这个程序:

public class TestNewLine {

    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, TransformerException {
        DocumentBuilderFactory documentBuilderfactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = documentBuilderfactory.newDocumentBuilder();
        Document xml = builder.parse(TestNewLine.class.getResourceAsStream("data.xml"));
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        DOMSource source = new DOMSource(xml);
        StreamResult result = new StreamResult(System.out);
        transformer.transform(source, result);
    }
}

并打印出来:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Code><![CDATA[code line1
code line 2
code line 3

code line 4]]></Code>

据我了解,新行已经保留。您期望得到什么输出?