解析Xml文件

通过Xml转Json的方式解析

注意点:由于xml文件是FTP上的文件,且文件类型为:FTPFile

public InputStream getXML(FTPFile fPath) throws IOException{
//        ftp.enterLocalPassiveMode();
//        ftp.retrieveFileStream(new String(dirPath[1].getBytes("UTF-8"), "ISO-8859-1"));
        InputStream inputStream = this.ftp.retrieveFileStream(new String(fPath.getName().getBytes("UTF-8"),"ISO-8859-1"));
//        InputStream inputStream = this.ftp.retrieveFileStream(fPath.getName());
        byte[] bytes = toByteArray(inputStream);
        //下边两行代码代码重要,不然会报空,返回的字节数组,
        // 调用后再转换成
        inputStream.close();
        ftp.completePendingCommand();
        InputStream inputStream1 = new ByteArrayInputStream(bytes);
        return inputStream1;
    }
View Code
for(int i = 0;i<fileName_xml_ftp.size();i++){
            try {
                xmlJson = JsonUtils.xml2jsonString(ftp.getXML(fileName_xml_ftp.get(i)));
            } catch (IOException e) {
                log.error("######xml文件转义Json出现IO异常!########");
                e.printStackTrace();
                return;
            } catch (JSONException e) {
                log.error("######xml文件转义Json异常!########");
                e.printStackTrace();
                return;
            }
       .......  
}
View Code

1.首先将xml文件转化为Json

package cn.com.fotic.eimp.utils;

import org.apache.commons.io.IOUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;
import java.io.IOException;
import java.io.InputStream;

/**
 * @Title: JsonUtils
 * @Description:
 * @author: liqiang
 * @date: 2019-07-10 17:31
 */
public class JsonUtils {

    public static String xml2jsonString(InputStream in) throws JSONException, IOException {
//        InputStream in = JsonUtils.class.getResourceAsStream(file);
        String xml = IOUtils.toString(in);
        JSONObject xmlJSONObj = XML.toJSONObject(xml);
        return xmlJSONObj.toString();
    }

    public static void main(String[] args) throws JSONException, IOException {

//        String string = xml2jsonString();
//        System.out.println(string);
    }
}
View Code

设计到的jar包

       <dependency>
            <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20141113</version>
    </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>        
View Code

 

2.再将xml中的元素定义为对象Bean

/**
     * JSON字符串转对象
     *
     * @param jsonString
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T parse(String jsonString, Class<T> clazz) {
        return JSON.parseObject(jsonString, clazz);
    }
View Code

设计到的jar包

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <!-- <version>1.2.47</version> -->
            <version>1.1.41</version>
        </dependency>
View Code

 

posted @ 2019-07-11 14:15  家有小壮壮、  阅读(133)  评论(0编辑  收藏  举报