2010-01-13 4 views
1

i ai suivantes fichier xml en entrée ....XML Split, dans des fichiers XML multiples

<?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> 

et je veux prendre ce fichier XML et le diviser en plusieurs fichiers dans le code java comme ça ...

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> 

et beaucoup d'autres fichiers xml .Chaque fichier xml contient 10 ou 15 au maximum IRP_ACCOUNT.

Quelqu'un peut-il m'aider s'il vous plaît?

+0

ne SURTOUT usr XPath et VTD-XML pour cette tâche. –

Répondre

16

rapide et sale:

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))); 
    } 
} 
+0

Kevin, Merci pour votre aide. Je dois appliquer cela et vous mettre à jour dès que possible. –

+0

Salut Kevin, Quand j'ai donné un fichier xml de taille 17 Mo alors il me donne l'exception de l'espace java tas de mémoire à newDocumentBuilder(). Méthode parse(). Qu'est-ce que je devrais faire pour l'éviter ?? –

+1

Augmentez la taille de votre tas? – Kevin