通过超链接 解析zip压缩包 中的 xml 内容

import com.alibaba.fastjson.JSON;
import com.api.zp.documentexchange.OkHttp;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.dom4j.*;
import org.xml.sax.SAXException;
import selfdev.util.log.LogTool;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.*;
import java.nio.charset.Charset;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class ZipTest {
public static String requestUrlGetStrXml(String dnFileUrl) throws IOException {
         dnFileUrl = dnFileUrl.replace("", "");
         LOGGER.writeLog("dnFileUrl: "+dnFileUrl);

        ZipInputStream zipInputStream = null;
        BufferedReader br = null;
        // URL url = new URL(dnFileUrl);
        // HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        Charset utf8 = Charset.forName("utf-8");
        // int resultCode = conn.getResponseCode();
        StringBuilder sb=new StringBuilder();
        //OKhttp访问
        OkHttpClient okClient  = OkHttp.getUnsafeOkHttpClient();
        Request request =new Request.Builder()
                .url(dnFileUrl)
                .build();
        Response response = okClient.newCall(request).execute();
        InputStream inputStream = response.body().byteStream();
        BufferedInputStream in = new BufferedInputStream(inputStream,4096);

		
            zipInputStream = new ZipInputStream(in);
            ZipEntry zipEntry;
            LOGGER.writeLog("Charset.defaultCharset() "+Charset.defaultCharset());
            while ((zipEntry = zipInputStream.getNextEntry()) != null) {
                if (zipEntry.toString().endsWith(".xml")) {
                    //设置原本 xml 的编码  ps:如果和原来的xml编码不一致就会出现乱码
                    br = new BufferedReader(new InputStreamReader(zipInputStream,utf8));

                    String line;
                    while ((line = br.readLine()) != null) {
                        LOGGER.writeLog(line);
                        // String gbk = new String(line.getBytes("gbk"),
                        //         "utf-8");
                        // String gbkk = new String(line.getBytes(utf8));
                        // LOGGER.writeLog(gbkk);
                        sb.append(line);
                    }
                }
            }
        return sb.toString();
    }
//解析xml
    public static Map parseXml(String strXml) throws DocumentException {
        Map<String,String> map=new HashMap<>();
        Document document = DocumentHelper.parseText(strXml);
        Element root = document.getRootElement();
        Element object = root.element("OBJECT");
        List item = object.elements("ITEM");
        for (Iterator<?> it = item.iterator(); it.hasNext();) {
            Element e = (Element) it.next();
            String value = e.attributeValue("NAME");
            if (value.equals("DOC_FILE")) {
                Element row = e.element("ROW");
                List item1 = row.elements();
                Iterator iterator = item1.iterator();
                while (iterator.hasNext()) {
                    Element next = (Element) iterator.next();
                    String name = next.attributeValue("NAME");
                    String text = next.getText();
                    map.put(name,text);
                }
            }
        }
        return map;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值