XML和JSON互转

引入maven jar包

gradle 引入相关版本的包也一样

<!-- https://mvnrepository.com/artifact/net.sf.json-lib/json-lib -->
        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
            <classifier>jdk15</classifier>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.ws/spring-xml -->
        <dependency>
            <groupId>org.springframework.ws</groupId>
            <artifactId>spring-xml</artifactId>
            <version>2.3.0.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/xom/xom -->
        <dependency>
            <groupId>xom</groupId>
            <artifactId>xom</artifactId>
            <version>1.2.5</version>
        </dependency>

相关代码

package com.ruoyi.edi.utils;


import net.sf.json.JSONObject;
import net.sf.json.xml.XMLSerializer;
import org.springframework.stereotype.Service;
import org.springframework.xml.transform.StringResult;
import org.springframework.xml.transform.StringSource;

import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * Create by administrator 2021/4/26/0026 10:46
 **/
@Service
public class XsltUtils {
    private static TransformerFactory factory = TransformerFactory.newInstance();


    public  String transform(String xml, String xsl) throws Exception {
        Templates template = factory.newTemplates(new StringSource(xsl.trim()));
        Transformer transformer = template.newTransformer();
        StringResult sr = new StringResult();
        transformer.transform(new StringSource(xml.trim()), sr);
        return sr.toString();
    }

    static void transform(File xml, File xsl, File output) {
        try {
            Templates template = factory.newTemplates(new StreamSource(new FileInputStream(xsl)));
            Transformer transformer = template.newTransformer();
            Source source = new StreamSource(new FileInputStream(xml));
            Result result = new StreamResult(new FileOutputStream(output));
            transformer.transform(source, result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public  String json2XML(String json) {


        JSONObject jobj = JSONObject.fromObject(json);
        XMLSerializer xmlSerializer=new XMLSerializer();
        xmlSerializer.setRootName("request");
        xmlSerializer.setTypeHintsEnabled(false);
        return xmlSerializer.write(jobj);
//        return new XMLSerializer().write(jobj);
    }

    static String xml2JSON(String xml) {
        return new XMLSerializer().read(xml).toString();
    }
}
备注

XmlSerializer.write(object,"GBK")生成xml 的时候可以改变<?xml version="1.0" encoding="UTF-8"?>
这个方法可以设置信息头xmlSerializer.setRootName("Request");  如果不设置 默认为一个"O";xmlSerializer.setTypeHintsEnabled(false); 为设置是否显示类型
JSONtoXml的结果

<o><barodes class="array"><e class="object"><barcode class="object"><barcode type="string">条码</barcode><companyCode type="string">货主编码</companyCode><itemCode type="string">货品编码</itemCode><quantityUM type="string">EA</quantityUM><warehouseCode type="string">仓库编码</warehouseCode></barcode></e></barodes><item class="object"><attributeTemplateCode type="string">属性模板</attributeTemplateCode><brand type="string">品牌</brand><class01 type="string">大类</class01><class02 type="string">中类</class02><class03 type="string">小类</class03><code type="string">itemCode01</code><companyCode type="string">companyCode01</companyCode><daysToExpire type="number">30</daysToExpire><expiringDays type="number">30</expiringDays><hi type="number">10</hi><imported type="number">0</imported><isGift type="number">0</isGift><itemColor type="string">颜色</itemColor><itemSize type="string">尺寸</itemSize><itemStyle type="string">规格</itemStyle><minShelfLifeDays type="number">30</minShelfLifeDays><name type="string">itemCode01</name><placeOfOrigin type="string">产地</placeOfOrigin><productLine type="string">产品线</productLine><taxCategoryCode type="string">税类编码</taxCategoryCode><tfSuit type="number">0</tfSuit><ti type="number">10</ti><trackSerialNum type="number">0</trackSerialNum><unifiedCompareCode type="string">统一对照码</unifiedCompareCode><warehouseCode type="string">warehouseCode01</warehouseCode></item><itemPackings class="array"><e class="object"><itemPacking class="object"><details class="array"><e class="object"><detail class="object"><companyCode type="string">货主编码</companyCode><displayName type="string">单位显示名称</displayName><itemCode type="string">货品编码</itemCode><packingCode type="string">包装编码</packingCode><sequence type="number">1</sequence><unitCode type="string">单位编码</unitCode><unitHeight type="number">1.21</unitHeight><unitLength type="number">1.21</unitLength><unitName type="string">单位名称</unitName><unitVolume type="number">1.21</unitVolume><unitWeight type="number">1.21</unitWeight><unitWidth type="number">1.21</unitWidth><warehouseCode type="string">仓库编码</warehouseCode></detail></e></details><header class="object"><companyCode type="string">货主编码</companyCode><itemCode type="string">货品编码</itemCode><itemName type="string">货品名称</itemName><packingCode type="string">包装编码</packingCode><packingName type="string">包装名称</packingName><sequence type="number">1</sequence><warehouseCode type="string">仓库编码</warehouseCode></header></itemPacking></e></itemPackings><warehouseCodes type="string"/></o>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值