Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (102)
JavaBeans (21)
JDBC (121)
JDK (426)
JSP (20)
Logging (108)
Mail (58)
Messaging (8)
Network (84)
PDF (97)
Report (7)
Scripting (84)
Security (32)
Server (121)
Servlet (26)
SOAP (24)
Testing (54)
Web (15)
XML (322)
Collections:
Other Resources:
com.fasterxml.jackson.dataformat.xml.XmlMapper Example
How to use com.fasterxml.jackson.dataformat.xml.XmlMapper class?
✍: FYIcenter.com
com.fasterxml.jackson.dataformat.xml.XmlMapper class allows you to map a Java class object to a JSON message in a pretty format.
You can follow this tutorial to try it.
1. Write a sample Java program, XmlMapperWriter.java:
// XmlMapperWriter.java
// Copyright (c) FYIcenter.com
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
public class XmlMapperWriter {
public static void main(String[] args) throws Exception {
Person person = new Person();
ObjectMapper mapper = new XmlMapper();
String xml = mapper.writeValueAsString(person);
System.out.println(xml);
}
public static class Person {
private String name = "John Smith";
private boolean married = false;
private int age = 25;
private String phone = null;
public String getName() {
return this.name;
}
public boolean getMarried() {
return this.married;
}
public int getAge() {
return this.age;
}
public String getPhone() {
return this.phone;
}
}
}
2. Run this Java program with 4 Jackson JAR files. Too bad. It requires the org/codehaus/stax2/XMLInputFactory2 class.
fyicenter$ java -cp jackson-core-2.14.0.jar: \ jackson-databind-2.14.0.jar: \ jackson-annotations-2.14.0.jar \ jackson-dataformat-xml-2.14.0.jar \ XmlMapperWriter.java Exception in thread "main" java.lang.NoClassDefFoundError: org/codehaus/stax2/XMLInputFactory2 ... at java.base/java.util.ServiceLoader$3.hasNext(ServiceLoader.java:1386) at java.xml/javax.xml.stream.FactoryFinder$1.run(FactoryFinder.java:348) ...
Go to Stax2 Maven Website. Download stax2-api-4.0.0.jar and included it in the classpath. Too bad. It requires another class.
fyicenter$ java -cp jackson-core-2.14.0.jar: \ jackson-databind-2.14.0.jar: \ jackson-annotations-2.14.0.jar \ jackson-dataformat-xml-2.14.0.jar \ stax2-api-4.0.0.jar \ XmlMapperWriter.java Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/module/jaxb/JaxbAnnotationIntrospector ... at com.fasterxml.jackson.dataformat.xml.util.AnnotationUtil.findNamespaceAnnotation at com.fasterxml.jackson.dataformat.xml.ser.XmlBeanSerializerModifier.changeProperties at com.fasterxml.jackson.databind.ser.BeanSerializerFactory.constructBeanOrAddOnSerializer ...
Go to Jackson Maven Website. Download jackson-module-jaxb-annotations-2.14.0.jar and included it in the classpath.
fyicenter$ java -cp jackson-core-2.14.0.jar: \ jackson-databind-2.14.0.jar: \ jackson-annotations-2.14.0.jar \ jackson-dataformat-xml-2.14.0.jar \ stax2-api-4.0.0.jar \ jackson-module-jaxb-annotations-2.14.0.jar \ XmlMapperWriter.java <Person xmlns=""> <name>John Smith</name><married>false</married><age>25</age><phone/> </Person>
The Java program mapped a Java class object to an XML message correctly.
⇐ com.fasterxml.jackson.databind.ObjectWriter Example
2021-08-11, ∼2244🔥, 0💬
Popular Posts:
How to compare performances of various XML parsers with the jaxp\SourceValidator.jav aprovided in th...
Apache Log4j provides the interface that applications should code to and provides the adapter compon...
What Is HttpComponents httpclient-4.2.2.jar? HttpComponents httpclient-4.2.2.jar is the JAR file for...
XML Serializer, Release 2.7.1, allows you to write out XML, HTML etc. as a stream of characters from...
GJT (Giant Java Tree) implementation of XML Pull Parser. JAR File Size and Download Location: File n...