简体   繁体   English

如何从 jackson Xml 解析中删除命名空间定义

[英]How to remove namespace definition from jackson Xml parsing

I'm using Jackson data format for serializing Pojos as XML.我使用 Jackson 数据格式将 Pojos 序列化为 XML。

It is working fine but I would like to remove the namespace definition:它工作正常,但我想删除命名空间定义:

@JacksonXmlRootElement(localName="simple_something")
public class Simple {
    public int x = 1;
    public int y = 2;
}

I do:我愿意:

ObjectMapper xmlMapper = new XmlMapper();
String xml = xmlMapper.writeValueAsString(new Simple());

I get:我得到:

<simple_something xmlns="">
  <x>1</x>
  <y>2</y>
</simple_something>

but I would like to remove the xmlns=""但我想删除 xmlns=""

and that it looks like它看起来像

<simple_something>
  <x>1</x>
  <y>2</y>
</simple_something>

Any ideas?有任何想法吗?

Make sure to use Woodstox Stax implementation, and not Stax implementation Oracle bundles with JDK.确保使用Woodstox Stax 实现,而不是 Stax 实现 Oracle 与 JDK 捆绑。 This is usually done by adding Maven dependency to explicitly include woodstox jar.这通常是通过添加 Maven 依赖项来显式包含 woodstox jar 来完成的。 This is explained on XML module README at https://github.com/FasterXML/jackson-dataformat-xml/这在https://github.com/FasterXML/jackson-dataformat-xml/ 的XML 模块自述文件中进行了解释

Oracle's implemention adds that declaration in namespace-repairing mode for some reason.由于某种原因,Oracle 的实现在命名空间修复模式中添加了该声明。 It is also slower and has more bugs, so there's not much reason to rely on it, unless you really want to minimize external dependencies.它也更慢并且有更多的错误,所以没有太多理由依赖它,除非你真的想最小化外部依赖。

Also note that that namespace declaration is completely benign, so while unnecessary it is legal XML.另请注意,命名空间声明是完全无害的,因此虽然不必要,但它是合法的 XML。 So while it is eyesore all xml tools should work just fine with such extra declarations.因此,虽然它令人眼花缭乱,但所有 xml 工具都应该可以很好地处理这些额外的声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM