package com.yh.saas.plugin.yiliangyiyun.service.impl;

import cn.hutool.core.util.XmlUtil;
import org.springframework.stereotype.Service;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

@Service
public class TestImpl {
    public static String xmlChangeString(String fileName){
        try {
            SAXReader saxReader = new SAXReader();//新建一个解析类
            Document tempDocument = saxReader.read(XmlUtil.class.getClassLoader().getResourceAsStream(fileName));//读入一个文件
            return tempDocument.asXML();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return null;
    }
    //将字符串string类型转换成xml文件
    public static void strChangeXML(String str) throws IOException {
        SAXReader saxReader = new SAXReader();
        Document document;
        try {
            document = saxReader.read(new ByteArrayInputStream(str.getBytes("UTF-8")));
            OutputFormat format = OutputFormat.createPrettyPrint();
            /** 将document中的内容写入文件中 */
            XMLWriter writer = new XMLWriter(new FileWriter(new File("F:/qwe/cctv.xml")),format);
            writer.write(document);
            writer.close();
        } catch (DocumentException e) {
            e.printStackTrace();
        }

    }

    public static void main(String args[]){
        String s = "<?xml version=\"1.0\" encoding=\"GBK\"?>\n" +
                "\n" +
                "<Kp>\n" +
                "\n" +
                "<Version>2.0</Version>\n" +
                "\n" +
                "\n" +
                "<Fpxx>\n" +
                "\n" +
                "<Zsl>1</Zsl>\n" +
                "\n" +
                "\n" +
                "<Fpsj>\n" +
                "\n" +
                "\n" +
                "<Fp>\n" +
                "\n" +
                "<Djh>23003657</Djh>\n" +
                "\n" +
                "<Gfmc>陈际良</Gfmc>\n" +
                "\n" +
                "<Gfsh>370785199002158677</Gfsh>\n" +
                "\n" +
                "<Gfyhzh>信用社6223190742010264</Gfyhzh>\n" +
                "\n" +
                "<Gfdzdh>柏城镇李家营社区文化路4号</Gfdzdh>\n" +
                "\n" +
                "<Bz>车号:鲁VP0531毛:56900 皮:16880 净:40020 应付:66753.36 整晒费:0 实付:66753.36 性质:贸易粮 单号:23003657 ,★☆性质:贸易粮 形态:玉米堆</Bz>\n" +
                "\n" +
                "<Fhr/>\n" +
                "\n" +
                "<Skr/>\n" +
                "\n" +
                "<Spbmbbh>10.0</Spbmbbh>\n" +
                "\n" +
                "<Hsbz>0</Hsbz>\n" +
                "\n" +
                "\n" +
                "<Spxx>\n" +
                "\n" +
                "\n" +
                "<Sph>\n" +
                "\n" +
                "<Xh>1</Xh>\n" +
                "\n" +
                "<Spmc>玉米</Spmc>\n" +
                "\n" +
                "<Ggxh>3等</Ggxh>\n" +
                "\n" +
                "<Jldw>公斤</Jldw>\n" +
                "\n" +
                "<Spbm>1010101030000000000</Spbm>\n" +
                "\n" +
                "<Qyspbm/>\n" +
                "\n" +
                "<Syyhzcbz>0</Syyhzcbz>\n" +
                "\n" +
                "<Lslbz>1</Lslbz>\n" +
                "\n" +
                "<Yhzcsm/>\n" +
                "\n" +
                "<Dj>1.66800000</Dj>\n" +
                "\n" +
                "<Sl>40020</Sl>\n" +
                "\n" +
                "<Je>66753.36</Je>\n" +
                "\n" +
                "<Kce>0</Kce>\n" +
                "\n" +
                "<Slv>0</Slv>\n" +
                "\n" +
                "<Se>0</Se>\n" +
                "\n" +
                "</Sph>\n" +
                "\n" +
                "</Spxx>\n" +
                "\n" +
                "</Fp>\n" +
                "\n" +
                "</Fpsj>\n" +
                "\n" +
                "</Fpxx>\n" +
                "\n" +
                "</Kp>";
        try{
            strChangeXML(s);
        }catch (IOException e) {
            e.printStackTrace();
        }

    }
}
