簡體   English   中英

使用Java將兩個XML文件合並為一個XML文件

[英]Merging two XML files into one XML file using Java

我堅持如何繼續組合兩個不同的XML文件(具有相同的結構)。 當我對它進行一些研究時,人們會說必須使用像DOM或StAX這樣的XML解析器。 但我不能用常規IOStream做到這一點? 我目前正試圖在IOStream的幫助下做,但這不是解決我的目的,它更復雜。

例如,我嘗試過的是;

               public class GUI {

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

           // Creates file to write to
           Writer output = null;
           output = new BufferedWriter(new   FileWriter("C:\\merged.xml"));
           String newline = System.getProperty("line.separator");

           output.write("");

           // Read in xml file 1
           FileInputStream in = new FileInputStream("C:\\1.xml");
           BufferedReader br = new BufferedReader(new InputStreamReader(in));
           String strLine;

           while ((strLine = br.readLine()) != null) {

           if (strLine.contains("<MemoryDump>")){
           strLine = strLine.replace("<MemoryDump>", "xmlns:xsi");
           }
           if (strLine.contains("</MemoryDump>")){
           strLine = strLine.replace("</MemoryDump>", "xmlns:xsd");
          }

          output.write(newline);
          output.write(strLine);

          System.out.println(strLine);
          }

           // Read in xml file 2
           FileInputStream in = new FileInputStream("C:\\2.xml");
           BufferedReader br1 = new BufferedReader(new InputStreamReader(in));
           String strLine1;

           while ((strLine1 = br1.readLine()) != null) {

           if (strLine1.contains("<MemoryDump>")){
           strLine1 = strLine1.replace("<MemoryDump>", "");
           }
           if (strLine1.contains("</MemoryDump>")){
           strLine1 = strLine1.replace("</MemoryDump>", "");
          }

          output.write(newline);
          output.write(strLine1);

我請求您通過添加其他內容告訴我如何繼續合並兩個XML文件。 如果你能給我一些示例鏈接,那將是很棒的..!

先感謝您..! 的System.out.println(strLine1); }

}

不完全確定你要做什么。 合並你的意思是:

一種。 您希望合並2個DOM的內容,並提出一個帶有附加節點的對象模型(有效的)

您希望一個接一個地合並這兩個文件,而不關心實際內容

如果是a,請使用XML解析器。 當然你可以手工編寫這個東西並嘗試將流處理成dom對象,但是你將重寫那些解析器的大部分內容。 為什么重寫已存在的東西。

如果是b,那就做一個愚蠢的副本吧。 復制第一個文件(再次使用實用程序,例如apache common的FileUtil允許你復制文件。除非必要,否則不要寫),打開IO流到復制的文件,然后讀取和寫入第二個文件。

作為一般規則,永遠不要在詞法層面上進行任何XML處理:始終使用XML解析器。 (如果(a)您是XML專家,那么您可以違反此規則,因此您知道可能出現的問題,並且(b)您知道結果不一定是正確的。)

其次,執行此類處理的最簡單方法是使用為作業設計的語言,如XSLT或XQuery。 使用Java使它非常適合豬油。

如果您更加具體地了解要合並的文件類型以及您希望輸出的內容,我們可以為您提供更准確的答案。

package com.cts.sterling.order;

//package com.academy.ecommerce.sterling.userexits;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import com.cts.sterling.custom.accelerators.util.XMLUtil;


public class MergeXml {

    public static Document mergeXml(Document doc1, Document doc2)

            throws Exception {

        // Getting the attributes of OrderLine from document2 and document1
        NodeList objOrderLineList2 = doc2.getElementsByTagName("OrderLine");
        NodeList objOrderLineList1 = doc1.getElementsByTagName("OrderLine");

        // Creating Element for OrderLine
        Element eleOrderline2 = (Element) objOrderLineList2.item(0);
        Element eleOrderline1 = (Element) objOrderLineList1.item(0);

        // Declaring attributes as String array 
        String[] Line1={"LineType","LevelOfService"};

        // Copying attributes from document2 to document1
        XMLUtil.copyAttributes(eleOrderline2, eleOrderline1, Line1);

        // Getting the attributes of Extn from document2 and document1
        NodeList objExtn2 = doc2.getElementsByTagName("Extn");
        NodeList objExtn1 =doc1.getElementsByTagName("Extn");

        // Creating Element for Extn
        Element eleExtn2 = (Element) objExtn2.item(0);
        Element eleExtn1 = (Element) objExtn1.item(0);

        // Declaring attributes as String array
        String[] Line2={"ExtnMediaCode","ExtnLastName","ExtnGroupID"};

        // Copying attributes from document2 to document1
        XMLUtil.copyAttributes(eleExtn2, eleExtn1, Line2);

        // Getting the attributes of WSIAddnlOrderLineData from document2 and document1
        NodeList objAddln2 = doc2.getElementsByTagName("WSIAddnlOrderLineData");
        NodeList objAddln1 =doc1.getElementsByTagName("WSIAddnlOrderLineData");

        // Creating Element for WSIAddnlOrderLineData
        Element eleAddln2 = (Element) objAddln2.item(0);
        Element eleAddln1 = (Element) objAddln1.item(0);

        // Declaring attributes as String array
        String[] Line3 ={"ExtnShipMode" , "ExtnDeliverTogether","ExtnComponentReplacementIndicator","ExtnGiftCardRequiredIndicator","ExtnReplOriginalItemID", 
                "ExtnSpecialHandlingIndicator","ExtnSpecialHandlingReasonCode","ExtnCardType","ExtnCardClass","ExtnEcomSuborderNo","ExtnEcomOrderLineNo",
                "ExtnPFSFlag","ExtnSVCCarrierServiceCode","ExtnSVCSCAC","ExtnSVCUpgradeFlag","ExtnSVCSKUType","ExtnSVCTo","ExtnSVCFrom"};   

        // Copying attributes from document2 to document1
        XMLUtil.copyAttributes(eleAddln2, eleAddln1, Line3);

        // Getting the attributes of Instruction from document2 and document1
        NodeList objInst2 = doc2.getElementsByTagName("Instruction");
        NodeList objInst1 =doc1.getElementsByTagName("Instruction");

        // Creating Element for Instruction
        Element eleInst2 = (Element) objInst2.item(0);
        Element eleInst1 = (Element) objInst1.item(0);

        // Declaring attributes as String array
        String[] Line4 ={"InstructionText","InstructionType","SequenceNo","InstructionURL","InstructionUsage"}; 

        // Copying attributes from document2 to document1
        XMLUtil.copyAttributes(eleInst2, eleInst1, Line4);

        //Printing output document
        System.out.println(XMLUtil.getString(doc1));
        return doc1;

    }
    //Main method
    public static void main(String[] args) {
        try{

            File file1 = new File("D:/Handson/merge1.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc1 = dBuilder.parse(file1);
            File file2 = new File("D:/Handson/merge2.xml");
            Document doc2 = dBuilder.parse(file2);

            //calling the method
            mergeXml(doc1,doc2);
        }
        catch (Exception e) {
            e.printStackTrace();
        }

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM