2012-06-22 47 views
2

我有以下XML:切割與JAXB一個XML

 


     <Package> 
     <PackageHeader> 
      <name>External Vendor File</name> 
      <description>External vendor file for some purpose</description> 
      <version>3.141694baR3</version> 
     </PackageHeader> 
     <PackageBody> 
      <Characteristic id="1"> 
       <Size> 
        <value>1.68</value> 
        <scale>Meters</scale> 
        <comment>Size can vary, depending on temperature</comment> 
       </Size> 
       <Weight> 
        <value>9</value> 
        <scale>M*Tons</scale> 
        <comment>His mama is so fat, we had to use another scale</comment> 
       </Weight> 
       <rating> 
        <ratingCompany>ISO</ratingCompany> 
        <rating:details xmlns:rating="http://www.w3schools.com/ratingDetails"> 
         <rating:value companyDepartment="Finance">A</rating:value> 
         <rating:expirationDate update="1/12/2010">1/1/2014</rating:expirationDate> 
         <rating:comment userID="z94234">You're not Silvia.</rating:comment> 
         <rating:comment userID="r24942">You're one of the Kung-Fu Creatures On The Rampage</rating:comment> 
         <rating:comment userID="i77880">TWO!</rating:comment> 
         <rating:priority>3</rating:priority> 
        </rating:details> 
       </rating> 
      </Characteristic> 
      <Characteristic id="2"> 
       <Size/>    
       <Weight/> 
       <rating/> 
      </Characteristic> 
      ... 
      <Characteristic id="n"/> 
     </PackageBody> 
    </Package> 

 

而且Java代碼:

 


    public class XMLTest { 

     public static void main(String[] args) throws Exception { 

      Package currentPackage = new Package();  
      Package sourcePackage = new Package(); 
      int totalCharacteristics; 

      PackageBody currentPackageBody = new PackageBody();  
      Characteristic currentCharacteristic = new Characteristic();   
      rating currentRating = new rating(); 

      FileInputStream fis = new FileInputStream("sourceFile.xml"); 

      JAXBContext myCurrentContext = JAXBContext.newInstance(Package.class); 
      Marshaller m = myCurrentContext.createMarshaller(); 
      Unmarshaller um = myCurrentContext.createUnmarshaller(); 

      sourcePackage = (Package)um.unmarshal(fis); 

      currentPackage.setPackageHeader(sourcePackage.getPackageHeader()); 

      totalCharacteristics = sourcePackage.getPackageBody().getCharacteristics().size(); 
      for (int i = 0; i < totalCharacteristics; i++) 
      { 
       currentRating = sourcePackage.getPackageBody().getCharacteristics().get(i).getrating(); 
      }  

      currentCharacteristic.setrating(currentRating); 
      currentPackageBody.getCharacteristics().add(currentCharacteristic); 

      currentPackage.setPackageBody(currentPackageBody);  
      m.marshal(currentPackage, new File("targetFile.xml")); 
      fis.close(); 
     } 
    } 

 

這使我的下一個XML:

 


    <Package> 
     <PackageHeader> 
      <name>External Vendor File</name> 
      <description>External vendor file for some purpose</description> 
      <version>3.141694baR3</version> 
     </PackageHeader> 
     <PackageBody> 
      <Characteristic id="1">   
       <rating> 
        <ratingCompany>ISO</ratingCompany> 
        <rating:details xmlns:rating="http://www.w3schools.com/ratingDetails"> 
         <rating:value companyDepartment="Finance">A</rating:value> 
         <rating:expirationDate update="1/12/2010">1/1/2014</rating:expirationDate> 
         <rating:comment userID="z94234">You're not Silvia.</rating:comment> 
         <rating:comment userID="r24942">You're one of the Kung-Fu Creatures On The Rampage</rating:comment> 
         <rating:comment userID="i77880">TWO!</rating:comment> 
         <rating:priority>3</rating:priority> 
        </rating:details> 
       </rating> 
      </Characteristic> 
      <Characteristic id="2">   
       <rating/>   
      </Characteristic> 
      ... 
      <Characteristic id="n"/> 
     </PackageBody> 
    </Package> 

 

這是我需要的:

 


    <Package> 
     <PackageHeader> 
      <name>External Vendor File</name> 
      <description>External vendor file for some purpose</description> 
      <version>3.141694baR3</version> 
     </PackageHeader> 
     <PackageBody> 
      <Characteristic> 
       <rating id="1"> 
        <ratingCompany>ISO</ratingCompany> 
        <rating:details xmlns:rating="http://www.w3schools.com/ratingDetails">      
         <rating:comment userID="z94234">You're not Silvia.</rating:comment> 
         <rating:comment userID="r24942">You're one of the Kung-Fu Creatures On The Rampage</rating:comment> 
         <rating:comment userID="i77880">TWO!</rating:comment> 
         <rating:priority>3</rating:priority> 
        </rating:details> 
       </rating> 
      </Characteristic> 
      <Characteristic> 
       <rating id="2"/> 
      </Characteristic> 
      ... 
      <Characteristic/> 
     </PackageBody> 
    </Package> 

 

但我有幾個問題:

  • 我怎麼可能實現的方式來讀取一個文件4GBs? (例如,用StAX讀取它)。

  • 如果我想過濾從源到目標的一些標籤(如最後一個xml),我必須將它們逐一分配給targetFile嗎?是否有任何迭代器可能允許我遍歷所有子節點並分配它們?

  • 如果sourceFile發生變化,我是否需要重新運行xjc並重新編譯整個項目?

謝謝。

回答

2

對於閱讀巨大的XML文件,您絕對需要像StAX這樣的流解析器。另外,如果你想使用它,你可以使用JAXB的組合來有選擇地將給定的xml映射到java對象。只有在模式更改時,您才需要重新生成JAXB類。如果應用程序代碼發生更改,則無需重新生成。