2013-12-23 2 views
2

pom.xml을 실행하는 동안 두 개의 XML 파일을 병합하고 싶습니다.두 XML 파일을 Maven과 병합하는 방법은 무엇입니까?

파일 1 :

<A> 
<B/> 
</A> 

파일 2 :

<A> 
<C/> 
</A> 

결과 파일 :

<A> 
<B/> 
<C/> 
</A> 

내가 어떤 플러그인을 사용할 수 있습니까?

고맙습니다.

+1

의 사용을 대답은 참조 : 사용 XSLT를 [SO-질문에 의해 설명 : 병합 - 두 XML - 파일 -을 using-xslt] [1] [1] : http://stackoverflow.com/questions/19021205/merging-two-xml-files-using-xslt –

+0

답장을 보내 주셔서 감사합니다;) – holegeek

답변

2

아래 코드를 사용하여 지정된 xpath에서 두 xml 파일을 병합 할 수 있습니다. 루트는 다른 XML을 병합해야하는 xml입니다. insertDoc은 추가해야 할 문서/노드를 전달할 수도 있습니다. 그리고 xpath는 두 번째 xml을 추가해야하는 xml의 경로입니다.

public void generateDocument(Document root, Document insertDoc, String xpath) { 

     if (null != root) { 

      Node element = insertDoc.getDocumentElement(); 
      Node dest = root.importNode(element, true); 

      try { 
       Node node = getNode(root, xpath); 
       node.insertBefore(dest, null); 
      } catch (ParserConfigurationException ex) { 
       Logger.getLogger(ProcessXML.class.getName()).log(Level.SEVERE, 
         null, ex); 
      } catch (SAXException ex) { 
       Logger.getLogger(ProcessXML.class.getName()).log(Level.SEVERE, 
         null, ex); 
      } catch (IOException ex) { 
       Logger.getLogger(ProcessXML.class.getName()).log(Level.SEVERE, 
         null, ex); 
      } catch (XPathExpressionException ex) { 
       Logger.getLogger(ProcessXML.class.getName()).log(Level.SEVERE, 
         null, ex); 
      } 

     } 

그리고 당신은 자바 코드를 실행 간부-받는다는 - 플러그인을 사용할 수는 플러그인 here

관련 문제