• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

lukas-krecan/json2xml: Java JSON to XML converter

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

lukas-krecan/json2xml

开源软件地址:

https://github.com/lukas-krecan/json2xml

开源编程语言:

Java 100.0%

开源软件介绍:

Java Json to XML conversion Apache License 2 CI Maven Central

Json2Xml project is a simple implementation of JSON to XML conversion. Under the hood it uses Jackson pull 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.

Json2Xml converts the following JSON

       {
       "document":{
          "a":1,
          "b":2,
          "c":{
             "d":"text"
          },
          "e":[1, 2, 3],
          "f":[[1, 2, 3], [4, 5,6]],
          "g":null,
          "h":[
             {
                "i":true,
                "j":false
             }
          ],
          "k":[
             [
                {"l":1, "m":2}
             ],
             [
                {"n":3, "o":4},
                {"p":5, "q":6}
             ]
          ]
       }
    }
     
    <document>
       <a>1</a>
       <b>2</b>
       <c>
          <d>text</d>
       </c>
       <e>
          <e>1</e>
          <e>2</e>
          <e>3</e>
       </e>
       <f>
          <f>
             <f>1</f>
             <f>2</f>
             <f>3</f>
          </f>
          <f>
             <f>4</f>
             <f>5</f>
             <f>6</f>
          </f>
       </f>
       <g />
       <h>
          <h>
             <i>true</i>
             <j>false</j>
          </h>
       </h>
       <k>
          <k>
             <k>
                <l>1</l>
                <m>2</m>
             </k>
          </k>
          <k>
             <k>
                <n>3</n>
                <o>4</o>
             </k>
             <k>
                <p>5</p>
                <q>6</q>
             </k>
          </k>
       </k>
    </document>
     

Usage

If you have SAX content handler, you can use net.javacrumbs.json2xml.JsonSaxAdapter class directly.

ContentHandler ch = ...;
JsonSaxAdapter adapter = new JsonSaxAdapter(json, ch);
adapter.parse();

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 version="1.0" encoding="UTF-8"?>
<document xmlns="http://javacrumbs.net/test">
   <a type="int">1</a>
   <b type="int">2</b>
   <c>
      <d type="string">text</d>
   </c>
   <e type="array">
      <e type="int">1</e>
      <e type="int">2</e>
      <e type="int">3</e>
   </e>
   <f type="array">
      <f type="array">
         <f type="int">1</f>
         <f type="int">2</f>
         <f type="int">3</f>
      </f>
      <f type="array">
         <f type="int">4</f>
         <f type="int">5</f>
         <f type="int">6</f>
      </f>
   </f>
   <g type="null" />
   <h type="array">
      <h>
         <i type="boolean">true</i>
         <j type="boolean">false</j>
      </h>
   </h>
   <k type="array">
      <k type="array">
         <k>
            <l type="int">1</l>
            <m type="int">2</m>
         </k>
      </k>
      <k type="array">
         <k>
            <n type="int">3</n>
            <o type="int">4</o>
         </k>
         <k>
            <p type="int">5</p>
            <q type="int">6</q>
         </k>
      </k>
   </k>
</document>

Artificial root

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

{"a":1, "b":2}

to

<artificialRoot>
  <a>1</a>
  <b>2</b>
</artificialRoot>

Converting XML DOM back to JSON

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>



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
lyft/mapper: A JSON deserialization library for Swift发布时间:2022-07-09
下一篇:
kristopolous/TickTick: JSON in your Bash scripts发布时间:2022-07-09
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap