从Java中的XML中删除XML标头 [英] Remove the XML header from an XML in Java

查看:800
本文介绍了从Java中的XML中删除XML标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆