0

我不知道如何继续组合两个不同的 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); }

}

4

3 回答 3

1

作为一般规则,永远不要在词法级别对 XML 进行任何处理:始终使用 XML 解析器。(如果 (a) 您是 XML 专家,因此您知道可能会出现什么问题,并且 (b) 您知道结果不一定总是正确的,则您可以打破此规则。)

其次,进行这种处理的最简单方法是使用专为这项工作设计的语言,例如 XSLT 或 XQuery。使用 Java 让它变得非常简单。

如果您更具体地了解要合并的文件类型以及您希望输出的外观,我们可以为您提供更准确的答案。

于 2012-04-12T08:22:59.133 回答
1

不完全确定您要做什么。合并是指:

一个。您想要合并 2 个 DOM 的内容并提出一个带有附加节点的对象模型(一个有效的模型)

湾。您想一个接一个地合并 2 个文件,而不关心实际内容

如果是,请使用 XML 解析器。当然,您可以手动编写内容并尝试将流处理为 dom 对象,但您将重写很多这些解析器的用途。为什么要重写已经存在的东西。

如果是b,就做一个愚蠢的副本。复制第一个文件(再次使用实用程序,像 apache common 的 FileUtil 之类的东西允许您复制文件。除非必要,否则不要写入),打开复制文件的 IO 流,然后读取和写入第二个文件。

于 2012-04-11T17:27:50.823 回答
0
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();
        }

    }
}
于 2013-08-23T06:49:19.770 回答