Jackson XML-使用名称空间前缀反序列化XML [英] Jackson XML - deserializing XML with namespace prefixes

查看:615
本文介绍了Jackson XML-使用名称空间前缀反序列化XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jackson XML插件( https://github.com/FasterXML/jackson-dataformat-xml ),我不确定是否支持它,但是我想知道是否可以同时使用名称空间前缀对XML进行序列化和反序列化,例如:

I'm working with the Jackson XML plugin (https://github.com/FasterXML/jackson-dataformat-xml), and I'm not sure if it's supported, but I'm wondering if it's possible to both serialize and deserialize XML with namespace prefixes, like so:

<name:Foo>
  <name:Bar>
    <name:x>x</name:x>
    <name:y>y</name:y>
  </name:Bar>
</name:Foo>

我可以使用Jackson的插件生成此类XML,如下所示:

I can generate this type of XML using Jackson's plugin like so:

@JacksonXmlProperty(localName="name:Bar")
public Bar getBar() {
    return bar;
}

但是,我找不到一种方法来配置POJO以从生成的XML反序列化.请参见以下示例代码:

However, I can't find a way to configure my POJOs to deserialize from the XML generated. Please see the following example code:

public class Bar{
    @JacksonXmlProperty(localName="name:x")
    public String x = "x";
    @JacksonXmlProperty(localName="name:y")
    public String y = "y";
}


@JacksonXmlRootElement(localName="name:Foo")
    public class Foo{
        private Bar bar;

        @JacksonXmlProperty(localName="name:Bar")
        public Bar getBar() {
           return bar;
        }

        public void setBar(Bar bar) {
            this.bar = bar;
        }   
}


public class TestDeserialization {

    public static void main(String[] args) throws Exception {
        Foo foo = new Foo();
        foo.setBar(new Bar());

        XmlMapper xmlMapper = new XmlMapper();
        String xml = xmlMapper.writerWithDefaultPrettyPrinter().writeValueAsString(foo);
        System.out.println(xml);
        System.out.println("XML Desearialzing....");
        Foo foo2= xmlMapper.readValue(xml, Foo.class);
        System.out.println(xmlMapper.writeValueAsString(foo2));
     }
}

尝试运行此测试会给我一个例外:

Trying to run this test gives me an exception:

Exception in thread "main" java.io.IOException: com.ctc.wstx.exc.WstxParsingException: Undeclared namespace prefix "name"

这是可以理解的,但是我想知道是否有一种方法可以使它与Jackson XML一起使用?

which is understandable, but I was wondering if there's a way to get this to work with Jackson XML?

推荐答案

JacksonXmlProperty批注具有属性namespace.用它来定义namespace

JacksonXmlProperty annotation has property namespace. Use it for defining namespace

public class Bar {
    @JacksonXmlProperty(namespace = "name",localName="x")
    public String x = "x";
    @JacksonXmlProperty(namespace = "name",localName="y")
    public String y = "y";
} 

@JacksonXmlRootElement(namespace = "name", localName = "Foo")
public class Foo {
   private Bar bar;

   @JacksonXmlProperty(namespace = "name", localName = "Bar")
   public Bar getBar() {
      return bar;
   }

   public void setBar(Bar bar) {
      this.bar = bar;
   }
}

这篇关于Jackson XML-使用名称空间前缀反序列化XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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