xml合并
利用xslt模板进行合并
/* * 合并xml文件 */public void testMerge() throws Exception{String aa = "F:/workspace/bme/mystudy/uconfig/11.xml";String bb = "F:/workspace/bme/mystudy/uconfig/22.xml";System.out.println(XmlUtil.DOM2String(mergeDocumentByXslt(getDocumentByUrl(aa),getDocumentByUrl(bb))));} private Document mergeDocumentByXslt(Document defaultDocument, Document mergedDoc) throws Exception { TransformerFactory factory = new TransformerFactoryImpl(); InputStream in = new File("F:/workspace/bme/mystudy/uconfig/uconfig-merge.xslt").toURL().openStream(); Templates templates = factory.newTemplates(new StreamSource(in)); Transformer transformer = templates.newTransformer(); javax.xml.transform.Source source = new DOMSource(defaultDocument); NodeSet nodeSet = new NodeSet(mergedDoc.getChildNodes()); transformer.setParameter("with", nodeSet); Document transformedDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); DOMResult result = new DOMResult(transformedDoc); transformer.transform(source, result); return transformedDoc; } private Document getDocumentByUrl(String fileName) throws Exception{ DOMParser parser = new DOMParser(); parser.setFeature("http://xml.org/sax/features/validation", false); parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); InputStream aa = new FileInputStream(fileName); parser.parse(new InputSource(aa)); Document doc = parser.getDocument(); aa.close(); return doc; }?