The Wayback Machine - https://web.archive.org/web/20180414124559/http://xml.sys-con.com/node/4142956

Welcome!

Industrial IoT Authors: Elizabeth White, Stackify Blog, Yeshim Deniz, SmartBear Blog, Liz McMillan

Related Topics: Java IoT, Industrial IoT, @CloudExpo

Java IoT: Blog Feed Post

Solving the XML Problem with Jackson | @CloudExpo #API #Java #JSON #Cloud

Jackson is a popular library for handling JSON in Java applications, quickly becoming the de-facto standard in the ecosystem

Solving the XML Problem with Jackson
By Eugen Paraschiv

Jackson is a popular library for handling JSON in Java applications, quickly becoming the de-facto standard in the ecosystem. Starting with version 2, it has also introduced a mature XML implementation alongside its established JSON support.

Adding Jackson XML to the Project
Adding the Jackson XML module to the project only needs a single dependency - the Jackson XML module itself:

<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.9.0</version>
</dependency>

And in Gradle:

compile "com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.9.0"

This will automatically pull in all of the other Jackson dependencies that are needed:

Note that the Woodstox StAX library can be excluded and replaced with any other valid implementation of the StAX API.

Configuring the Jackson XML Module
Jackson is typically used by means of an ObjectMapper instance. In order to work with XML, we'll instead use the XmlMapper class. This is a direct extension of ObjectMapper and can be used as a replacement, with the exact same API we're already used to.

The absolute simplest way of working with this is to just use the default configuration:

ObjectMapper objectMapper = new XmlMapper();

However, if we need additional configuration, we can instead construct the Jackson Module manually and set it up as necessary:

JacksonXmlModule xmlModule = new JacksonXmlModule();
xmlModule.setDefaultUseWrapper(false);
ObjectMapper objectMapper = new XmlMapper(module);

As of version 2.9.0, the only configuration options for the XML Module are:

  • setDefaultUseWrapper - defines whether or not to use a wrapper for non-annotated List properties by default
  • setXMLTextElementName - defines the virtual name to use when processing character data sections - when not binding to Java beans

Due to the fact that the XmlMapper extends the standard ObjectMapper, we can essentially use all of the standard Jackson ObjectMapper configuration settings and APISs.

For example, we can configure it to produce indented output as follows:

objectMapper.enable(SerializationFeature.INDENT_OUTPUT);

Note that some of these settings might not work with some XML implementations. For example, older versions of Jackson XML depended on Stax2Writer, which didn't support writing raw characters to the stream. This means that it doesn't support writing the raw newlines and indentations needed for the INDENT_OUTPUT feature to work.

Object Serialization
Jackson is best known for its ability to serialize POJOs into JSON and back, by means of standard conventions and - where necessary - special annotations to give clues to the Jackson library.

As already mentioned, the XmlMapper object directly replaced ObjectMapper, only it works in terms of XML instead of JSON. This means that the API is exactly the same, and it can be used as a direct replacement.

Writing XML
Writing XML is done using the various
writeValue methods that Jackson exposes. For example, marshalling some object to XML is done simply by using the writeValueAsString API:

String xml = objectMapper.writeValueAsString(input);

The standard versions of this all exist and work as expected:

  • Writing to a String
  • Writing to a Byte Array
  • Writing to a java.io.OutputStream - e.g. for streaming over a network connection
  • Writing to a java.io.Writer
  • Writing to a java.io.File

Reading XML
As we just explored, we the library handles writing to XML quite well.

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.readValue(inputStream, MyBean.class);

Again, the standard versions of this all exist, and work as expected:

  • Reading from a String
  • Reading from a Byte Array
  • Reading from a java.io.InputStream - e.g. for streaming over a network connection
  • Reading from a java.io.Reader
  • Reading from a java.io.File

Jackson Annotations for Serialization
The Jackson XML module supports the full range of annotations that Jackson provides for annotating our POJOs
.

This means that we can have one single set of beans, with one set of annotations and, depending on the ObjectMapper instance, we select whether we get XML or JSON. That's a huge benefit when it comes to structuring our code, as we no longer have to worry about translating between different beans that represent essentially the same data just to get different serialization formats.

For example, given the following bean:

@JsonPropertyOrder({"age", "id", "name"})
public class Person {
@JsonProperty("_id")
private String id;

private String name;

private int age;

@JsonIgnore
private String note;
}

will produce this JSON:

{
"age":4,
"_id":"12345",
"name":"George"
}

And this XML:

<Person>
<age>4</age>
<_id>12345</_id>
<name>George</name>
</Person>

Additional Jackson Annotations for XML
The Jackson XML module adds some additional support for XML specific features.
These annotations allow us to control the XML namespace and local name for elements, including the root element, whether a field is rendered in an element or as plain text, whether the content of an element is rendered in a CData wrapper, and whether a collection should use a wrapper element or not.

@JacksonXmlProperty

@JacksonXmlProperty can be applied to any field in a bean to control the details of the element that is being rendered. This annotation allows us to determine the namespace, the local name, and whether the field is serialized as an Element or an Attribute. For example, the following bean:

For example, the following bean:

public class Person {
@JacksonXmlProperty(
isAttribute = true, namespace = "urn:stackify:jacksonxml", localName = "_id")
private String id;

@JacksonXmlProperty(namespace = "urn:stackify:jackson")
private String name;

private String note;
}

This generates the following XML output:

<Person xmlns:wstxns1="urn:stackify:jacksonxml"
wstxns1:_id="12345">
<wstxns2:name xmlns:wstxns2="urn:stackify:jackson">Graham</wstxns2:name>
<note>Hello</note>
</Person>

@JacksonXmlRootElement

The @JacksonXmlRootElement has a similar role to the @JacksonXmlProperty but for the root element of the entire document. This can only adjust the Namespace and Local name - since the root element can never be serialized as an attribute.

For example, let's look at this Java POJO:

@JacksonXmlRootElement(namespace = "urn:stackify:jacksonxml", localName = "PersonData") public class Person {
private String id;

private String name;

private String note;
}

When serialized, this will result in the following XML:

<PersonData xmlns="urn:stackify:jacksonxml">
<id xmlns="">12345</id>
<name xmlns="">Graham</name>
<note xmlns="">Hello</note>
</PersonData>

@JacksonXmlText

Next, let's have a look at the @JacksonXmlText annotation.

Simply put, this indicates that an element should be rendered as plain text without another element containing it.

For example, the following POJO:

public class Person {
private String id;

private String name;

@JacksonXmlText
private String note;
}

Will produce this simple XML output:

<Person>
<id>12345</id>
<name>Graham</name>
Hello
</Person>

Naturally, you do have to be careful using this annotation and make sure you're still generating valid XML.

@JacksonXmlCData

The @JacksonXmlCData annotation indicates that a CData wrapper should be placed around the content of the element. This can be used in conjunction with the @JacksonXmlText if desired to produce a CData wrapper without an element tag.

Let's have a look at a POJO using this annotation:

public class Person {
private String id;

@JacksonXmlCData
private String name;

@JacksonXmlText
@JacksonXmlCData
private String note;
}

This will result in the following XML:

<Person>
<id>12345</id>
<name><![CDATA[Graham]]></name>
<![CDATA[Hello]]>
</Person>

JacksonXmlElementWrapper

The @JacksonXmlElementWrapper annotation is used to override the default setting from setDefaultUseWrapper - as seen above. This can ensure that a collection either does or does not use a wrapper element, and can control what the wrapper element uses for namespace and local name.

When using wrapper elements, we get an additional element added which contains all of the elements from the collection, and when wrapper elements are not used then the individual collection elements are written directly inline:

class Wrapper {
@JacksonXmlElementWrapper(localName = "list")
private List names;
}

This will produce the following XML:

<Wrapper>
<list>
<names>John</names>
<names>Paul</names>
<names>George</names>
<names>Ringo</names>
</list>
</Wrapper>

Whereas, if the JacksonXmlElementWrapper is replaced with:

@JacksonXmlElementWrapper(useWrapping = false)

Then the XML produced won't contain the list element:

<Wrapper>
<names>John</names>
<names>Paul</names>
<names>George</names>
<names>Ringo</names>
</Wrapper>

Supporting JAXB Annotations

The Jackson XML module also has the ability to support the standard JAXB annotations on our beans - instead of needing the Jackson specific ones. This can be useful if we want to use Jackson for the actual XML Serialization but don't want to depend on it at compile time.

This can also be used to allow JAXB to generate our bean definitions from an XML Schema and have Jackson process them.

This functionality is an additional module that needs to be added in for it to work. It doesn't work out of the box like the Jackson annotations do. In order to configure this - we need to add the JaxbAnnotationModule to our ObjectMapper as follows:

objectMapper.registerModule(new JaxbAnnotationModule());

We can now write or generate a Java bean with JAXB annotations and simply process it with this XmlMapper.

For example, the following POJO:

@XmlRootElement(name = "employee")
@XmlAccessorType(XmlAccessType.FIELD)
public class EmployeeBean {
@XmlAttribute(name = "_id")
@XmlID
private String id;

@XmlElement
private String name;

@XmlElement
private String type;
}

Will produce the following XML when marshalled:

<employee _id="12345">
<name>Graham</name>
<type>Developer</type>
</employee>

Partial Reading and Writing
Jackson also has the powerful ability to actually jump into the middle of an existing XML file and either marshall or unmarshall XML to and from that file.

The functionality makes good use of the standard XMLStreamWriter class, and naturally of XMLStreamReader as well. This cool functionality gives us a lot of flexibility to work with existing XML documents and integrate with these cleanly and easily.

Generating XML
The XmlMapper is able to serialize an entire Java bean into a document that is currently being produced, allowing Jackson to integrate and construct the document along with other external actors. This also gives some ability to support constructs that Jackson cannot handle natively - for example, the XML Prolog.

In order to do this, the XmlMapper needs to be called to write values to the XMLStreamWriter object - the same as if we were writing to any other Writer:

StringWriter stringWriter = new StringWriter();
XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newFactory();
XMLStreamWriter sw = xmlOutputFactory.createXMLStreamWriter(stringWriter);

XmlMapper mapper = new XmlMapper();

sw.writeStartDocument();
sw.writeStartElement("root");

mapper.writeValue(sw, employeeBean);
sw.writeComment("Some insightful commentary here");
sw.writeEndElement();
sw.writeEndDocument();

This will produce the following XML:

<?xml version='1.0' encoding='UTF-8'?>
<root>
<employee _id="12345">
<name>Graham</name>
<type>Developer</type>
</employee>
<!--Some insightful commentary here-->
</root>

Here, the XML Prolog, root element, and the comment - are not produced by Jackson XML, but everything inside the EmployeeBean element is.

Consuming XML
The opposite of this is also possible - deserializing a Java bean from the middle of an XMLStreamReader.

This can be especially useful if we only care to have Java bean representations for data in the middle of a larger object - for example, if we are parsing an Atom wrapper around the data we are interested in.

In order to do this, the XmlMapper needs to be called to read values from the XMLStreamReader object - the same as if we were reading from any other Reader.

Let's have a look at a simple example. The following Java code will consume the XML generated above into an EmployeeBeen instance:

XMLInputFactory f = XMLInputFactory.newFactory();
XMLStreamReader sr = f.createXMLStreamReader(new FileInputStream(inputFile));

XmlMapper mapper = new XmlMapper();
sr.next(); // to point to <root>
sr.next(); // to point to root-element under root
EmployeeBeen employee = mapper.readValue(sr, EmployeeBeen.class);
sr.close();

Limitations of the Jackson XML Module
The XML module in Jackson is by no means designed to be an exact replacement for JAXB. There are certain constructs that will work subtly differently between JAXB and Jackson, and there is no guarantee that Jackson will produce identical XML to JAXB.

Note that, unlike with JSON, the outermost object must be a bean type - it can not be a primitive or wrapper type, an enumeration, or a collection. This is a direct result of how XML works - there's no way in XML to represent such a top-level value.

By default, Jackson will always use a wrapper element for collections, which is also different to how JAXB works. This is the major way that the XML produced by Jackson is not compatible with the XML produced by JAXB. Of course, the behavior can be configured, using the JacksonXmlElementWrapper annotation for one field or the setDefaultUseWrapper configuration setting on the XmlMapper globally.

Jackson also has no support for working with specific XML Schemas. It's designed for writing Java Beans first, rather than generating the Java code from pre-existing schemas. Note that this can be solved to an extent by using the JAXB annotation support and generating the beans using the standard xjc tool.

Equally, it has no support for some of the more advanced XML tools - such as XPath or XSLT. If we need this level of support then we should instead use a more full-featured XML solution.

Usage on Android
In the mobile space, Jackson XML works perfectly well on Android. However, the StAX API is not included in the Android JVM, so it needs to instead be bundled manually.

If we're using the Woodstox XML library that Jackson XML depends on by default - there's nothing extra to be done. If, however, we're using an alternative library - then we might need to add that dependency manually:

<dependency>
<groupId>javax.xml.stream</groupId>
<artifactId>stax-api</artifactId>
<version>1.0-2</version>
</dependency</span>

And for Gradle:

compile "javax.xml.stream:stax-api:jar:1.0-2"

Summary
If you're looking for a mature, flexible way of supporting and working with both JSON and XML for the same data, the Jackson XML module is a fantastic library to leverage. It's not only a solid way to go on its own, but it also has the added benefit of being able to mostly reuse the same configuration for both XML as well as JSON.

Typically, this has to be handled using two different libraries with entirely separate configurations.

Finally, beyond flexibility and ease-of-use, the Jackson team has historically placed a strong emphasis on performance. And given that marshalling and unmarshalling of data is a large part of most web applications, choosing the right library to handle all of that work is critical. That, plus a performance monitoring tool such as Retrace will allow you to get the most out of your app.

The post Solving the XML Problem with Jackson appeared first on Stackify.

Read the original blog entry...

More Stories By Stackify Blog

Stackify offers the only developers-friendly solution that fully integrates error and log management with application performance monitoring and management. Allowing you to easily isolate issues, identify what needs to be fixed quicker and focus your efforts – Support less, Code more. Stackify provides software developers, operations and support managers with an innovative cloud based solution that gives them DevOps insight and allows them to monitor, detect and resolve application issues before they affect the business to ensure a better end user experience. Start your free trial now stackify.com

@ThingsExpo Stories
DXWorldEXPO LLC announced today that "IoT Now" was named media sponsor of CloudEXPO | DXWorldEXPO 2018 New York, which will take place on November 11-13, 2018 in New York City, NY. IoT Now explores the evolving opportunities and challenges facing CSPs, and it passes on some lessons learned from those who have taken the first steps in next-gen IoT services.
DXWorldEXPO LLC announced today that ICOHOLDER named "Media Sponsor" of Miami Blockchain Event by FinTechEXPO. ICOHOLDER gives detailed information and help the community to invest in the trusty projects. Miami Blockchain Event by FinTechEXPO has opened its Call for Papers. The two-day event will present 20 top Blockchain experts. All speaking inquiries which covers the following information can be submitted by email to [email protected] Miami Blockchain Event by FinTechEXPOalso offers sp...
Coca-Cola’s Google powered digital signage system lays the groundwork for a more valuable connection between Coke and its customers. Digital signs pair software with high-resolution displays so that a message can be changed instantly based on what the operator wants to communicate or sell. In their Day 3 Keynote at 21st Cloud Expo, Greg Chambers, Global Group Director, Digital Innovation, Coca-Cola, and Vidya Nagarajan, a Senior Product Manager at Google, discussed how from store operations and ...
We are seeing a major migration of enterprises applications to the cloud. As cloud and business use of real time applications accelerate, legacy networks are no longer able to architecturally support cloud adoption and deliver the performance and security required by highly distributed enterprises. These outdated solutions have become more costly and complicated to implement, install, manage, and maintain.SD-WAN offers unlimited capabilities for accessing the benefits of the cloud and Internet. ...
Gemini is Yahoo’s native and search advertising platform. To ensure the quality of a complex distributed system that spans multiple products and components and across various desktop websites and mobile app and web experiences – both Yahoo owned and operated and third-party syndication (supply), with complex interaction with more than a billion users and numerous advertisers globally (demand) – it becomes imperative to automate a set of end-to-end tests 24x7 to detect bugs and regression. In th...
The Founder of NostaLab and a member of the Google Health Advisory Board, John is a unique combination of strategic thinker, marketer and entrepreneur. His career was built on the "science of advertising" combining strategy, creativity and marketing for industry-leading results. Combined with his ability to communicate complicated scientific concepts in a way that consumers and scientists alike can appreciate, John is a sought-after speaker for conferences on the forefront of healthcare science,...
I think DevOps is now a rambunctious teenager - it's starting to get a mind of its own, wanting to get its own things but it still needs some adult supervision," explained Thomas Hooker, VP of marketing at CollabNet, in this SYS-CON.tv interview at DevOps Summit at 20th Cloud Expo, held June 6-8, 2017, at the Javits Center in New York City, NY.
As ridesharing competitors and enhanced services increase, notable changes are occurring in the transportation model. Despite the cost-effective means and flexibility of ridesharing, both drivers and users will need to be aware of the connected environment and how it will impact the ridesharing experience. In his session at @ThingsExpo, Timothy Evavold, Executive Director Automotive at Covisint, discussed key challenges and solutions to powering a ride sharing and/or multimodal model in the age ...
In his keynote at 19th Cloud Expo, Sheng Liang, co-founder and CEO of Rancher Labs, discussed the technological advances and new business opportunities created by the rapid adoption of containers. With the success of Amazon Web Services (AWS) and various open source technologies used to build private clouds, cloud computing has become an essential component of IT strategy. However, users continue to face challenges in implementing clouds, as older technologies evolve and newer ones like Docker c...
CloudEXPO | DXWorldEXPO are the world's most influential, independent events where Cloud Computing was coined and where technology buyers and vendors meet to experience and discuss the big picture of Digital Transformation and all of the strategies, tactics, and tools they need to realize their goals. Sponsors of DXWorldEXPO | CloudEXPO benefit from unmatched branding, profile building and lead generation opportunities.
delaPlex is a global technology and software development solutions and consulting provider, deeply committed to helping companies drive growth, revenue and marketplace value. Since 2008, delaPlex's objective has been to be a trusted advisor to its clients. By redefining the outsourcing industry's business model, the innovative delaPlex Agile Business Framework brings an unmatched alliance of industry experts, across industries and functional skillsets, to clients anywhere around the world.
CloudEXPO New York 2018, colocated with DXWorldEXPO New York 2018 will be held November 11-13, 2018, in New York City and will bring together Cloud Computing, FinTech and Blockchain, Digital Transformation, Big Data, Internet of Things, DevOps, AI, Machine Learning and WebRTC to one location.
Founded in 2000, Chetu Inc. is a global provider of customized software development solutions and IT staff augmentation services for software technology providers. By providing clients with unparalleled niche technology expertise and industry experience, Chetu has become the premiere long-term, back-end software development partner for start-ups, SMBs, and Fortune 500 companies. Chetu is headquartered in Plantation, Florida, with thirteen offices throughout the U.S. and abroad.
Announcing Poland #DigitalTransformation Pavilion
The best way to leverage your CloudEXPO | DXWorldEXPO presence as a sponsor and exhibitor is to plan your news announcements around our events. The press covering CloudEXPO | DXWorldEXPO will have access to these releases and will amplify your news announcements. More than two dozen Cloud companies either set deals at our shows or have announced their mergers and acquisitions at CloudEXPO. Product announcements during our show provide your company with the most reach through our targeted audienc...
Explosive growth in connected devices. Enormous amounts of data for collection and analysis. Critical use of data for split-second decision making and actionable information. All three are factors in making the Internet of Things a reality. Yet, any one factor would have an IT organization pondering its infrastructure strategy. How should your organization enhance its IT framework to enable an Internet of Things implementation? In his session at @ThingsExpo, James Kirkland, Red Hat's Chief Archi...
The standardization of container runtimes and images has sparked the creation of an almost overwhelming number of new open source projects that build on and otherwise work with these specifications. Of course, there's Kubernetes, which orchestrates and manages collections of containers. It was one of the first and best-known examples of projects that make containers truly useful for production use. However, more recently, the container ecosystem has truly exploded. A service mesh like Istio addr...
Headquartered in Plainsboro, NJ, Synametrics Technologies has provided IT professionals and computer systems developers since 1997. Based on the success of their initial product offerings (WinSQL and DeltaCopy), the company continues to create and hone innovative products that help its customers get more from their computer applications, databases and infrastructure. To date, over one million users around the world have chosen Synametrics solutions to help power their accelerated business or per...
In his keynote at 19th Cloud Expo, Sheng Liang, co-founder and CEO of Rancher Labs, discussed the technological advances and new business opportunities created by the rapid adoption of containers. With the success of Amazon Web Services (AWS) and various open source technologies used to build private clouds, cloud computing has become an essential component of IT strategy. However, users continue to face challenges in implementing clouds, as older technologies evolve and newer ones like Docker c...
Michael Maximilien, better known as max or Dr. Max, is a computer scientist with IBM. At IBM Research Triangle Park, he was a principal engineer for the worldwide industry point-of-sale standard: JavaPOS. At IBM Research, some highlights include pioneering research on semantic Web services, mashups, and cloud computing, and platform-as-a-service. He joined the IBM Cloud Labs in 2014 and works closely with Pivotal Inc., to help make the Cloud Found the best PaaS.