Java用String 截取方式解析xml文件、处理大xml文件

对于小数据返回值直接可采用Document、element的解析方式即可获得xml节点值。

但对于返回值大于10M的数据,Document解析就显得吃力甚至出现程序中断情况。针对返回数据大的xml可采取Java String

分割截取方式处理。

如下报文返回的是银行信息,程序需要获得BankLocationCode、BankLocationName、BankId、AreaId节点值。

可采取如下处理方式

步骤一:压缩文件,把xml中的内容进行过滤替换,把空格回车都去掉,BankLocation可替换为@用于压缩文件,经过替换原40M的报文最终解析为9M。

步骤二:根据解析后的String 对内容进行分割处理,分割成多个list,如下为报文样例和程序样例

返回报文样式格式

 
<Root>
<Head>
  <Version>20</Version>
  <LastVersion>0</LastVersion>
  <dataType>5</dataType>
  <TotalNum>130953</TotalNum>
  <CreateTime>2015-03-17 17:52:27</CreateTime>
</Head>
<Body>
  <BankLocation>
    <BankLocationCode>000998800006</BankLocationCode>
    <BankLocationName>电子联行转换中心</BankLocationName>
    <BankId>0</BankId>
    <AreaId>0</AreaId>
    <CityId>0</CityId>
    <ActionType>1</ActionType>
  </BankLocation>
  <BankLocation>
    <BankLocationCode>001612845001</BankLocationCode>
    <BankLocationName>崇左市中心支行</BankLocationName>
    <BankId>0</BankId>
    <AreaId>0</AreaId>
    <CityId>0</CityId>
    <ActionType>1</ActionType>
  </BankLocation>
</Body>
</Root>

扫描二维码关注公众号,回复: 4529307 查看本文章

package com.test;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;


import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;


 


public class dealXml {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String resultXml =   getTextFromFile2(new File("F:\\代码开发工作\\天安财险\\ATS-551资金系统联行号每周两次自动更新\\20150305_0_30_5_0\\20150305_0_30_5_0 - 副本.xml"), "utf-8");
//String resultXml =  FileUtil.getTextFromFile2(new File("F:\\代码开发工作\\天安财险\\ATS-551资金系统联行号每周两次自动更新\\DSP返回报文-20150317_0_20_5_0.xml"), "utf-8");
resultXml=resultXml.replaceAll(" ", "").replaceAll("\n", "");
resultXml=resultXml.replaceAll("\r", "");
resultXml=resultXml.replaceAll("<ActionType>1</ActionType>", "");
resultXml=resultXml.replaceAll("<BankLocation>", "@").replaceAll("</BankLocation>", "");
System.out.println(" length: "+resultXml.length());
resultXml=resultXml.replaceAll("<BankLocationCode>", "").replaceAll("</BankLocationCode>", "");
resultXml=resultXml.replaceAll("<BankLocationName>", "#").replaceAll("</BankLocationName>", "");
resultXml=resultXml.replaceAll("<BankId>", "+").replaceAll("</BankId>", "");
resultXml=resultXml.replaceAll("<AreaId>", "^").replaceAll("</AreaId>", "");
resultXml=resultXml.replaceAll("<CityId>", "*").replaceAll("</CityId>", ""); 


resultXml=resultXml.replaceAll("<Body>", "").replaceAll("</Body>", ""); 
resultXml=resultXml.replaceAll("<Root>", "").replaceAll("</Root>", ""); 
System.out.println(" length-r: "+resultXml.length());
//System.out.println(" length-r: "+resultXml);
String  alllist[] = resultXml.split("@");
 
List<String> codelist  =new ArrayList<String>();
List<String> namelist  =new ArrayList<String>();
List<String> bankidlist  =new ArrayList<String>();
List<String> areaidlist  =new ArrayList<String>();


for (int i=1  ;i< alllist.length;i++){
String code =alllist[i];
codelist.add(code.substring(0, code.indexOf("#"))) ;
if(code.indexOf("#+")>0){
namelist.add("") ;
}else{
namelist.add(code.substring(code.indexOf("#")+1, code.indexOf("+"))) ;
}
if(code.indexOf("+^")>0){
bankidlist.add("") ;
}else{
bankidlist.add(code.substring(code.indexOf("+")+1, code.indexOf("^"))) ;
}
System.out.println(" start:"+i+"code="+code);


if(code.indexOf("^*")>0){
areaidlist.add("") ;
}else{
areaidlist.add(code.substring(code.indexOf("^")+1, code.indexOf("*"))) ;
}

System.out.println(" start: "+codelist.size());
}


    public static String getTextFromFile2(File file, String encoding) {
        Reader reader = null;
        StringBuilder sb = new StringBuilder();
        try {
            //System.out.println("以字符为单位读取文件内容,一次读多个字节:");
            // 一次读多个字符
            char[] tempchars = new char[1024];
            int charread = 0;
            reader = new InputStreamReader(new FileInputStream(file), encoding);
            // 读入多个字符到字符数组中,charread为一次读取字符数
            while ((charread = reader.read(tempchars)) != -1) {
            sb.append(tempchars, 0, charread);
            }


        } catch (Exception e1) {
            e1.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
        return sb.toString();
    }


}

--------------------- 本文来自 土匪八号 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/fujianianhua/article/details/50480430?utm_source=copy

猜你喜欢

转载自blog.csdn.net/weixin_41167961/article/details/82970122