Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用builder.parse方法时在xml中保留换行符&;变压器_Java_Xml_Javax.xml - Fatal编程技术网

Java 使用builder.parse方法时在xml中保留换行符&;变压器

Java 使用builder.parse方法时在xml中保留换行符&;变压器,java,xml,javax.xml,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> 文档xml是使用DocumentB

目标是在保留换行符的同时,从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>
文档xml是使用DocumentBuilder中的Parse(File)方法获得的。大致如下:

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似乎丢失了代码元素CDATA中的换行符

我们如何保存新线?
我不熟悉Java API。

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

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>



据我所知,这条新线已经保留了下来。您希望得到什么输出?

如何获取
文档
对象?您是否可以发布创建它的代码并将其传递给
writeFile
?您是否需要将其作为XML而不是纯文本读取?如果您不需要将其视为XML,您应该可以直接读取。@manouti我更新了关于如何获取文档对象的问题。@BrianJ没有特别的原因,我在网上找到了它。我不熟悉Java API。@dushyantp我试图用简单的代码和输入XML重现这个问题,但没有成功:我复制了问题中的XML,但在输出中它保持不变,只是删除了元素名称前的一个小缩进。是否正在使用XSLT文件?为什么文件只是按原样复制?也许我在问题中遗漏了什么。抱歉,检查后,发现我问错了问题。CDATA中的换行由于DocumentBuilder解析方法而丢失。写入即转换方法很好。我将修改这个问题,并检查我们是否能找到解决方案。
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Code><![CDATA[code line1
code line 2
code line 3

code line 4]]></Code>