Pretty printing XML with Jackson library Pretty printing XML with Jackson library xml xml

Pretty printing XML with Jackson library


According to the Stax2WriterAdapter's documentation thier default implementation is incomplete. Also comments in the code say that the writeRaw() method cannot be implemented using Stax 1.0 which is default for Java runtime.

You should switch to use a Stax2 library as suggested on this wiki page.

After I added the following Maven dependency to my project, the example below started to work as expected:

<dependency>    <groupId>org.codehaus.woodstox</groupId>    <artifactId>woodstox-core-asl</artifactId>    <version>4.1.4</version></dependency>

Code example:

public class JacksonXmlMapper {    public static class Person {        final public String name;        public Person(String name) {            this.name = name;        }    }    public static void main(String[] args) throws JsonProcessingException {        ObjectMapper mapper = new XmlMapper();        mapper.enable(SerializationFeature.INDENT_OUTPUT);        System.out.println(mapper.writeValueAsString(new Person("John")));    }}

Output:

<Person>   <name>John</name></Person>


I have used the below dependencies to sort the above said problem. Here is the list of those:

1) jackson-annotations-2.9.8.jar

2) jackson-core-2.9.8.jar

3) jackson-databind-2.9.8.jar

4) jackson-dataformat-xml-2.9.8.jar

5) jackson-module-jaxb-annotations-2.9.8.jar

6) stax2-api-4.0.0.jar

7) woodstox-core-asl-4.4.1.jar

enter image description here

@Urosh T: content updated.


Its caused because of the java.lang.UnsupportedOperationException:

if you implement the unimplemented method, your code should be working fine. for futher insights about the exception, please check this

("java.lang.UnsupportedOperationException: Not supported yet.").

Thanks!!