从Java中的XML中删除XML标头 [英] Remove the XML header from an XML in Java
问题描述
StringWriter writer = new StringWriter();
XmlSerializer serializer = new KXmlSerializer();
serializer.setOutput(writer);
serializer.startDocument(null, null);
serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
// Creating XML
serializer.endDocument();
String xmlString = writer.toString();
在上述环境中,是否有任何标准API可用于删除XML标头<?xml version ='1.0'?>
或者你建议通过字符串操作:
In the above environment, whether there are any standard API's available to remove the XML header <?xml version='1.0' ?>
or do you suggest to go via string manipulation:
if (s.startsWith("<?xml ")) {
s = s.substring(s.indexOf("?>") + 2);
}
想要输出 xmlString
没有XML标题信息<?xml version ='1.0'?>
。
Wanted the output in the xmlString
without XML header info <?xml version='1.0' ?>
.
推荐答案
如果使用JAXP序列化程序,则可以访问XSLT中定义的所有输出属性,例如 omit-xml-declaration =yes
。您可以使用身份变换器的形式获取此信息,使用 transformerFactory.getTransformer()
调用,不带参数,然后调用 setOutputProperty ()
。
If you use a JAXP serializer you get access to all the output properties defined in XSLT, for example omit-xml-declaration="yes"
. You can get this in the form of an "identity transformer", called using transformerFactory.getTransformer()
with no parameters, on which you then call setOutputProperty()
.
这篇关于从Java中的XML中删除XML标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!