【问题标题】:Splitting huge xml file >10GB into small chunks using Stax Parser使用 Stax Parser 将大于 10GB 的大型 xml 文件拆分为小块
【发布时间】:2016-03-12 23:05:15
【问题描述】:

我们有一个场景,我们需要将大小超过 10GB 的大型 xml 文件分割成小块。每个块应包含 100 或 200 个元素。示例 xml

<Employees>
  <Employee id="1">
    <age>29</age>
    <name>Pankaj</name>
    <gender>Male</gender>
    <role>Java Developer</role>
  </Employee>
  <Employee id="3">
    <age>35</age>
    <name>Lisa</name>
    <gender>Female</gender>
    <role>CEO</role>
  </Employee>
  <Employee id="3">
    <age>40</age>
    <name>Tom</name>
    <gender>Male</gender>
    <role>Manager</role>
  </Employee>
  <Employee id="3">
    <age>25</age>
    <name>Meghna</name>
    <gender>Female</gender>
    <role>Manager</role>
  </Employee>
  <Employee id="3">
    <age>29</age>
    <name>Pankaj</name>
    <gender>Male</gender>
    <role>Java Developer</role>
  </Employee>
  <Employee id="3">
    <age>35</age>
    <name>Lisa</name>
    <gender>Female</gender>
    <role>CEO</role>
  </Employee>
  <Employee id="3">
    <age>40</age>
    <name>Tom</name>
    <gender>Male</gender>
    <role>Manager</role>
 </Employee>
</Employees>

我有 Stax 解析器代码,它将文件分成小块。但是每个文件只包含一个完整的 Employee 元素,我需要在单个文件中包含 100 个或 200 个或更多 &lt;Employee&gt; 元素。这是我的java代码

public static void main(String[] s) throws Exception{
     String prefix = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+"\n";
        String suffix = "\n</Employees>\n";
        int count=0;
        try {

        int i=0;
             XMLInputFactory xif = XMLInputFactory.newInstance();
             XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("D:\\Desktop\\Test\\latestxml\\test.xml"));
             xsr.nextTag(); // Advance to statements element

             TransformerFactory tf = TransformerFactory.newInstance();
             Transformer t = tf.newTransformer();
             while(xsr.nextTag() == XMLStreamConstants.START_ELEMENT) {
                 File file = new File("C:\\Users\\test\\Desktop\\xml\\"+"out"  +i+ ".xml");
                 FileOutputStream fos=new FileOutputStream(file,true);
                 t.transform(new StAXSource(xsr), new StreamResult(fos));
                 i++;

             }

        } catch (Exception e) {
            e.printStackTrace();
        }

【问题讨论】:

    标签: java xml stax


    【解决方案1】:

    我希望我做对了,但你只需要在每次添加一个雇主时增加计数

            File file = new File("out" + i + ".xml");
            FileOutputStream fos = new FileOutputStream(file, true);
            appendStuff("<Employees>",file);
            while (xsr.nextTag() == XMLStreamConstants.START_ELEMENT) {
                count++;
                t.transform(new StAXSource(xsr), new StreamResult(fos));
                if(count == 100) {
                    count = 0;
                    i++;
                    appendStuff("</Employees>",file);
                    fos.close();
                    file = new File("out" + i + ".xml");
                    fos = new FileOutputStream(file, true);
                    appendStuff("<Employees>",file);
                }
            }
    

    它不是很好,但你明白了

    private static void appendStuff(String content, File file) throws IOException {
        FileWriter fw = new FileWriter(file.getAbsoluteFile(),true);
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(content);
        bw.close();
    }
    

    【讨论】:

    • 如何在每个文件中写入开始标签 和结束标签 以及拆分数据。
    • 代码运行良好,谢谢。但我不明白为什么 "" 会附加在每个 标签前面。
    • 您可以阅读 StAXDocu 或不正确的方式打开文件并替换它tutorialspoint.com/java/java_string_replaceall.htm
    【解决方案2】:

    不要每次迭代都放 i,当你的迭代达到 100 或 200 时,它应该更新为最新计数

    喜欢:

    String outputPath = "/test/path/foo.txt";
    
        while(xsr.nextTag() == XMLStreamConstants.START_ELEMENT) {
    
                        FileOutputStream file = new FileOutputStream(outputPath,true);
                         ... 
                         ...
                         count ++; 
                         if(count == 100){
                          i++;
                          outputPath = "/test/path/foo"+i+"txt";
                          count = 0;
                          }  
                     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-07
      • 2017-12-25
      • 2021-11-03
      • 1970-01-01
      • 1970-01-01
      • 2022-07-21
      • 2020-05-06
      相关资源
      最近更新 更多