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();
}
}
}
我想合併兩個XML文件並將其寫入另一個XML文件。對不起,以前不清楚。 – dmurali 2012-04-11 17:32:04
如果合併文件的*內容*,請將兩者(使用JDOM等解析器)讀入dom對象,創建一個全新的對象,您將追加到該對象,遍歷原始2個對象的模型,追加到新對象。沖洗並重復 – Michael 2012-04-11 17:42:01
我不確定我是否正確理解了你的觀點。我將不得不使用JDOM解析器讀取'file1',並從中取出必要的內容將其添加到'file2'中,是嗎? – dmurali 2012-04-11 18:01:41