Java를 사용하여 병합하려는 두 개의 XML 파일이 하나의 XML 파일에 있습니다. 을 File1의Java에서 두 개의 XML 파일 병합 및 추가 정보 추가
형식 :있는 File2의
<root>
<a>
<a>--include two lines under <a>
<c/>
</a>
<d/>
</root>
형식 :
<root>
<a>
<c/>
</a>
<d/> -- include 1 more line at the last
</root>
아무도 내가 모두 파일에 특정 정보를 추가 한 후이 파일을 병합 어떻게 말해 줄 수.
그리고 이것이 내가 시도하지만, 내 목적을 해결하지 않는 것입니다 ..
public class Xml {
public static void main(String[] args) throws Exception {
Writer output = null;
output = new BufferedWriter(new FileWriter("H:\\merged_xml.xml"));
String newline = System.getProperty("line.separator");
output.write("<a>");
// Read in xml file 1
FileInputStream in = new FileInputStream("file1.xml");
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
if (strLine.contains("<a>")){
strLine = strLine.replace("<a>", "info to include");
}
if (strLine.contains("</a>")){
strLine = strLine.replace("</a>", "info to include");
}
output.write(newline);
output.write(strLine);
System.out.println(strLine);
}
// Read in xml file 2
FileInputStream in2 = new FileInputStream("file2.xml");
BufferedReader br2 = new BufferedReader(new InputStreamReader(in2));
String strLine2;
while ((strLine2 = br2.readLine()) != null) {
if (strLine2.contains("<d>")){
strLine2 = strLine2.replace("<d>", "info to include");
}
output.write(strLine2);
output.write(newline);
System.out.println(strLine2);
}
output.write("</d>");
output.close();
System.out.println("Merge Complete");
}
}
유사한 문제를 다루는 게시물을 확인해보십시오. http://stackoverflow.com/questions/648471/merge-two-xml-files-in-java – Prashanth
@Sonu : 감사합니다 ..! 하지만 구분 기호를 사용하여 원치 않는 요소를 작성하지 않도록하려면 어떻게해야합니까? – dmurali
죄송합니다, 의미가 없습니다. 작은 예제를 줄 수 있습니까? – Prashanth