简体   繁体   English

将XML文件拆分为多个文件,每个文件有500个标记

[英]Split XML file into multiple files with 500 tags each

I have a large (1 GB) file that I need to split into smaller files. 我有一个大(1 GB)文件,我需要将其拆分为较小的文件。 I want each smaller file to contain 500 of the <OFFER> tags. 我希望每个较小的文件包含500个<OFFER>标记。

Here is a small snippet of the large XML file: 这是一个大型XML文件的小片段:

<?xml version="1.0"?><RESULT>
<header>
    <site>http://www.thomascook.fr</site>
    <marque>ThomasCook France</marque>
    <logo>http://www.example.com/example.gif</logo>
</header>
<OFFER>
    <IFF>5810</IFF>
    <TO>TCF</TO>
    <COUNTRY>Chypre</COUNTRY>
    <REGION>Chypre du Sud</REGION>
    <HOTELNAME>Elias Beach &amp; Country Club</HOTELNAME>
    <DESCRIPTION>....</DESCRIPTION>
    <TYPE>Sejour</TYPE>
    <STARS>5.0</STARS>
    <THEMAS>Plage directe;Special enfant;Bien-Etre-Fitness</THEMAS>
    <THUMBNAIL>http://example.com/example.jpg</THUMBNAIL>
    <URL>http://example.com/example.html</URL>
    <DATE>
        <BROCHURE>TCFB</BROCHURE>
        <DURATION>7</DURATION>
        <DURATION_VAR>6_6-9</DURATION_VAR>
        <BOARD>Demi-pension</BOARD>
        <DEPARTURE>27.2.2011</DEPARTURE>
        <RETURN>6.3.2011</RETURN>
        <DEPARTURE_CITY>PAR</DEPARTURE_CITY>
        <ARRIVAL_CITY>LCA</ARRIVAL_CITY>
        <PRICE>790</PRICE>
        <URL>http://example.com/other-example.html</URL>
    </DATE>
</OFFER>
<OFFER>
  (etc)
</OFFER>

How can I do this? 我怎样才能做到这一点?

From you english I understand that you want to split a big XML file into multiple small files. 从你的英文我明白你想要将一个大的XML文件拆分成多个小文件。 The best one is http://vtd-xml.sourceforge.net/ 最好的是http://vtd-xml.sourceforge.net/

Sample Code, the following code will split the big xml based on XPath, TopTag/ChildTag 示例代码,以下代码将基于XPath,TopTag / ChildTag拆分大xml


import java.io.File;
import java.io.FileOutputStream;

import com.ximpleware.AutoPilot;
import com.ximpleware.FastLongBuffer;
import com.ximpleware.VTDGen;
import com.ximpleware.VTDNav;

// This example shows how to split XML
public class Split {
    public static void main(String[] args) {
        String prefix = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<TopTag xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n";
        String suffix = "\n</TopTag<";
        try {

            VTDGen vg = new VTDGen();
            if (vg.parseFile(args[0], false)) {
                int splitBy = Integer.parseInt(args[1]);
                String filePrefix =  args[2];
                VTDNav vn = vg.getNav();
                AutoPilot ap = new AutoPilot(vn);
                ap.selectXPath("/TopTag/ChildTag");
                // flb contains all the offset and length of the segments to be
                // skipped
                FastLongBuffer flb = new FastLongBuffer(4);
                int i;
                byte[] xml = vn.getXML().getBytes();
                while ((i = ap.evalXPath()) != -1) {
                    flb.append(vn.getElementFragment());
                }
                int size = flb.size();
                if (size != 0) {
                    File fo = null;
                    FileOutputStream fos = null;
                    for (int k = 0; k < size; k++) {
                        if (k % splitBy == 0) {
                            if (fo != null) {
                                fos.write(suffix.getBytes());
                                fos.close();
                                fo = null;
                            }
                        }
                        if (fo == null) {
                            fo = new File(filePrefix + k + ".xml");
                            fos = new FileOutputStream(fo);
                            fos.write(prefix.getBytes());
                        }
                        fos.write(xml, flb.lower32At(k), flb.upper32At(k));
                    }
                    if (fo != null) {
                        fos.write(suffix.getBytes());
                        fos.close();
                        fo = null;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

As a programming question, this is just a matter of stax programming . 作为一个编程问题,这只是stax 编程的问题

Every 500 elements make the necessary calls to end the element and the document, close the file, open a new file, start the new file, and continue along. 每500个元素进行必要的调用以结束元素和文档,关闭文件,打开新文件,启动新文件,然后继续。 If you have a program that can write one file in stax, it's not very different to write many. 如果你有一个程序可以用stax写一个文件,那么编写很多文件并没有什么不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM