java - 如何仅从 xml 文件中删除 encoding=UTF8
问题描述
public static void saveXMLDocument(Document xodrDoc, String absoluteFileName) throws Exception{
//write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
DOMSource source = new DOMSource(xodrDoc);
StreamResult result = new StreamResult(new File(absoluteFileName));
transformer.transform(source, result);
}
所以当我尝试只删除“编码”属性时,这就是我的输出文件的样子:
<OpenDRIVE>
<header date="Wed May 23 12:21:34 2018" east="0.0000000000000000e+00" name="" north="0.0000000000000000e+00" revMajor="1" revMinor="4" south="0.0000000000000000e+00" version="1.00" west="0.0000000000000000e+00">
</header>
<road id="7" junction="-1" length="1.0000000000000000e+03" name="">
<link>
</link>
<planView>
<geometry hdg="0.0000000000000000e+00" length="1.0000000000000000e+01" s="0.0000000000000000e+00" x="0.0000000000000000e+00" y="0.0000000000000000e+00">
<line/>
</geometry>
<geometry hdg="0.0000000000000000e+00" length="22" s="1.0000000000000000e+01" x="1.0000087425259599e+01" y="0.0000000000000000e+00">
<arc curvature="0.10"/>
</geometry>
</planView>
输出应该是什么样的:
<?xml version="1.0" standalone="yes"?>
<OpenDRIVE>
<header revMajor="1" revMinor="4" name="" version="1.00" date="Wed May 23 12:21:34 2018" north="0.0000000000000000e+00" south="0.0000000000000000e+00" east="0.0000000000000000e+00" west="0.0000000000000000e+00">
</header>
<road name="" length="1.0000000000000000e+03" id="7" junction="-1">
<link>
</link>
<planView>
<geometry s="0.0000000000000000e+00" x="0.0000000000000000e+00" y="0.0000000000000000e+00" hdg="0.0000000000000000e+00" length="1.0000000000000000e+01">
<line/>
</geometry>
<geometry s="1.0000000000000000e+01" x="1.0000087425259599e+01" y="0.0000000000000000e+00" hdg="0.0000000000000000e+00" length="9.9000000000000000e+02">
<arc curvature="5.0000000000000001e-04"/>
</geometry>
</planView>
所以我知道我不应该删除编码部分,但这对我来说很重要,否则我无法使用此文件,因为我使用特殊工具进行加载。预期的输出是我在程序中加载的模式。再次保存后,我得到了当前输出。
xml 文件必须包含独立属性,但不能包含编码部分。
解决方案
您可以利用ProcessingInstruction
它来配置您想要附加的任何标签。
在此示例中,我将使用您在案例中指定的自定义数据添加标签“xml”。
ProcessingInstruction newPI = xodrDoc.createProcessingInstruction("xml", "version=\"1.0\" standard=\"yes\"");
xodrDoc.insertBefore(newPI, xodrDoc.getDocumentElement());
我创建了一个示例类来测试添加自定义 xml 标头。
public class SaveXmlWithCustomXmlHeader {
public static void main(String[] args) throws Exception {
String xmlString = "<a><b></b><c></c></a>";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
Document document = null;
try {
builder = factory.newDocumentBuilder();
document = builder.parse(new InputSource(new StringReader(xmlString)));
} catch (Exception e) {
e.printStackTrace();
}
saveXMLDocument(document, "hello.xml");
}
public static void saveXMLDocument(Document xodrDoc, String absoluteFileName) throws Exception {
// add custom xml tag
ProcessingInstruction newPI = xodrDoc.createProcessingInstruction("xml", "version=\"1.0\" standard=\"yes\"");
xodrDoc.insertBefore(newPI, xodrDoc.getDocumentElement());
//write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(xodrDoc);
StreamResult result = new StreamResult(new File(absoluteFileName));
transformer.transform(source, result);
}
}
hello.xml 的输出:
<?xml version="1.0" standard="yes"?><a>
<b/>
<c/>
</a>
更新:在写入文件
时使用似乎存在问题ProcessingInstruction
,因为它没有正确缩进其余 xml 数据中的 xml 标头。
通常,无论如何处理 xml 都不会改变,因为它只是人类可读的东西,而不是技术问题。
但对于那些关心的人,这里是替代方案ProcessingInstruction
:
public static void saveXMLDocument(Document xodrDoc, String absoluteFileName) throws Exception {
// add custom xml tag
//write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
StringWriter writer = new StringWriter();
writer.append("<?xml version=\"1.0\" standalone=\"yes\"?>").append("\n");
final DOMSource source = new DOMSource(xodrDoc);
transformer.transform(source, new StreamResult(writer));
try (BufferedWriter bufferedWriter = Files.newBufferedWriter(Paths.get(absoluteFileName))) {
bufferedWriter.write(writer.toString());
}
}
推荐阅读
- mysql - 如何在 MySQL 中以三个一组的形式显示行
- angularjs - 为什么我收到 RangeError: Maximum call stack size exceeded 观察对父范围变量的更改(过滤的 ng-repeat 数组)?
- c# - “由于意外的数据包格式,握手失败”连接到 SFTP 服务器时出现 FluentFTP 错误
- javascript - tsconfig.json 中的“模块”:“esnext”没有删除动态延迟加载模块错误
- python - 将一个熊猫系列除以另一个并忽略索引
- ms-access - 在查询表达式中无效使用 '.', ',' '()'
- zabbix - 使用 Zabbix 网络监控验证 pdf 的内容
- javascript - 以角度停止连续被调用的函数
- c# - 如何在 C# 中将 0.01 舍入为 0.01
- r - 如何从dendextend到consesnus树分析获得单独制作的树状图?