Discuss / Java / 这种方式怎么能获取到属性?

这种方式怎么能获取到属性?

Topic source

shadowTy

#1 Created at ... [Delete] [Delete and Lock User]

isbn的属性lang

夢夢燈籠

#2 Created at ... [Delete] [Delete and Lock User]

@JacksonXmlProperty

这样就可以获取isbn的属性lang的值了。

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;

public class Book {

    public long id;

    public String name;

    public String author;

    @JacksonXmlProperty(localName="isbn")

    public BookAttr isbn;

    public List<String> tags;

    public String pubDate;

}

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlText;

public class BookAttr {

    @JacksonXmlProperty(isAttribute=true, localName="lang")

    public String lang;

    @JacksonXmlText

    public String value;

}

public class Jackson {

    public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {

        InputStream input = Jackson.class.getResourceAsStream("/jcakbook.xml");

        JacksonXmlModule module = new JacksonXmlModule();

        // to default to using "unwrapped" Lists:

        //module.setDefaultUseWrapper(false);

        XmlMapper mapper = new XmlMapper(module);

        //自动忽略无法对应pojo的字段

        //mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        //字段为null,自动忽略,不再序列化

        //mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

        //XML标签名:使用骆驼命名的属性名,  

        //mapper.setPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE);

        //设置转换模式

        //mapper.enable(MapperFeature.USE_STD_BEAN_NAMING);

        Book book = mapper.readValue(input, Book.class);

        System.out.println(book.id);

        System.out.println(book.name);

        System.out.println(book.author);

        System.out.println(book.isbn);

        System.out.println("节点isbn的值为:" + book.isbn.value);

        System.out.println("节点isbn的属性lang的值为:" + book.isbn.lang);

        System.out.println(book.tags);

        System.out.println(book.pubDate);

    }

}


  • 1

Reply