Java Transformer、TransformerFactory 搞乱了多行 xml 注释
我在使用 Transformer/TransformerFactory 更新现有 XML 文件(在 Wowza 服务器上)时遇到了一些麻烦。它工作得很好并且更新正确,但是它使多行注释变得混乱。
注释一开始看起来像这样:
<!--StorageDir path variables
${com.wowza.wms.AppHome} - Application home directory
${com.wowza.wms.ConfigHome} - Configuration home directory
${com.wowza.wms.context.VHost} - Virtual host name
${com.wowza.wms.context.VHostConfigHome} - Virtual host config directory
${com.wowza.wms.context.Application} - Application name
${com.wowza.wms.context.ApplicationInstance} - Application instance name
-->
转换后,它们最终都在一行中,其中包含方形字符,其中回车符曾经在此处(不会在此处发布结果,因为方形不显示,并且换行使得最终结果看起来与上面的块非常相似)。
这是我用来进行 xml 更新的代码(只是一个片段):
TransformerFactory tFactory = TransformerFactory.newInstance();
tFactory.setAttribute("indent-number", new Integer(4));
Transformer transformer = tFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(document);
File outFile = new File(path.getPath() + "/Application.xml");
FileOutputStream outputStream = new FileOutputStream(outFile);
StreamResult result = new StreamResult(new OutputStreamWriter(outputStream));
transformer.transform(source, result);
我需要保持这些多行注释清晰且格式正确,因为这些 xml 文件有时需要手动编辑,并且注释用作指南和提醒。
有什么意见吗?我怎样才能让变形金刚停止破坏我的评论?非常感谢! :)
编辑: 这就是文档最初创建的方式(其中 appXML 是要编辑的 Application.xml 文件的路径):
Document document;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(appXML);
之后,我制作了很多特定于节点的文件更改...总共太多,无法将它们全部放在这里,但所有更改都遵循以下格式:
更改节点:
Node streamType = document.getElementsByTagName("StreamType").item(0);
streamType.setTextContent(mountType);
附加子元素
Node nameNode = document.createElement("Name");
nameNode.appendChild(document.createTextNode("utilities"));
Node module = document.createElement("Module");
module.appendChild(nameNode);
Element modules = (Element)document.getElementsByTagName("Modules").item(0);
modules.appendChild(module);
希望信息有帮助!
I'm having a bit of trouble using Transformer/TransformerFactory to update an existing XML file (on a Wowza server). It works great and updates correctly, but it makes a mess of multiline comments.
The comments start out looking like this:
<!--StorageDir path variables
${com.wowza.wms.AppHome} - Application home directory
${com.wowza.wms.ConfigHome} - Configuration home directory
${com.wowza.wms.context.VHost} - Virtual host name
${com.wowza.wms.context.VHostConfigHome} - Virtual host config directory
${com.wowza.wms.context.Application} - Application name
${com.wowza.wms.context.ApplicationInstance} - Application instance name
-->
After the transform, they end up all in one line with square characters where the return carriages used to be (won't post the result here because the squares don't show, and the line wrap makes the end result look pretty much like the above block).
This is the code I use to make the xml updates (just a snippet):
TransformerFactory tFactory = TransformerFactory.newInstance();
tFactory.setAttribute("indent-number", new Integer(4));
Transformer transformer = tFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(document);
File outFile = new File(path.getPath() + "/Application.xml");
FileOutputStream outputStream = new FileOutputStream(outFile);
StreamResult result = new StreamResult(new OutputStreamWriter(outputStream));
transformer.transform(source, result);
I need to keep these multiline comments legible and correctly formatted because these xml files need to be edited by hand sometimes, and the comments are used as guides and reminders.
Any input? How might I get the transformers to stop mangling my comments? Thanks very much! :)
EDIT: This is how the document is initially created (where appXML is the path of the Application.xml file that is going to be edited):
Document document;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(appXML);
After that, I make a lot of node-specific changes... too many in total to put them all here, but there are the formats all of the changes follow:
Changing a node:
Node streamType = document.getElementsByTagName("StreamType").item(0);
streamType.setTextContent(mountType);
Appending a child element
Node nameNode = document.createElement("Name");
nameNode.appendChild(document.createTextNode("utilities"));
Node module = document.createElement("Module");
module.appendChild(nameNode);
Element modules = (Element)document.getElementsByTagName("Modules").item(0);
modules.appendChild(module);
Hope that info helps!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论