Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the xml class name using fasterxml jackson?

I am trying to figure out how to change the root node name using jackson fasterxml.

For example:

public class Car {
    @JsonProperty("engine-type") 
    String engineType = "v8";
}

public class Ford extends Car {
}

Ford car = new Ford();
ObjectMapper xmlMapper = new XmlMapper();
System.out.println(xmlMapper.writeValueAsString(this));

results in:

<Ford><engine-type>v8</engine-type></Ford>

This is what I want:

  1. The root node to be named car.
  2. I want Car to be lowercase in the xml:

For example:

<car><engine-type>v8</engine-type></car>

Thanks

like image 814
Scott David Murphy Avatar asked Jul 18 '15 00:07

Scott David Murphy


People also ask

Can Jackson parse XML?

Jackson is a library for handling JSON in Java systems and now has support for XML from version 2. DOM4J is a memory-efficient library for parsing XML, XPath, and XSLT (eXtensible Stylesheet Language).

What is Jackson Dataformat XML?

Package. Description. com.fasterxml.jackson.dataformat.xml. Package that contains XML-based backends which can serialize POJOs to and deserialize from XML, using Stax XML parsers and generators for XML processing and mostly standard Jackson data binding otherwise.

Can ObjectMapper be used for XML?

Reading XML We can also read XML, using the various readValue APIs that are part of provided by the ObjectMapper. For example, reading some XML from an InputStream into a Java Bean: MyBean bean = objectMapper.


1 Answers

I think you could find your solution here: How to deserialize XML with annotations using FasterXML Why don't you use @JacksonXmlRootElement like:

@JacksonXmlRootElement(localName = "car")
public class Ford extends Car {
}
like image 180
Thánh Ma Avatar answered Sep 22 '22 00:09

Thánh Ma