Json2Xml project is a simple implementation of JSON to XML conversion. Under the hood it uses Jacksonpull parser and generates
XML SAX events. This way the conversion has low memory consumption and is pretty fast.
There is already Jettison project that has similar objective, unfortunately it can not handle JSON arrays properly.
Otherwise it's possible to use net.javacrumbs.json2xml.JsonXmlReader together with standard Java transformation.
Transformer transformer = TransformerFactory.newInstance().newTransformer();
InputSource source = new InputSource(...);
Result result = ...;
transformer.transform(new SAXSource(new JsonXmlReader(),source), result);
For example
Transformer transformer = TransformerFactory.newInstance().newTransformer();
InputSource source = new InputSource(new StringReader(json));
DOMResult result = new DOMResult();
transformer.transform(new SAXSource(new JsonXmlReader(namespace, addTypeAttributes, artificialRootName), source), result);
result.getNode();
Type attributes
Since XML does not have any mechanism to reflect JSON type information, there is a new feature since json2xml version 1.2. You can switch on the addTypeAttributes flag using a
constructor argument. Then you will get the type information in XML attributes like this:
XML support only one root element but JSON documents may have multiple roots. To overcome this mismatch,
you can specify artificialRootName which will generate artificial XML root with given name.
new JsonXmlReader(null, false, "artificialRoot") will transform
If you have created an XML DOM node from JSON content with addTypeAttributes then you can convert it back to JSON using net.javacrumbs.json2xml.JsonXmlHelper.
// convert JSON to DOM Node
Node node = JsonXmlHelper.convertToDom(...);
// do whatever on the DOM Node (eventually modify using XPATH it while respecting the type attributes)
String json = JsonXmlHelper.convertToJson(node);
This method has proven very useful to work on huge JSON document using XPATH and converting it back to JSON afterward.
Name transformation
Other difference between JSON and XML are allowed names. In cases, when your JSON contains names not allowed as XML element names,
an ElementNameConverter can be used. For example to convert '@' to '_' create the following reader
new JsonXmlReader("", false, null, new ElementNameConverter() {
public String convertName(String name) {
return name.replaceAll("@","_");
}
})
Compatibility notes:
Version 4.1 handles arrays differently than the previous version. The change is in handling of arrays of JSON objects.
In older version it behaved erratically. Version 4 fixes the behavior, but is backwards-incompatible.
Also note that version 2.0 and higher require Jackson 2.x If you need new features in Jackson 1.x, just file a ticket and
I will backport the changes.
Please do not use version 4.0. It has broken handling of arrays. Since it has been in the wild for two days only, I do not assume
anyone is using it.
Maven
To use with Maven, add this dependency
<dependency>
<groupId>net.javacrumbs</groupId>
<artifactId>json-xml</artifactId>
<version>4.2</version><!-- for Jackson >= 2.0 -->
<!--<version>1.3</version>--><!-- older version for Jackson < 2.0 -->
</dependency>
请发表评论