Java 在多个XML文件中拆分XML

Java 在多个XML文件中拆分XML,java,xml,Java,Xml,我有以下xml文件作为输入 <?xml version="1.0" encoding="ISO-8859-1"?> <T0020 xsi:schemaLocation="http://www.safersys.org/namespaces/T0020V1 T0020V1.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.safersys.org/namespa

我有以下xml文件作为输入

<?xml version="1.0" encoding="ISO-8859-1"?>
<T0020
    xsi:schemaLocation="http://www.safersys.org/namespaces/T0020V1 T0020V1.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.safersys.org/namespaces/T0020V1">
    <INTERFACE>
        <NAME>SAFER</NAME>
        <VERSION>04.02</VERSION>
    </INTERFACE>
    <TRANSACTION>
        <VERSION>01.00</VERSION>
        <OPERATION>REPLACE</OPERATION>
        <DATE_TIME>2009-09-01T00:00:00</DATE_TIME>
        <TZ>CT</TZ>
    </TRANSACTION>
    <IRP_ACCOUNT>
        <IRP_CARRIER_ID_NUMBER>274845</IRP_CARRIER_ID_NUMBER>
        <IRP_BASE_COUNTRY>US</IRP_BASE_COUNTRY>
        <IRP_BASE_STATE>AR</IRP_BASE_STATE>
        <IRP_ACCOUNT_NUMBER>55002</IRP_ACCOUNT_NUMBER>
        <IRP_ACCOUNT_TYPE>I</IRP_ACCOUNT_TYPE>
        <IRP_STATUS_CODE>100</IRP_STATUS_CODE>
        <IRP_STATUS_DATE>2007-11-06</IRP_STATUS_DATE>
        <IRP_UPDATE_DATE>2009-08-03</IRP_UPDATE_DATE>
        <IRP_NAME>
            <NAME_TYPE>LG</NAME_TYPE>
            <NAME>A P SUPPLY CO</NAME>
            <IRP_ADDRESS>
                <ADDRESS_TYPE>PH</ADDRESS_TYPE>
                <STREET_LINE_1>1400 N OATS</STREET_LINE_1>
                <STREET_LINE_2/>
                <CITY>TEXARKANA</CITY>
                <STATE>AR</STATE>
                <ZIP_CODE>71854</ZIP_CODE>
                <COUNTY>MILLER</COUNTY>
                <COLONIA/>
                <COUNTRY>US</COUNTRY>
            </IRP_ADDRESS>
            <IRP_ADDRESS>
                <ADDRESS_TYPE>MA</ADDRESS_TYPE>
                <STREET_LINE_1>P O BOX 1927</STREET_LINE_1>
                <STREET_LINE_2/>
                <CITY>TEXARKANA</CITY>
                <STATE>AR</STATE>
                <ZIP_CODE>75504</ZIP_CODE>
                <COUNTY/>
                <COLONIA/>
                <COUNTRY>US</COUNTRY>
            </IRP_ADDRESS>
        </IRP_NAME>  
</IRP_ACCOUNT>
<IRP_ACCOUNT> ..... </IRP_ACCOUNT>
<IRP_ACCOUNT> ..... </IRP_ACCOUNT>
<IRP_ACCOUNT> ..... </IRP_ACCOUNT>
 </T0020>

更安全的
4.02
1
代替
2009-09-01T00:00:00
计算机断层扫描
274845
美国
应收账
55002
我
100
2007-11-06
2009-08-03
LG
阿普供应公司
酸碱度
1400吨燕麦
德克萨卡纳
应收账
71854
磨坊主
美国
文科硕士
邮政信箱1927
德克萨卡纳
应收账
75504
美国
..... 
..... 
..... 
我想把这个xml文件通过如下java代码拆分成多个文件

File1.xml

<T0020>
 <IRP_ACCOUNT> ..... </IRP_ACCOUNT>
 <IRP_ACCOUNT> ..... </IRP_ACCOUNT>
</T0020>

..... 
..... 
File2.xml

<T0020>
 <IRP_ACCOUNT> ..... </IRP_ACCOUNT>
 <IRP_ACCOUNT> ..... </IRP_ACCOUNT>
</T0020>

..... 
..... 
File3.xml

<T0020>
 <IRP_ACCOUNT> ..... </IRP_ACCOUNT>
 <IRP_ACCOUNT> ..... </IRP_ACCOUNT>
</T0020>

..... 
..... 
每个xml文件最多包含10或15个IRP_帐户

谁能帮帮我吗?

又快又脏:

public class XmlSplit {

    public static void main(String [] args) throws Exception {
        File input = new File("input.xml");
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        Document doc = dbf.newDocumentBuilder().parse(input);
        XPath xpath = XPathFactory.newInstance().newXPath();

        NodeList nodes = (NodeList) xpath.evaluate("//T0020/IRP_ACCOUNT", doc, XPathConstants.NODESET);

        int itemsPerFile = 5;
        int fileNumber = 0;
        Document currentDoc = dbf.newDocumentBuilder().newDocument();
        Node rootNode = currentDoc.createElement("T0020");
        File currentFile = new File(fileNumber+".xml");
        for (int i=1; i <= nodes.getLength(); i++) {
            Node imported = currentDoc.importNode(nodes.item(i-1), true);
            rootNode.appendChild(imported);

            if (i % itemsPerFile == 0) {
                writeToFile(rootNode, currentFile);

                rootNode = currentDoc.createElement("T0020");
                currentFile = new File((++fileNumber)+".xml");
            }
        }

        writeToFile(rootNode, currentFile);
    }

    private static void writeToFile(Node node, File file) throws Exception {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.transform(new DOMSource(node), new StreamResult(new FileWriter(file)));
    }
}
公共类xmlspilt{
公共静态void main(字符串[]args)引发异常{
文件输入=新文件(“input.xml”);
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
Document doc=dbf.newDocumentBuilder().parse(输入);
XPath=XPathFactory.newInstance().newXPath();
NodeList节点=(NodeList)xpath.evaluate(“//T0020/IRP_ACCOUNT”,doc,XPathConstants.NODESET);
int itemsPerFile=5;
int fileNumber=0;
Document currentDoc=dbf.newDocumentBuilder().newDocument();
节点rootNode=currentDoc.createElement(“T0020”);
File currentFile=新文件(fileNumber+“.xml”);
对于(int i=1;i快速和肮脏:

public class XmlSplit {

    public static void main(String [] args) throws Exception {
        File input = new File("input.xml");
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        Document doc = dbf.newDocumentBuilder().parse(input);
        XPath xpath = XPathFactory.newInstance().newXPath();

        NodeList nodes = (NodeList) xpath.evaluate("//T0020/IRP_ACCOUNT", doc, XPathConstants.NODESET);

        int itemsPerFile = 5;
        int fileNumber = 0;
        Document currentDoc = dbf.newDocumentBuilder().newDocument();
        Node rootNode = currentDoc.createElement("T0020");
        File currentFile = new File(fileNumber+".xml");
        for (int i=1; i <= nodes.getLength(); i++) {
            Node imported = currentDoc.importNode(nodes.item(i-1), true);
            rootNode.appendChild(imported);

            if (i % itemsPerFile == 0) {
                writeToFile(rootNode, currentFile);

                rootNode = currentDoc.createElement("T0020");
                currentFile = new File((++fileNumber)+".xml");
            }
        }

        writeToFile(rootNode, currentFile);
    }

    private static void writeToFile(Node node, File file) throws Exception {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.transform(new DOMSource(node), new StreamResult(new FileWriter(file)));
    }
}
公共类xmlspilt{
公共静态void main(字符串[]args)引发异常{
文件输入=新文件(“input.xml”);
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
Document doc=dbf.newDocumentBuilder().parse(输入);
XPath=XPathFactory.newInstance().newXPath();
NodeList节点=(NodeList)xpath.evaluate(“//T0020/IRP_ACCOUNT”,doc,XPathConstants.NODESET);
int itemsPerFile=5;
int fileNumber=0;
Document currentDoc=dbf.newDocumentBuilder().newDocument();
节点rootNode=currentDoc.createElement(“T0020”);
File currentFile=新文件(fileNumber+“.xml”);

对于(int i=1;i Kevin,感谢您的帮助。我需要应用此方法并尽快更新您。嗨,Kevin,当我提供了大小为17 MB的xml文件时,它会在newDocumentBuilder()中为我提供java堆空间不足的异常。parse()方法。我应该做些什么来避免它?谢谢Kevin,谢谢你的帮助。我的问题现在已经解决了。hy@Kevin,希望你看到这个评论。我有一个类似但复杂的问题,我是XML pasring的新手。我有一个复杂的XML结构,像这样:……我必须将这个XML拆分为一个XML文件,但我必须维护上面的节点(还有它们的属性)。我该怎么办?Kevin,谢谢你的帮助。我需要应用这个并尽快更新你。嗨,Kevin,当我给出了大小为17 MB的xml文件时,它给了我一个例外:newDocumentBuilder()的java堆内存不足。parse()方法。我应该做些什么来避免它?谢谢Kevin,谢谢你的帮助。我的问题现在已经解决了。hy@Kevin,希望你看到这个评论。我有一个类似但复杂的问题,我是XML pasring的新手。我有一个复杂的XML结构,像这样:……我必须将这个XML拆分为一个XML文件,但我必须维护上面的节点(还有它们的属性)。我该怎么办呢?您肯定应该为该任务使用usr xpath和vtd xml。您肯定应该为该任务使用usr xpath和vtd xml。