Java #xD;和#13;在读写XML文件时

Java #xD;和#13;在读写XML文件时,java,xml,dom,stax,Java,Xml,Dom,Stax,我有一个来自网络API的XML文件输入。当我试图从浏览器中将其保存为XML文件时,它有一些多余的 在其中。问题是,当试图通过StaX解析这个XML数据,并在处理后执行一些任务,将其写回另一种XML格式(如DOM)时,它有 取而代之 我想做的就是避免这些多余的 来自输入和 从输出。 找不到这些背后的原因,也找不到解决办法 这是我保存到文件后在输入XML元素值中得到的 写入后,输出 <someNode>Today is a fine day. So does e

我有一个来自网络API的XML文件输入。当我试图从浏览器中将其保存为XML文件时,它有一些多余的

在其中。问题是,当试图通过StaX解析这个XML数据,并在处理后执行一些任务,将其写回另一种XML格式(如DOM)时,它有

取而代之

我想做的就是避免这些多余的

来自输入和

从输出。
找不到这些背后的原因,也找不到解决办法

这是我保存到文件后在输入XML元素值中得到的

写入后,输出

<someNode>Today is a fine day.

So does everyday.
</someNode>
实际预期和要求的输出

<someNode>Today is a fine day.

So does everyday.
</someNode>
在过滤和处理原始数据后编写新的XML结构文件

// After processing and writing new Element structure to org.w3c.dom.Document
// write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer tr = transformerFactory.newTransformer();
tr.setOutputProperty(OutputKeys.INDENT, "yes");
tr.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
tr.setOutputProperty(OutputKeys.METHOD, "xml");
tr.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tr.setOutputProperty(OutputKeys.STANDALONE, "no");

DOMSource source = new DOMSource(doc);
File file = new File(xmlFilePath);
Writer outputStream = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
StreamResult result = new StreamResult(outputStream);
tr.transform(source, result);
不知道我到底错过了什么。但是任何建议或帮助都是很好的。

最简单的解决方案(除了挂接到SAX事件流之外)是编写一个XSLT脚本,它完全满足您的需要,并将其作为您的转换器而不是默认的标识转换器调用

有关建议,请参阅


然后,您需要为转换文本节点提供自己的规则,通过将ASCII 13个字符转换为空字符串来删除它们。有关详细信息,请参阅。

看起来像是写在Windows上的system@Thorbjørnravandersen:是的,我正在用Netbeans开发一个windows系统。原始的XML被破坏了。ASCII 13不应该在其中。@ThorbjørnRavnAndersen:你的意思是这是API输出本身的错误,我应该向API的开发人员报告吗?在API输出文件中,它是 ;这是不对的。但我无法控制它。我必须避免写 ;在我的输出XML中。这就是问题所在。是的,就是这样,速度更快,效率也更高。现在我已经把它作为一个bug报告给了开发者。如果没有解决,我们将为您的解决方案。哪一个BTW正在工作。
// Get Input XML stream from API
URL apiURL = new URL(API_Url);
HttpsURLConnection httpsAPIURLConn;
httpsAPIURLConn = (HttpsURLConnection) apiURL.openConnection();
httpsAPIURLConn.setConnectTimeout(10000); // timeout
httpsAPIURLConn.setDoInput(true);
InputStream inStream = httpsAPIURLConn.getInputStream();

// Data stream okay, Start StaX XLIFF reader
XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
// This is to read entity referenced strings
xmlInputFactory.setProperty(XMLInputFactory.IS_COALESCING, true);

// StaX StreamReader
XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(new BufferedInputStream(inStream), "UTF-8");

// Read and load XML data to in-memory database to filter and process
// After processing and writing new Element structure to org.w3c.dom.Document
// write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer tr = transformerFactory.newTransformer();
tr.setOutputProperty(OutputKeys.INDENT, "yes");
tr.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
tr.setOutputProperty(OutputKeys.METHOD, "xml");
tr.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tr.setOutputProperty(OutputKeys.STANDALONE, "no");

DOMSource source = new DOMSource(doc);
File file = new File(xmlFilePath);
Writer outputStream = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
StreamResult result = new StreamResult(outputStream);
tr.transform(source, result);