Transformer/TransformerFactory を使用して (Wowza サーバー上の) 既存の XML ファイルを更新するのに少し問題があります。それはうまく機能し、正しく更新されますが、複数行のコメントが混乱します。
コメントは次のように始まります。
<!--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
-->
変換後、それらはすべて、改行があった四角形の文字で 1 行にまとめられます (四角形が表示されないため、ここでは結果を投稿しません。また、行の折り返しにより、最終結果は次のようになります。上ブロック)。
これは、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);
その情報が役立つことを願っています!