[英]Can I maintain xml format except a changed target like below or maintain a original format?
我想在更改某些属性值后保持原始xml格式。 除更改属性外,如何维护格式?
我使用java8,apache9和日食氧气。
例如,我的示例XML是
<a>
<b id="123"
name="456"
value="789">
<c id="awe"
name="try">
</a>
我使用这样的代码。
Document doc = getConfigDocument();
NodeList nl = doc.getElementsByTagName("serverInfo");
Element oasisElement = (Element) doc.getElementsByTagName("b").item(0);
oasisElement.setAttribute("id", "1231");
Transformer tr;
try
{
tr = TransformerFactory.newInstance().newTransformer();
tr.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tr.setOutputProperty(OutputKeys.INDENT, "yes");
tr.transform(new DOMSource(doc), new StreamResult(new File(getConfigRoot())));
resultMsg = "success";
}
在此代码之后,XMl将被重写,并像这样更改格式。
<a>
<b id="1231" name="456" value="789">
<c id="awe" name="try">
</a>
我可以维护xml格式,但不能更改如下所示的目标,还是维护原始格式?
<a>
<b id="123" name="456" value="789">
<c id="awe"
name="try">
</a>
否,XML解析器不会向应用程序报告详细的布局,因此该应用程序(在本例中为XSLT处理器)无法保留原始布局。
但是,如果输入格式遵循系统规则,则可以使序列化程序(处理XSLT转换的结果)遵循相同的规则。 如果您使用Saxon序列化程序而不是内置的JDK(Xalan)序列化程序,则可以通过指定序列化属性来获得与输入格式非常接近的内容:
indent=yes
saxon:indent-spaces=1
saxon:line-length=10
saxon:attribute-order="id name value"
为此,您需要Saxon-PE或更高。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.